<?php

//the global file operation
include("../public/impOpreation.php");
//global varable
global $showoutside;

//to check if the page from .htacess
//$showoutside = $_GET['sn'];
// get the config file
include_once("../public/config.php");

//here the db files that include in the file
include("../public/include_dao.php");

//Tamweenda3mstatus
require_once('../models/dao/Tamweenda3mstatusDAO.class.php');
require_once('../models/dto/Tamweenda3mstatu.class.php');
require_once('../models/mysql/Tamweenda3mstatusMySqlDAO.class.php');
require_once('../models/mysql/ext/Tamweenda3mstatusMySqlExtDAO.class.php');
//Client
require_once('../models/dao/ClientDAO.class.php');
require_once('../models/dto/Client.class.php');
require_once('../models/mysql/ClientMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientMySqlExtDAO.class.php');

require_once('../models/dao/YoutubeLinkDAO.class.php');
require_once('../models/dto/YoutubeLink.class.php');
require_once('../models/mysql/YoutubeLinkMySqlDAO.class.php');
require_once('../models/mysql/ext/YoutubeLinkMySqlExtDAO.class.php');

//get the do the action
$do = $_GET['do'];
$langs = $_SESSION['erp_lang'];
include_once("../views/languages/$langs/success.php");
include_once("../views/languages/$langs/error.php");
/* ======================
  Controller Name :-
  ======================== */

//here the global templates
$smarty->display("header.html");

//Tamweenda3mstatus
$tamweenDa3mStatus = new Tamweenda3mstatu();
$tamweenDa3mStatusDAO = new Tamweenda3mstatusMySqlDAO();
$tamweenDa3mStatusEX = new Tamweenda3mstatusMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();

//Client
$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientEX = new ClientMySqlExtDAO();

//check and use the condition that suite this action
if (empty($do) || $do == "show") {
    //here the permssion check
    include_once("../public/authentication.php");

    $month = filter_input(INPUT_POST, 'month');
    if (!empty($month)) {
        $month = $month . "-01";
        $queryStringJoin .=" and month = '" . $month . "' ";
        $monthData = $clientEX->queryAllTamweenDa3mStatus($queryStringJoin, ' and client.clientid !=1 ');
        $smarty->assign("monthData", $monthData);
        $smarty->assign("month", $month);
    }

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    //here the smarty templates
    $smarty->display("tamweenda3mstatusview/show.html");
} elseif ($do == "save") {
    //here the permssion check
    include_once("../public/authentication.php");
    try {
        saveStatusDa3m();
        header("location:?do=sucess");
    } catch (Exception $e) {
        //echo $e;
        header("location:?do=error");
    }
}
if ($do == "showtamween") {
    //here the permssion check
    include_once("../public/authentication.php");

    $month = filter_input(INPUT_POST, 'month');
    $showAll = (int) filter_input(INPUT_POST, 'showAll');
    $smarty->assign("showAll", $showAll);
    if (!empty($month)) {
        $month = $month . "-01";
        $queryStringJoin .=" and month = '" . $month . "' ";
        $monthData = $clientEX->queryAllTamweenDa3mStatus2($queryStringJoin, ' and client.clientid !=1 ');
        $smarty->assign("monthData", $monthData);
        $smarty->assign("month", $month);
    }

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    //here the smarty templates
    $smarty->display("tamweenda3mstatusview/showtamween.html");
} elseif ($do == "savetamween") {
    //here the permssion check
    include_once("../public/authentication.php");
    try {
        saveStatusTamween();

        $note = " تمت العملية بنجاح";
        $url = "tamweenda3mstatus.php?do=showtamween";
        $smarty->assign('urldirect', $url);
        $smarty->assign('msgnote', $note);
        $smarty->display("notes2.html");
//        header("location:?do=sucess");
    } catch (Exception $e) {
        //echo $e;
        header("location:?do=error");
    }
} elseif ($do == "sucess") {
//here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {
//here the smarty templates
    $smarty->display("error.html");
}



$smarty->assign("customValidation", 1);
$smarty->assign("customTamween", 1);

//here the global templates
$smarty->display("footer.html");

/* ===============================
  function in this CONTROLLER
  ================================ */

function saveStatusDa3m($param) {
    global $tamweenDa3mStatus;
    global $tamweenDa3mStatusDAO;
    global $tamweenDa3mStatusEX;

    $itr = (int) filter_input(INPUT_POST, 'itr');
    $month = filter_input(INPUT_POST, 'month_result');

    for ($i = 1; $i <= $itr; $i++) {
        $tamweenda3mstatusid = (int) filter_input(INPUT_POST, 'tamweenda3mstatusid' . $i);
        $clientid = (int) filter_input(INPUT_POST, 'clientid' . $i);
        $da3mStatus = (int) filter_input(INPUT_POST, 'da3mStatus' . $i);
        $oldStatus = (int) filter_input(INPUT_POST, 'oldStatus' . $i);
        if ($clientid > 0 && !empty($month)) {
            $tamweenDa3mStatus->clientid = $clientid;
            $tamweenDa3mStatus->month = $month;
            $tamweenDa3mStatus->del = 0;
            if ($tamweenda3mstatusid > 0) {
                if ($da3mStatus != $oldStatus) {//if value changed only to save old data of user and date and low server headic
//                    $tamweenDa3mStatus->da3mStatus = $da3mStatus;
//                    $tamweenDa3mStatus->userid = $_SESSION['userid'];
//                    $tamweenDa3mStatus->sysdate = date('Y-m-d H:i:s');
//
//                    $tamweenDa3mStatus->id = $tamweenda3mstatusid;
                    $tamweenDa3mStatusEX->updateDa3m($da3mStatus, $_SESSION['userid'], date('Y-m-d H:i:s'), $tamweenda3mstatusid);
                }
            } else {
                $tamweenDa3mStatus->da3mStatus = $da3mStatus;
                $tamweenDa3mStatus->userid = $_SESSION['userid'];
                $tamweenDa3mStatus->sysdate = date('Y-m-d H:i:s');
                //
                $tamweenDa3mStatus->tamweenStatus = 0;
                $tamweenDa3mStatus->useridTamween = 0;
                $tamweenDa3mStatus->sysdateTamween = '';
                $tamweenDa3mStatusDAO->insert($tamweenDa3mStatus);
            }
        }
    }
}

function saveStatusTamween($param) {
    global $tamweenDa3mStatus;
    global $tamweenDa3mStatusDAO;
    global $tamweenDa3mStatusEX;

    $itr = (int) filter_input(INPUT_POST, 'itr');
    $month = filter_input(INPUT_POST, 'month_result');

    for ($i = 1; $i <= $itr; $i++) {
        $tamweenda3mstatusid = (int) filter_input(INPUT_POST, 'tamweenda3mstatusid' . $i);
        $clientid = (int) filter_input(INPUT_POST, 'clientid' . $i);
        $tamweenStatus = (int) filter_input(INPUT_POST, 'tamweenStatus' . $i);
        $oldStatus = (int) filter_input(INPUT_POST, 'oldStatus' . $i);
        if ($clientid > 0 && !empty($month)) {
            $tamweenDa3mStatus->clientid = $clientid;
            $tamweenDa3mStatus->month = $month;
            $tamweenDa3mStatus->del = 0;

            if ($tamweenda3mstatusid > 0) {
                if ($tamweenStatus != $oldStatus) {//if value changed only to save old data of user and date and low server headic
//                    $tamweenDa3mStatus->tamweenStatus = $tamweenStatus;
//                    $tamweenDa3mStatus->useridTamween = $_SESSION['userid'];
//                    $tamweenDa3mStatus->sysdateTamween = date('Y-m-d H:i:s');
//
//                    $tamweenDa3mStatus->id = $tamweenda3mstatusid;
                    //$tamweenDa3mStatusDAO->update($tamweenDa3mStatus);
                    $tamweenDa3mStatusEX->updateTamween($tamweenStatus, $_SESSION['userid'], date('Y-m-d H:i:s'), $tamweenda3mstatusid);
                }
            } else {
                $tamweenDa3mStatus->tamweenStatus = $tamweenStatus;
                $tamweenDa3mStatus->useridTamween = $_SESSION['userid'];
                $tamweenDa3mStatus->sysdateTamween = date('Y-m-d H:i:s');
                //
                $tamweenDa3mStatus->da3mStatus = 0;
                $tamweenDa3mStatus->userid = 0;
                $tamweenDa3mStatus->sysdate = '';
                $tamweenDa3mStatusDAO->insert($tamweenDa3mStatus);
            }
        }
    }
}

?>