<?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)) {
    $showData = [];
    $params = [];
    $where = " WHERE o.isdel = 0 AND o.type = 'in' ";
    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 o.sys_date BETWEEN ? AND ? ";
        $params[] = $from;
        $params[] = $to;
    }
    if (isset($_GET['side_name']) && $_GET['side_name'] != "") {
        $where .= " AND o.side = ? ";
        $params[] = $_GET['side_name'];
    }
    $sql = "SELECT o.*, u.employeename 
            FROM outandin o 
            LEFT JOIN user u ON o.userid = u.userid 
            $where 
            ORDER BY o.id DESC";
    $showData = R::getAll($sql, $params);
    $allSides = R::getAll("SELECT DISTINCT side FROM sides WHERE side != '' AND isdel = 0");
    
    $smarty->assign('allSides', $allSides);
    $smarty->assign('showData', $showData);
    $smarty->display("incomereportview/report.html");
    
} elseif ($do == "report") {
    $showData = [];
    $params = [];
    $where = " WHERE o.isdel = 0 AND o.type = 'in' ";
    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 o.sys_date BETWEEN ? AND ? ";
        $params[] = $from;
        $params[] = $to;
    }
    if (isset($_GET['side_name']) && $_GET['side_name'] != "") {
        $where .= " AND o.side = ? ";
        $params[] = $_GET['side_name'];
    }
    $sql = "SELECT o.*, u.employeename 
            FROM outandin o 
            LEFT JOIN user u ON o.userid = u.userid 
            $where 
            ORDER BY o.id DESC";
    $showData = R::getAll($sql, $params);
    $allSides = R::getAll("SELECT DISTINCT side FROM sides WHERE side != '' AND isdel = 0");
    
    $smarty->assign('allSides', $allSides);
    $smarty->assign('showData', $showData);
    $smarty->display("incomereportview/report.html");

} elseif ($do == "view") { //edit request
    $id = $_GET['id'];
    
        $administrations = R::getAll("SELECT * FROM administrations WHERE isdel=0 ");
    $subjects = R::getAll("SELECT DISTINCT  id,subject FROM outandin WHERE isdel = 0");

$sql = "SELECT name AS name, id AS id, 'employee' AS type
        FROM employeesdetails 
        WHERE isdel=0
        UNION
        SELECT id, member_name AS name, 'member' AS type 
        FROM memberships 
        WHERE isdel = 0 AND request_status = 'accepted'
        ORDER BY name ASC";

$allNames = R::getAll($sql);
$smarty->assign('allNames', $allNames);


    $sides = R::findAll('sides', "isdel = 0");
    $lastOut = R::getCell("SELECT ord_num FROM outandin WHERE type = 'out' ORDER BY id DESC LIMIT 1");
    $nextOut = $lastOut ? (int)$lastOut + 1 : 1;
    $lastIn = R::getCell("SELECT incoming FROM outandin WHERE type = 'in' ORDER BY id DESC LIMIT 1");
    $nextIn = $lastIn ? (int)$lastIn + 1 : 1;
    $smarty->assign('nextOut', $nextOut);
    $smarty->assign('nextIn', $nextIn);
    $smarty->assign('administrations', $administrations);
    $smarty->assign('subjects', $subjects);
    $smarty->assign('sides', $sides);
    $smarty->assign('todayDate', $today);

    $sides =  R::findAll('sides', "isdel=0");
    $employees =  R::getAll("SELECT * FROM user ");
    $showData = R::load('outandin', $id);
    $smarty->assign('showData', $showData);
    $smarty->assign('sides', $sides);
    $smarty->assign('employees', $employees);
    $smarty->display("incomereportview/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");
    }
}