<?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");
if (empty($do)) {
 $showData = [];
    $where = " WHERE isdel = 0 "; 
    $params = [];
    if (isset($_POST['from']) && $_POST['from'] != "" && isset($_POST['to']) && $_POST['to'] != "") {
        $from = date('Y-m-d 00:00:00', strtotime($_POST['from']));
        $to   = date('Y-m-d 23:59:59', strtotime($_POST['to']));
        $where .= " AND sys_date BETWEEN ? AND ? ";
        $params[] = $from;
        $params[] = $to;
    }
    if (isset($_POST['car_name']) && $_POST['car_name'] != "") {
        $where .= " AND car = ? ";
        $params[] = $_POST['car_name'];
    }
    $sql = "SELECT * FROM carsdetails $where ORDER BY id DESC";
    $showData = R::getAll($sql, $params);
    $allCars = R::getAll("SELECT DISTINCT car FROM carsdetails WHERE isdel = 0");
    $smarty->assign('allCars', $allCars);

    $smarty->assign('showData', $showData);
    $smarty->display("carsreportview/report.html");

} elseif ($do == "report") {
   $showData = [];
    $where = " WHERE isdel = 0 ";
    $params = [];
    if (isset($_POST['from']) && $_POST['from'] != "" && isset($_POST['to']) && $_POST['to'] != "") {
        $from = date('Y-m-d 00:00:00', strtotime($_POST['from']));
        $to   = date('Y-m-d 23:59:59', strtotime($_POST['to']));
        $where .= " AND sys_date BETWEEN ? AND ? ";
        $params[] = $from;
        $params[] = $to;
    }
    if (isset($_POST['car_name']) && $_POST['car_name'] != "") {
        $where .= " AND car = ? ";
        $params[] = $_POST['car_name'];
    }
    $sql = "SELECT * FROM carsdetails $where ORDER BY id DESC";
    $showData = R::getAll($sql, $params);
    $allCars = R::getAll("SELECT DISTINCT car FROM carsdetails WHERE isdel = 0");
    $smarty->assign('allCars', $allCars);
    $smarty->assign('showData', $showData);
    $smarty->display("carsreportview/report.html");

} elseif ($do == "view") { //edit request
    $id = $_GET['id'];

    $sql = "SELECT c.* , l.* , g.* , f.* , l.date AS l_date , l.comment AS l_comment , l.images AS l_images , g.comment AS g_comment , g.images AS g_images , f.images AS f_images
            FROM carsdetails c
            LEFT JOIN ltinerary l ON c.car = l.car
            LEFT JOIN gasoline g ON l.car = g.car
            LEFT JOIN fixes f ON g.car = f.car
            WHERE c.id = $id AND c.isdel = 0";
   $showData =R::getRow($sql); 
   
    
    $smarty->assign('showData', $showData);
    $smarty->display("carsreportview/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");
    }
}