<?php
include("../public/impOpreation.php");
include("../library/uploadImages.php");
$do = $_GET['do'];
$today = date("Y-m-d H:i:s");
$date = date("Y-m-d");

if (empty($do)) {
    $smarty->display("header.html");


    $allChasis = R::getAll("select DISTINCT chasisno from importcontract where chasisno != '' and del <2");
    $smarty->assign('allChasis', $allChasis);

    $smarty->display("carTrackingView/add.html");
    $smarty->display("footer.html");
} elseif ($do == "show") {
    $chasis = filter_input(INPUT_POST, 'chasis');
    $status = filter_input(INPUT_POST, 'status');

    $qs = "where 1 ";
    if ($chasis) {
        $qs .= " and chasis = '$chasis' ";
        $smarty->assign('chasis', $chasis);
    }
    $smarty->assign('status', $status);

    if ($status == "buy")
        $qs .= " and buydate != '0000-00-00 00:00:00'  and warehousedate = '0000-00-00 00:00:00' and e2date = '0000-00-00 00:00:00' and portdate = '0000-00-00 00:00:00' and arrivaldate = '0000-00-00 00:00:00' and showroomdate = '0000-00-00 00:00:00' and deliverdate = '0000-00-00 00:00:00' ";
    elseif ($status == "warehouse")
        $qs .= " and warehousedate != '0000-00-00 00:00:00' and e2date = '0000-00-00 00:00:00' and portdate = '0000-00-00 00:00:00' and arrivaldate = '0000-00-00 00:00:00' and showroomdate = '0000-00-00 00:00:00' and deliverdate = '0000-00-00 00:00:00' ";
    elseif ($status == "e2")
        $qs .= " and e2date != '0000-00-00 00:00:00' and portdate = '0000-00-00 00:00:00' and arrivaldate = '0000-00-00 00:00:00' and showroomdate = '0000-00-00 00:00:00' and deliverdate = '0000-00-00 00:00:00' ";
    elseif ($status == "port")
        $qs .= " and portdate != '0000-00-00 00:00:00' and arrivaldate = '0000-00-00 00:00:00' and showroomdate = '0000-00-00 00:00:00' and deliverdate = '0000-00-00 00:00:00' ";
    elseif ($status == "arrival")
        $qs .= " and arrivaldate != '0000-00-00 00:00:00' and showroomdate = '0000-00-00 00:00:00' and deliverdate = '0000-00-00 00:00:00' ";
    elseif ($status == "showroom")
        $qs .= " and showroomdate != '0000-00-00 00:00:00' and deliverdate = '0000-00-00 00:00:00' ";
    elseif ($status == "deliver")
        $qs .= " and deliverdate != '0000-00-00 00:00:00'";

    $trackingData = R::findAll('cartracking', $qs);
    foreach ($trackingData  as $single) {
        $single->lastuser = R::findOne('user', 'userid = ?', [$single->userid])->employeename;
        if ($single->deliverdate != "0000-00-00 00:00:00") {
            $single->status = "Delivered to the customer";
            $single->lastdate = $single->deliverdate;
        } elseif ($single->showroomdate != "0000-00-00 00:00:00") {
            $single->status = "vroom showroom";
            $single->lastdate = $single->showroomdate;
        } elseif ($single->arrivaldate != "0000-00-00 00:00:00") {
            $single->status = "Viking warehouse";
            $single->lastdate = $single->arrivaldate;
        } elseif ($single->portdate != "0000-00-00 00:00:00") {
            $single->status = "Port of Egypt";
            $single->lastdate = $single->portdate;
        } elseif ($single->e2date != "0000-00-00 00:00:00") {
            $single->status = "Ocean flight";
            $single->lastdate = $single->e2date;
        } elseif ($single->warehousedate != "0000-00-00 00:00:00") {
            $single->status = "warehouse";
            $single->lastdate = $single->warehousedate;
        } elseif ($single->buydate != "0000-00-00 00:00:00") {
            $single->status = "Dealer showroom";
            $single->lastdate = $single->buydate;
        } else {
            $single->status = "لم يتم التتبع بعد";
            $single->lastdate = "";
        }
    }
    $smarty->assign('trackingData', $trackingData);

    $allChasis = R::getAll("select DISTINCT chasisno from importcontract where chasisno != '' and del <2");
    $smarty->assign('allChasis', $allChasis);

    $smarty->display("header.html");
    $smarty->display("carTrackingView/show.html");
    $smarty->display("footer.html");
} elseif ($do == "edit") {
    $id = filter_input(INPUT_GET, 'id');
    $trackingData = R::load('cartracking', $id);
    $smarty->assign('trackingData', $trackingData);

    $allChasis = R::getAll("select DISTINCT chasisno from importcontract where chasisno != '' and del <2");
    $smarty->assign('allChasis', $allChasis);

    $smarty->display("header.html");
    $smarty->display("carTrackingView/edit.html");
    $smarty->display("footer.html");
} elseif ($do == "editprint") {
    $id = filter_input(INPUT_GET, 'id');
    $trackingData = R::load('cartracking', $id);
    
    $trackingData['lastuser'] = R::findOne('user', 'userid = ?', [$trackingData['userid']])->employeename;
    $trackingData['buyuser'] = R::findOne('user', 'userid = ?', [$trackingData['buyuserid']])->employeename;
    $trackingData['warehouseuser'] = R::findOne('user', 'userid = ?', [$trackingData['warehouseuserid']])->employeename;
    $trackingData['e2user'] = R::findOne('user', 'userid = ?', [$trackingData['e2userid']])->employeename;
    $trackingData['portuser'] = R::findOne('user', 'userid = ?', [$trackingData['portuserid']])->employeename;
    $trackingData['arrivaluser'] = R::findOne('user', 'userid = ?', [$trackingData['arrivaluserid']])->employeename;
    $trackingData['showroomuser'] = R::findOne('user', 'userid = ?', [$trackingData['showroomuserid']])->employeename;
    $trackingData['deliveruser'] = R::findOne('user', 'userid = ?', [$trackingData['deliveruserid']])->employeename;

    $smarty->assign('trackingData', $trackingData);

    $allChasis = R::getAll("select DISTINCT chasisno from importcontract where chasisno != '' and del <2");
    $smarty->assign('allChasis', $allChasis);

    $smarty->display("header.html");
    $smarty->display("carTrackingView/editprint.html");
    $smarty->display("footer.html");
}  else if ($do == 'savedata') {
    $chasis = filter_input(INPUT_POST, 'chasis');
    $buydate = filter_input(INPUT_POST, 'buydate');
    $buycomment = filter_input(INPUT_POST, 'buycomment');
    $warehousedate = filter_input(INPUT_POST, 'warehousedate');
    $warehousecomment = filter_input(INPUT_POST, 'warehousecomment');
    $e2date = filter_input(INPUT_POST, 'e2date');
    $e2comment = filter_input(INPUT_POST, 'e2comment');
    $portdate = filter_input(INPUT_POST, 'portdate');
    $portcomment = filter_input(INPUT_POST, 'portcomment');
    $arrivaldate = filter_input(INPUT_POST, 'arrivaldate');
    $arrivalcomment = filter_input(INPUT_POST, 'arrivalcomment');
    $showroomdate = filter_input(INPUT_POST, 'showroomdate');
    $showroomcomment = filter_input(INPUT_POST, 'showroomcomment');
    $deliverdate = filter_input(INPUT_POST, 'deliverdate');
    $delivercomment = filter_input(INPUT_POST, 'delivercomment');
    $id = filter_input(INPUT_POST, 'id');
    if (!$id) {;
        $trackingData = R::dispense('cartracking');
    } else {
        $trackingData = R::load('cartracking', $id);
    }

    $trackingData->sysdate = date("Y-m-d H:i:s");
    $trackingData->userid = $_SESSION['userid'];
    $trackingData->chasis  = $chasis;
    if($buydate != "0000-00-00 00:00:00") {
        if($trackingData->buydate != $buydate)
            $trackingData->buyuserid = $_SESSION['userid'];
    }
    $trackingData->buydate  = $buydate;
    $trackingData->buycomment  = $buycomment;
    if($warehousedate != "0000-00-00 00:00:00") {
        if($trackingData->warehousedate != $warehousedate)
            $trackingData->warehouseuserid = $_SESSION['userid'];
    }
    $trackingData->warehousedate  = $warehousedate;
    $trackingData->warehousecomment  = $warehousecomment;
    if($e2date != "0000-00-00 00:00:00") {
        if($trackingData->e2date != $e2date)
            $trackingData->e2userid = $_SESSION['userid'];
    }
    $trackingData->e2date  = $e2date;
    $trackingData->e2comment  = $e2comment;
    if($portdate != "0000-00-00 00:00:00") {
        if($trackingData->portdate != $portdate)
            $trackingData->portuserid = $_SESSION['userid'];
    }
    $trackingData->portdate  = $portdate;
    $trackingData->portcomment  = $portcomment;
    if($arrivaldate != "0000-00-00 00:00:00") {
        if($trackingData->arrivaldate != $arrivaldate)
            $trackingData->arrivaluserid = $_SESSION['userid'];
    }
    $trackingData->arrivaldate  = $arrivaldate;
    $trackingData->arrivalcomment  = $arrivalcomment;
    if($showroomdate != "0000-00-00 00:00:00") {
        if($trackingData->showroomdate != $showroomdate)
            $trackingData->showroomuserid = $_SESSION['userid'];
    }
    $trackingData->showroomdate  = $showroomdate;
    $trackingData->showroomcomment  = $showroomcomment;
    if($deliverdate != "0000-00-00 00:00:00") {
        if($trackingData->deliverdate != $deliverdate)
            $trackingData->deliveruserid = $_SESSION['userid'];
    }
    $trackingData->deliverdate  = $deliverdate;
    $trackingData->delivercomment  = $delivercomment;
    try {
        R::store($trackingData);
        header("location:carTrackingController.php?do=show");
    } catch (Exception $e) {
        header("location:carTrackingController.php");
    }
} elseif ($do == "checkExist") {
    $chasis = filter_input(INPUT_POST, 'chasis');
    $id = filter_input(INPUT_POST, 'id');
    $qs = "";
    if ($id)
        $qs .= " and id != $id";
    $trackingData = R::findOne('cartracking', 'chasis = ? ' . $qs, [$chasis]);
    if ($trackingData) {
        echo $trackingData->id;
    } else {
        echo -1;
    }
}
