<?php

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");

//Movementmanage
require_once('../models/dao/MovementmanageDAO.class.php');
require_once('../models/dto/Movementmanage.class.php');
require_once('../models/mysql/MovementmanageMySqlDAO.class.php');
require_once('../models/mysql/ext/MovementmanageMySqlExtDAO.class.php');

//User
require_once('../models/dao/UserDAO.class.php');
require_once('../models/dto/User.class.php');
require_once('../models/mysql/UserMySqlDAO.class.php');
require_once('../models/mysql/ext/UserMySqlExtDAO.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'];

/* ======================

  Controller Name :- movementmanageCTRL

  OPERTATION in Controller

  1- show registered user
  2- admin accept their registration or not
  3-
  4-

  ======================== */

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

//here goes the instances and general variables
//Movementmanage
$myMovementmanage = new Movementmanage();
$myMovementmanageRecord = new MovementmanageMySqlDAO();
$myMovementmanageEx = new MovementmanageMySqlExtDAO();
//User
$myUserRecord = new UserMySqlDAO();

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

//check and use the condetion that suite this action

if (empty($do) || $do == "show") {
    $usersearchData = loadUser();
    $smarty->assign("usersearchData", $usersearchData);


    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $userSearchId = $_REQUEST['usersearchid'];

    if (isset($startDate) && $startDate != "" && isset($endDate) && $endDate != "" && $userSearchId == "-1") {
        $message = "تقرير الدخول من تاريخ:" . $startDate . "الى تاريخ :" . $endDate;
        $smarty->assign('message', $message);

        showByDate($startDate, $endDate);
    } elseif (isset($userSearchId) && $userSearchId != "-1" && $startDate == "" && $endDate == "") {

        $myuserdata = $myUserRecord->load($userSearchId);
        $message = "تقرير الدخول للموظف :" . $myuserdata->employeename;
        $smarty->assign('message', $message);
        showByUser($userSearchId);
    } elseif (isset($startDate) && $startDate != "" && isset($endDate) && $endDate != "" && isset($userSearchId) && $userSearchId != "-1") {

        $myuserdata = $myUserRecord->load($userSearchId);
        $message = "تقرير الدخول للموظف :" . $myuserdata->employeename . " <br> من تاريخ:" . $startDate . "الى تاريخ :" . $endDate;
        $smarty->assign('message', $message);
        showByDateAndUser($startDate, $endDate, $userSearchId);
    } else {
        $message = "تقرير دخول جميع المستخدمين";
        $smarty->assign('message', $message);
        $today = date("Y-m-d");
        showByDate($today, $today);
    }

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

    //here the smarty templates
    $smarty->display("presencereportview/show.html");
} elseif ($do == "sucess") {
    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("error.html");
}
//here the global templates
$smarty->display("footer.html");

/////////////////////////////////////////////////////////

function loadUser() {
    global $myUserRecord;

    $usersearchData = $myUserRecord->queryByConditions(0);

    return $usersearchData;
}

function showByDate($startDate, $endDate) {
    global $smarty;
    global $myMovementmanageRecord;
    global $myMovementmanageEx;


    $movementmanageData = $myMovementmanageEx->queryAllWithOpnameAndDate($startDate, $endDate);

    $smarty->assign('movementmanageData', $movementmanageData);
}

function showByUser($userSearchId) {
    global $smarty;
    global $myMovementmanageRecord;
    global $myMovementmanageEx;

    //$movementmanageData = $myMovementmanageEx->queryWithUserid($userSearchId);
    $movementmanageData = $myMovementmanageEx->queryAllWithUserAndOperationnameGrouped($userSearchId, 'index.php');
    $smarty->assign('movementmanageData', $movementmanageData);
}

function showByDateAndUser($startDate, $endDate, $userSearchId) {
    global $smarty;
    global $myMovementmanageRecord;
    global $myMovementmanageEx;

    //$movementmanageData = $myMovementmanageEx->queryAllWithDateAndUser($startDate, $endDate, $userSearchId);
    $movementmanageData = $myMovementmanageEx->queryAllWithOpnameAndDateAndUserGrouped($startDate, $endDate, $userSearchId);
    $smarty->assign('movementmanageData', $movementmanageData);
}

?>