<?php
//the global file operation
include("../public/impOpreation.php");

//global varable
global $showoutside;
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');
//to check if the page from .htacess
//$showoutside = $_GET['sn'];
// get the config file
include_once("../public/config.php");

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

//here the db files that include in the file
// GOES HERE ....................
include("../public/include_dao.php");
$do = $_GET['do'];
$today = date("Y-m-d H:i:s");

if (empty($do)) {
    //$youtubes = $youtubeLinkDAO->queryAll();
    //$smarty->assign("youtubes", $youtubes);
        $employeeholidaysnull = R::findOne('employeeholidays',"ISNULL(NULLIF(holiday, ''))");
    if(!$employeeholidaysnull->id){
        $employeeholidays = R::dispense('employeeholidays');
        $employeeholidays->holiday = '';         
        $employeeholidays->holiday_date = date("Y-m-d");  
        $employeeholidays->userid = $_SESSION['userid'];
        $employeeholidays->today = $today;
        $employeeholidays->del = 0;
        R::store($employeeholidays);
        $smarty->assign('employeeholidays', $employeeholidays);
    }else{
        $employeeholidaysnull->holiday_date = date("Y-m-d");  
        $employeeholidaysnull->userid = $_SESSION['userid'];
        $employeeholidaysnull->today = $today;
        R::store($employeeholidaysnull);
        $smarty->assign('employeeholidays', $employeeholidaysnull);
    }
    $smarty->assign('append', 0);
    $smarty->display("header.html");
    $smarty->display("employeeholidaysview/add.html");
    $smarty->display("footer.html");
} else if ($do == 'addappend') {
        $employeeholidays = R::dispense('employeeholidays');
        $employeeholidays->holiday = null;         
        $employeeholidays->holiday_date = date("Y-m-d");  
        $employeeholidays->userid = $_SESSION['userid'];
        $employeeholidays->today = $today;
        $employeeholidays->del = 0;
        R::store($employeeholidays);
        $smarty->assign('employeeholidays', $employeeholidays);
        $smarty->assign('append', 1);
        $smarty->display("employeeholidaysview/add.html");

}else if ($do == 'autosave') {
    $id = filter_input(INPUT_POST, 'id');
    $name = filter_input(INPUT_POST, 'name');
    $val = filter_input(INPUT_POST, 'val');
    R::exec("update employeeholidays set $name = '" . $val . "' where id = $id");
}else if ($do == 'show') {
    $smarty->display("header.html");
    $smarty->display("employeeholidaysview/show.html");
    $smarty->display("footer.html");
}else if ($do == 'showajax') {
    showajax();
}else if ($do == 'holidays') {
    holidays();
}else if ($do == 'edit') {
    $id = filter_input(INPUT_GET, 'id');
    $employeeholidays = R::load('employeeholidays',$id);
    $smarty->assign('employeeholidays', $employeeholidays);
    $smarty->assign('append', 0);
    $smarty->display("header.html");
    $smarty->display("employeeholidaysview/edit.html");
    $smarty->display("footer.html");
}else if ($do == 'del') {
    $id = filter_input(INPUT_POST, 'id');
    $employeeholidays = R::load('employeeholidays',$id);
    $employeeholidays->del = 1;
    R::store($employeeholidays);
    echo "1";
}



function showajax() {
    global $allcutsup;
    $columns = array('employeeholidays.id', 'holiday', 'holiday_date', 'userid', 'today', 'del');
    $fromdate = $_POST['fromdate'];
    $todate = $_POST['todate'];
    $id = $_POST['data1'];
    $userid = $_POST['data2'];



    $searchQuery = " ";
    if ($id != '') {
        $searchQuery .= " and employeeholidays.id = " . $id . " ";
    }
    if ($userid != '') {
        $searchQuery .= " and employeeholidays.userid = " . $userid . " ";
    }
  
    if ($fromdate != '' && $todate != '') {
        $searchQuery .='and  employeeholidays.today  >= "' . $today . ' 00-00-00" and employeeholidays.today <= "' . $today . ' 23-59-55" ';
    }
    if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {
        $searchQuery .= "and ( employeeholidays.id LIKE " % '.$_POST["search"]["value"].' % "
                    OR employeeholidays.holiday LIKE " % '.$_POST["search"]["value"].' % "
                    OR employeeholidays.holiday_date LIKE " % '.$_POST["search"]["value"].' % "
                    OR employeeholidays.userid LIKE " % '.$_POST["search"]["value"].' % "
                    OR employeeholidays.today LIKE " % '.$_POST["search"]["value"].' % "
        )
     ";
    }


    if (isset($_POST["order"])) {
        $searchQuery .= 'ORDER BY ' . $columns[$_POST['order']['0']['column']] . ' ' . $_POST['order']['0']['dir'] . '  ';
    }
    else
    {
     $searchQuery .= "ORDER BY  employeeholidays.id DESC ";
    }

    $counts = R::count("employeeholidays", "LEFT JOIN user ON employeeholidays.userid = user.userid where holiday<>'' $searchQuery ");
    if (isset($_POST['start']) && $_POST['length'] != '-1') {
        $searchQuery .= "LIMIT " . intval($_POST['start']) . ", " .
                intval($_POST['length']);
    }


    $rResult = R::getAll("SELECT employeeholidays.*,user.username FROM `employeeholidays`
            LEFT JOIN user ON employeeholidays.userid = user.userid where holiday<>'' $searchQuery  ");

    $output = array(
        "recordsTotal" => $counts,
        "recordsFiltered" => count($rResult),
        "data" => array()
    );
    $buybillfinalbilltotal = 0;
    $sellbillfinalbilltotal = 0;
    $finaltotals = 0;
    $i = 1;
    foreach ($rResult as $row) {
        $sub_array = array();
        $sub_array[] = $row["id"];
        $sub_array[] = $row["holiday"];
        $sub_array[] = $row["holiday_date"];
        $sub_array[] = $row["username"];
        $sub_array[] = $row["today"];
        if($row["del"] == 0){
        $sub_array[] = '<a href="employeeHolidays.php?do=edit&id='.$row["id"].'" type="button" class="btn btn-default btn-lg notesicon" target="_blank"></a>';
        
        $sub_array[] = '<a href="javascript:;" data-id="'. $row["id"] .'"   type="button" class="btn btn-default btn-lg deleteicon removerow"  ></a>';
        }else{
            $sub_array[] = '';
            $sub_array[] = 'محذوف';
        }
        
        $output['data'][] = $sub_array;
    }

    echo json_encode($output, JSON_UNESCAPED_SLASHES);
}


function holidays() {
    $row_array = array();
    $return_arr = array();
    $name = $_POST['searchTerm'];
    $productsData = R::getAll("SELECT employeeholidays.id, holiday as name
    FROM employeeholidays WHERE holiday LIKE '%" . $name . "%'  limit 50");
    foreach ($productsData as $pro) {
        $row_array['id'] = $pro['id'];
        $row_array['text'] = $pro['name'];
        array_push($return_arr, $row_array);
    }
    echo json_encode($return_arr);
}

?>