<?php

$do = isset($_GET['do']) ? $_GET['do'] : '';
$ajaxDoArr = array('getattendancesystemajax', 'getsubgroups', 'getgroups', 'getgroupAttendanceData', 'getsubgroupAttendanceData');
if (!in_array($do, $ajaxDoArr)) {
    //the global file operation
    include("../public/impOpreation.php");
}

//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
// GOES HERE ....................
include("../public/include_dao.php");

//Employeegroup
require_once('../models/dao/EmployeegroupDAO.class.php');
require_once('../models/dto/Employeegroup.class.php');
require_once('../models/mysql/EmployeegroupMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeegroupMySqlExtDAO.class.php');
//Employeesubgroup
require_once('../models/dao/EmployeesubgroupDAO.class.php');
require_once('../models/dto/Employeesubgroup.class.php');
require_once('../models/mysql/EmployeesubgroupMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeesubgroupMySqlExtDAO.class.php');
//Employeeattendancesystem
require_once('../models/dao/EmployeeattendancesystemDAO.class.php');
require_once('../models/dto/Employeeattendancesystem.class.php');
require_once('../models/mysql/EmployeeattendancesystemMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeeattendancesystemMySqlExtDAO.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 :-storeCTRL
  ======================== */

//here the global templates
if (!in_array($do, $ajaxDoArr)) {
    $smarty->display("header.html");
}
//Employeegroup
$employeeGroup = new Employeegroup();
$employeeGroupDAO = new EmployeegroupMySqlDAO();
$employeeGroupEX = new EmployeegroupMySqlExtDAO();
//Employeesubgroup
$employeeSubGroup = new Employeesubgroup();
$employeeSubGroupDAO = new EmployeesubgroupMySqlDAO();
$employeeSubGroupEX = new EmployeesubgroupMySqlExtDAO();
//Employeeattendancesystem
$employeeAttendanceSystem = new Employeeattendancesystem();
$employeeAttendanceSystemDAO = new EmployeeattendancesystemMySqlDAO();
$employeeAttendanceSystemEX = new EmployeeattendancesystemMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();
//Store
//check and use the condetion that suite this action
if (empty($do)) {
    //here the permssion check
    include_once("../public/authentication.php");

    $employeeAttendanceSystem = $employeeAttendanceSystemDAO->queryByDel(0);
    $smarty->assign('employeeAttendanceSystem', $employeeAttendanceSystem);

    //here the smarty templates
    $smarty->display("employeegroupsview/add.html");
} elseif ($do == "add") {

    //here the permssion check
    include_once("../public/authentication.php");
    try {
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "show") {
    //here the permssion check
    include_once("../public/authentication.php");

    $showData = $employeeGroupDAO->queryByDel(0);
    $smarty->assign("showData", $showData);

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

    $smarty->display("employeegroupsview/show.html");

    $smarty->assign("customCheck", 1);
} elseif ($do == "delete") {
    include_once("../public/authentication.php");

    // here the function that do the action

    try {
        $id = (int) $_GET['id'];
        $employeeGroupDAO->delete($id);
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "edit") {
    //here the permssion check
    include_once("../public/authentication.php");
    $id = (int) $_GET['id'];
    $loadData = $employeeGroupDAO->load($id);
    $smarty->assign("loadData", $loadData);

    $employeeAttendanceSystem = $employeeAttendanceSystemDAO->queryByDel(0);
    $smarty->assign('employeeAttendanceSystem', $employeeAttendanceSystem);

    $smarty->display("employeegroupsview/edit.html");
}
//edit  update
elseif ($do == "update") {

    //here the permssion check
    include_once("../public/authentication.php");
    //
    try {
        // here the function that do the action
        update();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "getattendancesystemajax") {
    $id = (int) filter_input(INPUT_POST, 'id');
    $employeeGroup = $employeeGroupDAO->load($id);
    echo $employeeGroup->employeeattendancesystemid;
} elseif ($do == "getsubgroups") {
    getsubgroups();
} elseif ($do == "getgroups") {
    getgroups();
} elseif ($do == "getgroupAttendanceData") {
      include("../public/impOpreation.php");
    $id = (int) filter_input(INPUT_POST, 'id');
    $row = $employeeGroupDAO->load($id);
    $stem = $employeeAttendanceSystemDAO->load($row->employeeattendancesystemid);
    $stemweek = R::getAll('select * from employeeattendancesystemweek where employeeattendancesystem_id = ?',[$row->employeeattendancesystemid]);
    echo json_encode(array($stem,$stemweek));
} elseif ($do == "getsubgroupAttendanceData") {
    include("../public/impOpreation.php");
    $id = (int) filter_input(INPUT_POST, 'id');
    $row = $employeeSubGroupDAO->load($id);
    $stem = $employeeAttendanceSystemDAO->load($row->employeeattendancesystemid);
    $stemweek = R::getAll('select * from employeeattendancesystemweek where employeeattendancesystem_id = ?',[$row->employeeattendancesystemid]);
    echo json_encode(array($stem,$stemweek));    
} 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);

//here the global templates
if (!in_array($do, $ajaxDoArr)) {
    $smarty->display("footer.html");
}

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

// add in Storerawmaterial tbl
function add() {
    //to use the variable out side the funcion
    global $employeeGroup;
    global $employeeGroupDAO;

    //add in tbl
    $employeeGroup->name = filter_input(INPUT_POST, 'name');
    $employeeGroup->employeeattendancesystemid = (float) filter_input(INPUT_POST, 'employeeattendancesystemid');

    $employeeGroup->userid = $_SESSION["userid"];
    $employeeGroup->del = 0;
    $employeeGroup->sysdate = date("Y-m-d H:i:s");

    $employeeGroupDAO->insert($employeeGroup);
}

function update() {
    //to use the variable out side the funcion
    global $employeeGroup;
    global $employeeGroupDAO;

    $id = (int) filter_input(INPUT_POST, 'id');
    $employeeGroup = $employeeGroupDAO->load($id);
    $employeeGroup->name = filter_input(INPUT_POST, 'name');
    $employeeGroup->employeeattendancesystemid = (float) filter_input(INPUT_POST, 'employeeattendancesystemid');

    $employeeGroup->userid = $_SESSION["userid"];
    $employeeGroup->del = 0;
    $employeeGroup->sysdate = date("Y-m-d H:i:s");

    $employeeGroupDAO->update($employeeGroup);
}

//
function getsubgroups() {
    global $employeeSubGroupEX;

    $row_array = array();
    $return_arr = array();

    $name = $_GET['term']; // name
    $group = (int) $_GET['group'];
    $day = $_GET['day'];

    if (isset($day) && !empty($day)) {
        $result = $employeeSubGroupEX->queryByNameAndGroupWithDayStatue($name, $group, $day);
    } else {
        $result = $employeeSubGroupEX->queryByNameAndGroup($name, $group);
    }

    if (count($result) > 0) {
        foreach ($result as $row) {
            $row_array['id'] = $row->id;
            $row_array['text'] = $row->name;
            $row_array['status'] = (int) $row->status;
            array_push($return_arr, $row_array);
        }
    }

    //echo $return_arr;
    echo json_encode($return_arr);
}

function getgroups() {
    global $employeeGroupEX;

    $row_array = array();
    $return_arr = array();

    $name = $_GET['term']; // name
    $group = (int) $_GET['group'];
    $day = $_GET['day'];

    if (isset($day) && !empty($day)) {
        $result = $employeeGroupEX->queryByNameWithDayStatue($name, $day);
    } else {
        $result = $employeeGroupEX->queryByNameEX($name);
    }

    if (count($result) > 0) {
        foreach ($result as $row) {
            $row_array['id'] = $row->id;
            $row_array['text'] = $row->name;
            $row_array['status'] = (int) $row->status;
            array_push($return_arr, $row_array);
        }
    }

    //echo $return_arr;
    echo json_encode($return_arr);
}

?>