<?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");
    $reviewersData = R::load('carreviewer',1);
    $reviewersData['reviewer1'] = explode(',', $reviewersData['reviewer1']);
    $reviewersData['reviewer2'] = explode(',', $reviewersData['reviewer2']);
    $smarty->assign("reviewersData", $reviewersData);
    $smarty->assign("userid", $_SESSION['userid']);

    if($_SESSION['userid'] != -1 && !in_array($_SESSION['userid'], $reviewersData['reviewer1']) && !in_array($_SESSION['userid'], $reviewersData['reviewer2'])){
        header("location:index.php");
    }

    /////////////////////////////Save////////////////////////////////
    $todaySaveDaily = R::getAll("SELECT DISTINCT savedaily.savedailyid,  savedaily.* FROM `savedaily` 
                    LEFT JOIN carreview ON carreview.tablename = 'savedailyController.php' and modelid = savedailyid 
                    WHERE reviewuserid1 IS NULL OR reviewuserid2 IS NULL OR reviewuserid1 = 0 OR reviewuserid2 = 0 order by savedailyid");
    $saveNotReviewed1 = 0;
    $saveNotReviewed2 = 0;
    $saveReviewed1 = [];
    $saveReviewed2 = [];
    foreach ($todaySaveDaily as $value) {
       $isExist = R::getAll("SELECT * from carreview where tablename = 'savedailyController.php' and modelid = ?", [$value['savedailyid']]);
         if(count($isExist) == 0){
              $saveNotReviewed1++;
              $saveNotReviewed2++;
         }
         else{
            if(count($isExist) == 2){
                array_push($saveReviewed1, $value['savedailyid']);
                array_push($saveReviewed2, $value['savedailyid']);
                continue;
            }
            else{
                if($isExist[0]['reviewuserid1'] > 0){
                    array_push($saveReviewed1, $value['savedailyid']);
                    $saveNotReviewed2++;
                }
                else{
                    array_push($saveReviewed2, $value['savedailyid']);
                    $saveNotReviewed1++;
                }
            }
         }
    }
    $smarty->assign("todaySaveDaily", $todaySaveDaily);
    $smarty->assign("saveReviewed1", $saveReviewed1);
    $smarty->assign("saveReviewed2", $saveReviewed2);
    $smarty->assign("saveNotReviewed1", $saveNotReviewed1);
    $smarty->assign("saveNotReviewed2", $saveNotReviewed2);

    /////////////////////////////SellBills////////////////////////////////
    $billsNotReviewed1 = 0;
    $billsNotReviewed2 = 0;
    $laterNakdiBills = R::getAll("SELECT DISTINCT sellbill.sellbillid,  sellbill.* FROM `sellbill`
                    LEFT JOIN carreview ON carreview.tablename = 'sellbillController.php' and modelid = sellbillid 
                    WHERE conditions = 0 AND (reviewuserid1 IS NULL OR reviewuserid2 IS NULL OR reviewuserid1 = 0 OR reviewuserid2 = 0) order by sellbillid");
    $billsReviewed1 = [];
    $billsReviewed2 = [];
    foreach ($laterNakdiBills as $value) {
            $isExist = R::getAll("SELECT * from carreview where tablename = 'sellbillController.php' and modelid = ?", [$value['sellbillid']]);
             if(count($isExist) == 0){
                  $billsNotReviewed1++;
                  $billsNotReviewed2++;
             }
             else{
                if(count($isExist) == 2){
                    array_push($billsReviewed1, $value['sellbillid']);
                    array_push($billsReviewed2, $value['sellbillid']);
                    continue;
                }
                else{
                    if($isExist[0]['reviewuserid1'] > 0){
                        array_push($billsReviewed1, $value['sellbillid']);
                        $billsNotReviewed2++;
                    }
                    else{
                        array_push($billsReviewed2, $value['sellbillid']);
                        $billsNotReviewed1++;
                    }
                }
             }
    }
    $smarty->assign("laterNakdiBills", $laterNakdiBills);
    $smarty->assign("billsReviewed1", $billsReviewed1);
    $smarty->assign("billsReviewed2", $billsReviewed2);
    $smarty->assign("billsNotReviewed1", $billsNotReviewed1);
    $smarty->assign("billsNotReviewed2", $billsNotReviewed2);

        /////////////////////////////BuyBills////////////////////////////////
    $buyBillsNotReviewed1 = 0;
    $buyBillsNotReviewed2 = 0;
    $buyBills = R::getAll("SELECT DISTINCT buybill.buybillid,  buybill.* FROM `buybill`
                    LEFT JOIN carreview ON carreview.tablename = 'buyBillController.php' and modelid = buybillid 
                    WHERE conditions = 0 AND (reviewuserid1 IS NULL OR reviewuserid2 IS NULL OR reviewuserid1 = 0 OR reviewuserid2 = 0) order by buybillid");
    $buyBillsReviewed1 = [];
    $buyBillsReviewed2 = [];
    foreach ($buyBills as $value) {
            $isExist = R::getAll("SELECT * from carreview where tablename = 'buyBillController.php' and modelid = ?", [$value['buybillid']]);
             if(count($isExist) == 0){
                  $buyBillsNotReviewed1++;
                  $buyBillsNotReviewed2++;
             }
             else{
                if(count($isExist) == 2){
                    array_push($buyBillsReviewed1, $value['buybillid']);
                    array_push($buyBillsReviewed2, $value['buybillid']);
                    continue;
                }
                else{
                    if($isExist[0]['reviewuserid1'] > 0){
                        array_push($buyBillsReviewed1, $value['buybillid']);
                        $buyBillsNotReviewed2++;
                    }
                    else{
                        array_push($buyBillsReviewed2, $value['buybillid']);
                        $buyBillsNotReviewed1++;
                    }
                }
             }
    }
    $smarty->assign("buyBills", $buyBills);
    $smarty->assign("buyBillsReviewed1", $buyBillsReviewed1);
    $smarty->assign("buyBillsReviewed2", $buyBillsReviewed2);
    $smarty->assign("buyBillsNotReviewed1", $buyBillsNotReviewed1);
    $smarty->assign("buyBillsNotReviewed2", $buyBillsNotReviewed2);

    /////////////////////////////Contracts////////////////////////////////
    $contracts = R::getAll("SELECT DISTINCT importcontract.id,  importcontract.* FROM `importcontract` 
                    LEFT JOIN carreview ON carreview.tablename = 'importcontract.php' and modelid = importcontract.id 
                    WHERE importcontract.del < 2 AND (reviewuserid1 IS NULL OR reviewuserid2 IS NULL OR reviewuserid1 = 0 OR reviewuserid2 = 0) order by importcontract.id");
    $contractsNotReviewed1 = 0;
    $contractsNotReviewed2 = 0;
    $contractsReviewed1 = [];
    $contractsReviewed2 = [];
    foreach ($contracts as $value) {
       $isExist = R::getAll("SELECT * from carreview where tablename = 'importContractController.php' and modelid = ?", [$value['id']]);
         if(count($isExist) == 0){
              $contractsNotReviewed1++;
              $contractsNotReviewed2++;
         }
         else{
            if(count($isExist) == 2){
                array_push($contractsReviewed1, $value['id']);
                array_push($contractsReviewed2, $value['id']);
                continue;
            }
            else{
                if($isExist[0]['reviewuserid1'] > 0){
                    array_push($contractsReviewed1, $value['id']);
                    $contractsNotReviewed2++;
                }
                else{
                    array_push($contractsReviewed2, $value['id']);
                    $contractsNotReviewed1++;
                }
            }
         }
    }
    $smarty->assign("contracts", $contracts);
    $smarty->assign("contractsReviewed1", $contractsReviewed1);
    $smarty->assign("contractsReviewed2", $contractsReviewed2);
    $smarty->assign("contractsNotReviewed1", $contractsNotReviewed1);
    $smarty->assign("contractsNotReviewed2", $contractsNotReviewed2);
    
    /////////////////////////////Files////////////////////////////////
    $files = R::getAll("SELECT DISTINCT carfile.id,  carfile.* FROM `carfile` 
                    LEFT JOIN carreview ON carreview.tablename = 'fileController.php' and modelid = carfile.id 
                    WHERE carfile.del < 2 AND (reviewuserid1 IS NULL OR reviewuserid2 IS NULL OR reviewuserid1 = 0 OR reviewuserid2 = 0) order by carfile.id");
    $filesNotReviewed1 = 0;
    $filesNotReviewed2 = 0;
    $filesReviewed1 = [];
    $filesReviewed2 = [];
    foreach ($files as $value) {
       $isExist = R::getAll("SELECT * from carreview where tablename = 'fileController.php' and modelid = ?", [$value['id']]);
         if(count($isExist) == 0){
              $filesNotReviewed1++;
              $filesNotReviewed2++;
         }
         else{
            if(count($isExist) == 2){
                array_push($filesReviewed1, $value['id']);
                array_push($filesReviewed2, $value['id']);
                continue;
            }
            else{
                if($isExist[0]['reviewuserid1'] > 0){
                    array_push($filesReviewed1, $value['id']);
                    $filesNotReviewed2++;
                }
                else{
                    array_push($filesReviewed2, $value['id']);
                    $filesNotReviewed1++;
                }
            }
         }
    }
    $smarty->assign("files", $files);
    $smarty->assign("filesReviewed1", $filesReviewed1);
    $smarty->assign("filesReviewed2", $filesReviewed2);
    $smarty->assign("filesNotReviewed1", $filesNotReviewed1);
    $smarty->assign("filesNotReviewed2", $filesNotReviewed2);

    /////////////////////////////ShippingFiles////////////////////////////////
    $shipping = R::getAll("SELECT DISTINCT shippingfile.id,  shippingfile.* FROM `shippingfile`
                    LEFT JOIN carreview ON carreview.tablename = 'shippingFilesController.php' and modelid = shippingfile.id 
                    WHERE reviewuserid1 IS NULL OR reviewuserid2 IS NULL OR reviewuserid1 = 0 OR reviewuserid2 = 0 order by shippingfile.id");
    $shippingNotReviewed1 = 0;
    $shippingNotReviewed2 = 0;
    $shippingReviewed1 = [];
    $shippingReviewed2 = [];
    foreach ($shipping as $value) {
       $isExist = R::getAll("SELECT * from carreview where tablename = 'shippingFilesController.php' and modelid = ?", [$value['id']]);
         if(count($isExist) == 0){
              $shippingNotReviewed1++;
              $shippingNotReviewed2++;
         }
         else{
            if(count($isExist) == 2){
                array_push($shippingReviewed1, $value['id']);
                array_push($shippingReviewed2, $value['id']);
                continue;
            }
            else{
                if($isExist[0]['reviewuserid1'] > 0){
                    array_push($shippingReviewed1, $value['id']);
                    $shippingNotReviewed2++;
                }
                else{
                    array_push($shippingReviewed2, $value['id']);
                    $shippingNotReviewed1++;
                }
            }
         }
    }
    $smarty->assign("shipping", $shipping);
    $smarty->assign("shippingReviewed1", $shippingReviewed1);
    $smarty->assign("shippingReviewed2", $shippingReviewed2);
    $smarty->assign("shippingNotReviewed1", $shippingNotReviewed1);
    $smarty->assign("shippingNotReviewed2", $shippingNotReviewed2);
    
    /////////////////////////////Car Tracking////////////////////////////////
    $tracking = R::getAll("SELECT DISTINCT cartracking.id, cartracking.* FROM `cartracking`
                    LEFT JOIN carreview ON carreview.tablename = 'carTrackingController.php' and modelid = cartracking.id 
                    WHERE reviewuserid1 IS NULL OR reviewuserid2 IS NULL OR reviewuserid1 = 0 OR reviewuserid2 = 0 order by cartracking.id");
    $trackingNotReviewed1 = 0;
    $trackingNotReviewed2 = 0;
    $trackingReviewed1 = [];
    $trackingReviewed2 = [];
    foreach ($tracking as $value) {
       $isExist = R::getAll("SELECT * from carreview where tablename = 'carTrackingController.php' and modelid = ?", [$value['id']]);
         if(count($isExist) == 0){
              $trackingNotReviewed1++;
              $trackingNotReviewed2++;
         }
         else{
            if(count($isExist) == 2){
                array_push($trackingReviewed1, $value['id']);
                array_push($trackingReviewed2, $value['id']);
                continue;
            }
            else{
                if($isExist[0]['reviewuserid1'] > 0){
                    array_push($trackingReviewed1, $value['id']);
                    $trackingNotReviewed2++;
                }
                else{
                    array_push($trackingReviewed2, $value['id']);
                    $trackingNotReviewed1++;
                }
            }
         }
    }
    $smarty->assign("tracking", $tracking);
    $smarty->assign("trackingReviewed1", $trackingReviewed1);
    $smarty->assign("trackingReviewed2", $trackingReviewed2);
    $smarty->assign("trackingNotReviewed1", $trackingNotReviewed1);
    $smarty->assign("trackingNotReviewed2", $trackingNotReviewed2);

    /////////////////////////////Dollar Earnings////////////////////////////////
    $dollar = R::getAll("SELECT DISTINCT dollarearnings.id, dollarearnings.* FROM `dollarearnings`
                    LEFT JOIN carreview ON carreview.tablename = 'dollarEarningsController.php' and modelid = dollarearnings.id 
                    WHERE conditions = 0 AND (reviewuserid1 IS NULL OR reviewuserid2 IS NULL OR reviewuserid1 = 0 OR reviewuserid2 = 0) order by dollarearnings.id");
    $dollarNotReviewed1 = 0;
    $dollarNotReviewed2 = 0;
    $dollarReviewed1 = [];
    $dollarReviewed2 = [];
    foreach ($dollar as $value) {
       $isExist = R::getAll("SELECT * from carreview where tablename = 'dollarEarningsController.php' and modelid = ?", [$value['id']]);
         if(count($isExist) == 0){
              $dollarNotReviewed1++;
              $dollarNotReviewed2++;
         }
         else{
            if(count($isExist) == 2){
                array_push($dollarReviewed1, $value['id']);
                array_push($dollarReviewed2, $value['id']);
                continue;
            }
            else{
                if($isExist[0]['reviewuserid1'] > 0){
                    array_push($dollarReviewed1, $value['id']);
                    $dollarNotReviewed2++;
                }
                else{
                    array_push($dollarReviewed2, $value['id']);
                    $dollarNotReviewed1++;
                }
            }
         }
    }
    $smarty->assign("dollar", $dollar);
    $smarty->assign("dollarReviewed1", $dollarReviewed1);
    $smarty->assign("dollarReviewed2", $dollarReviewed2);
    $smarty->assign("dollarNotReviewed1", $dollarNotReviewed1);
    $smarty->assign("dollarNotReviewed2", $dollarNotReviewed2);



    $smarty->display("carDataReviewsview/show.html");
    $smarty->display("footer.html");
}else if ($do == 'savedata') {

   
    $saveDailyIds1 = filter_input(INPUT_POST, 'saveDailyIds1');
    $saveDailyIds2 = filter_input(INPUT_POST, 'saveDailyIds2');
    $billIds1 = filter_input(INPUT_POST, 'billIds1');
    $billIds2 = filter_input(INPUT_POST, 'billIds2');
    $buyBillIds1 = filter_input(INPUT_POST, 'buyBillIds1');
    $buyBillIds2 = filter_input(INPUT_POST, 'buyBillIds2');
    $contractIds1 = filter_input(INPUT_POST, 'contractIds1');
    $contractIds2 = filter_input(INPUT_POST, 'contractIds2');
    $fileIds1 = filter_input(INPUT_POST, 'fileIds1');
    $fileIds2 = filter_input(INPUT_POST, 'fileIds2');
    $shippingIds1 = filter_input(INPUT_POST, 'shippingIds1');
    $shippingIds2 = filter_input(INPUT_POST, 'shippingIds2');
    $trackingIds1 = filter_input(INPUT_POST, 'trackingIds1');
    $trackingIds2 = filter_input(INPUT_POST, 'trackingIds2');
    $dollarIds1 = filter_input(INPUT_POST, 'dollarIds1');
    $dollarIds2 = filter_input(INPUT_POST, 'dollarIds2');

    $review->sysdate = date("Y-m-d H:i:s");        
    $review->userid = $_SESSION['userid'];
        
    $reviewersData = R::load('carreviewer',1);
    try {
        if(in_array($_SESSION['userid'], explode(',', $reviewersData['reviewer1'])))
        {
            if (isset($saveDailyIds1)) {
                foreach (explode(",", $saveDailyIds1) as $value) {
                    if (!$value) continue;
                    $review = R::dispense('carreview');
                    $review->tablename = 'savedailyController.php';
                    $review->modelid = $value;
                    $review->reviewuserid1 = $_SESSION['userid'];
                    $review->sysdate1 = date("Y-m-d H:i:s");
                    R::store($review);
                }
            }
            if (isset($billIds1)) {
                foreach (explode(",", $billIds1) as $value) {
                    if (!$value) continue;
                    $review = R::dispense('carreview');
                    $review->tablename = 'sellbillController.php';
                    $review->modelid = $value;
                    $review->reviewuserid1 = $_SESSION['userid'];
                    $review->sysdate1 = date("Y-m-d H:i:s");
                    R::store($review);
                }
            }
            if (isset($buyBillIds1)) {
                foreach (explode(",", $buyBillIds1) as $value) {
                    if (!$value) continue;
                    $review = R::dispense('carreview');
                    $review->tablename = 'buyBillController.php';
                    $review->modelid = $value;
                    $review->reviewuserid1 = $_SESSION['userid'];
                    $review->sysdate1 = date("Y-m-d H:i:s");
                    R::store($review);
                }
            }
            if (isset($contractIds1)) {
                foreach (explode(",", $contractIds1) as $value) {
                    if (!$value) continue;
                    $review = R::dispense('carreview');
                    $review->tablename = 'importContractController.php';
                    $review->modelid = $value;
                    $review->reviewuserid1 = $_SESSION['userid'];
                    $review->sysdate1 = date("Y-m-d H:i:s");
                    R::store($review);
                }
            }
            if (isset($fileIds1)) {
                foreach (explode(",", $fileIds1) as $value) {
                    if (!$value) continue;
                    $review = R::dispense('carreview');
                    $review->tablename = 'fileController.php';
                    $review->modelid = $value;
                    $review->reviewuserid1 = $_SESSION['userid'];
                    $review->sysdate1 = date("Y-m-d H:i:s");
                    R::store($review);
                }
            }
            if (isset($shippingIds1)) {
                foreach (explode(",", $shippingIds1) as $value) {
                    if (!$value) continue;
                    $review = R::dispense('carreview');
                    $review->tablename = 'shippingFilesController.php';
                    $review->modelid = $value;
                    $review->reviewuserid1 = $_SESSION['userid'];
                    $review->sysdate1 = date("Y-m-d H:i:s");
                    R::store($review);
                }
            }
            if (isset($trackingIds1)) {
                foreach (explode(",", $trackingIds1) as $value) {
                    if (!$value) continue;
                    $review = R::dispense('carreview');
                    $review->tablename = 'carTrackingController.php';
                    $review->modelid = $value;
                    $review->reviewuserid1 = $_SESSION['userid'];
                    $review->sysdate1 = date("Y-m-d H:i:s");
                    R::store($review);
                }
            }
            if (isset($dollarIds1)) {
                foreach (explode(",", $dollarIds1) as $value) {
                    if (!$value) continue;
                    $review = R::dispense('carreview');
                    $review->tablename = 'dollarEarningsController.php';
                    $review->modelid = $value;
                    $review->reviewuserid1 = $_SESSION['userid'];
                    $review->sysdate1 = date("Y-m-d H:i:s");
                    R::store($review);
                }
            }
        }
        if(in_array($_SESSION['userid'], explode(',', $reviewersData['reviewer2'])))
        {
            if (isset($saveDailyIds2)) {
                foreach (explode(",", $saveDailyIds2) as $value) {
                    if (!$value) continue;
                    $review = R::dispense('carreview');
                    $review->tablename = 'savedailyController.php';
                    $review->modelid = $value;
                    $review->reviewuserid2 = $_SESSION['userid'];
                    $review->sysdate2 = date("Y-m-d H:i:s");
                    R::store($review);
                }
            }
            if (isset($billIds2)) {
                foreach (explode(",", $billIds2) as $value) {
                    if (!$value) continue;
                    $review = R::dispense('carreview');
                    $review->tablename = 'sellbillController.php';
                    $review->modelid = $value;
                    $review->reviewuserid2 = $_SESSION['userid'];
                    $review->sysdate2 = date("Y-m-d H:i:s");
                    R::store($review);
                }
            }
            if (isset($buyBillIds2)) {
                foreach (explode(",", $buyBillIds2) as $value) {
                    if (!$value) continue;
                    $review = R::dispense('carreview');
                    $review->tablename = 'buyBillController.php';
                    $review->modelid = $value;
                    $review->reviewuserid2 = $_SESSION['userid'];
                    $review->sysdate2 = date("Y-m-d H:i:s");
                    R::store($review);
                }
            }
            if (isset($contractIds2)) {
                foreach (explode(",", $contractIds2) as $value) {
                    if (!$value) continue;
                    $review = R::dispense('carreview');
                    $review->tablename = 'importContractController.php';
                    $review->modelid = $value;
                    $review->reviewuserid2 = $_SESSION['userid'];
                    $review->sysdate2 = date("Y-m-d H:i:s");
                    R::store($review);
                }
            }
            if (isset($fileIds2)) {
                foreach (explode(",", $fileIds2) as $value) {
                    if (!$value) continue;
                    $review = R::dispense('carreview');
                    $review->tablename = 'fileController.php';
                    $review->modelid = $value;
                    $review->reviewuserid2 = $_SESSION['userid'];
                    $review->sysdate2 = date("Y-m-d H:i:s");
                    R::store($review);
                }
            }
            if (isset($shippingIds2)) {
                foreach (explode(",", $shippingIds2) as $value) {
                    if (!$value) continue;
                    $review = R::dispense('carreview');
                    $review->tablename = 'shippingFilesController.php';
                    $review->modelid = $value;
                    $review->reviewuserid2 = $_SESSION['userid'];
                    $review->sysdate2 = date("Y-m-d H:i:s");
                    R::store($review);
                }
            }
            if (isset($trackingIds2)) {
                foreach (explode(",", $trackingIds2) as $value) {
                    if (!$value) continue;
                    $review = R::dispense('carreview');
                    $review->tablename = 'carTrackingController.php';
                    $review->modelid = $value;
                    $review->reviewuserid2 = $_SESSION['userid'];
                    $review->sysdate2 = date("Y-m-d H:i:s");
                    R::store($review);
                }
            }
            if (isset($dollarIds2)) {
                foreach (explode(",", $dollarIds2) as $value) {
                    if (!$value) continue;
                    $review = R::dispense('carreview');
                    $review->tablename = 'dollarEarningsController.php';
                    $review->modelid = $value;
                    $review->reviewuserid2 = $_SESSION['userid'];
                    $review->sysdate2 = date("Y-m-d H:i:s");
                    R::store($review);
                }
            }
        }
        header("location:carReviewController.php");
    } catch (Exception $e) {
        header("location:carReviewController.php");
    }
}



