<?php

session_start();
ob_start();
$do = isset($_GET['do']) ? $_GET['do'] : "";
$ajaxDoArr = array("add2SimpleReturn");
include("initiateStaticSessionCommingWithCurl.php");
if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
    array_push($ajaxDoArr, $do);
}

if (!in_array($do, $ajaxDoArr)) {
    include("../public/impOpreation.php");
}



include_once("../public/config.php");
include("../library/newUplode/uploadfunctions.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");


if (!in_array($do, $ajaxDoArr)) {
    if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {

    } else {
        $smarty->display("header.html");
    }
}



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

$today = date("Y-m-d");
//check and use the condition that suite this action
if (empty($do)) {
    //here the permission check
    $showData = [];
    $params = [];

    $where = " WHERE s.isdel = 0 ";
    if (isset($_GET['from_date']) && $_GET['from_date'] != "" && isset($_GET['to_date']) && $_GET['to_date'] != "") {
        $from = date('Y-m-d 00:00:00', strtotime($_GET['from_date']));
        $to   = date('Y-m-d 23:59:59', strtotime($_GET['to_date'])); 
        
        $where .= " AND s.session_date BETWEEN ? AND ? ";
        $params[] = $from;
        $params[] = $to;
    }

    $sql = "SELECT s.*, c.subject , u.employeename
        FROM sessions s 
        LEFT JOIN cases c ON s.case = c.subject 
        LEFT JOIN user u ON s.userid = u.userid
                $where 
                ORDER BY s.id DESC";
    $showData = R::getAll($sql, $params);
    $smarty->assign('showData', $showData);
    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    $smarty->display("sessionsreportview/report.html");
    $smarty->assign("customCheck", 1);

} elseif ($do == "report") {
  $showData = [];
    $params = [];

    $where = " WHERE s.isdel = 0 ";
    if (isset($_GET['from_date']) && $_GET['from_date'] != "" && isset($_GET['to_date']) && $_GET['to_date'] != "") {
        $from = date('Y-m-d 00:00:00', strtotime($_GET['from_date']));
        $to   = date('Y-m-d 23:59:59', strtotime($_GET['to_date'])); 
        
        $where .= " AND s.session_date BETWEEN ? AND ? ";
        $params[] = $from;
        $params[] = $to;
    }

    $sql = "SELECT s.*, c.subject , u.employeename
        FROM sessions s 
        LEFT JOIN cases c ON s.case = c.subject 
        LEFT JOIN user u ON s.userid = u.userid
                $where 
                ORDER BY s.id DESC";
    $showData = R::getAll($sql, $params);
    $smarty->assign('showData', $showData);
    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    $smarty->display("sessionsreportview/report.html");
    $smarty->assign("customCheck", 1);

} elseif ($do == "view") { //edit request
    $id = $_GET['id'];
   
    $showData = R::load('sessions', $id);
    $cases = R::findAll('cases', "status=0 AND isdel = 0");
    $smarty->assign('showData', $showData);
   
    $smarty->assign('cases', $cases);
    
    $smarty->display("sessionsreportview/view.html");

} 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)) {
    if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {

    } else {
        $smarty->display("footer.html");
    }
}