<?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'];

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

        $queryString .= ' and date(movementmanagedate) >= "' . $startDate . '" and date(movementmanagedate) <= "' . $endDate . '" ';
    }
    if ($userSearchId > 0) {

        $myuserdata = $myUserRecord->load($userSearchId);
        $message = "تقرير الدخول للموظف :" . $myuserdata->employeename;
        $smarty->assign('message', $message);
        $queryString .= ' and movementmanage.userid = ' . $userSearchId . ' ';
    }

    if (empty($queryString)) {
        $message = "تقرير دخول جميع المستخدمين";
        $smarty->assign('message', $message);
        $today = date("Y-m-d");
        $queryString = ' and date(movementmanagedate) >= "' . $today . '" and date(movementmanagedate) <= "' . $today . '" ';
    }

    $movementmanageData = R::getAll('SELECT movementmanage.*, user.username
		 FROM movementmanage
                 JOIN user ON movementmanage.userid = user.userid
		 where movementmanage.operationname = "index.php" and movementmanage.userid > 0
                 ' . $queryString . '
                order by movementmanage.userid,movementmanage.movementmanageid');
    $movementmanageData = customArrayManyIndexMany($movementmanageData, array('userid', 'username')); //customArrayIndexMany
    $smarty->assign("movementmanageData", $movementmanageData);

    $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);
}

?>