<?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");

    $allUsers = R::getAll('select userid,employeename from user where conditions = 0 and userid > 0');
    $smarty->assign('allUsers', $allUsers);

    $reviewersData = R::load('carreviewer',1);
    $reviewersData->reviewer1 = explode(',', $reviewersData->reviewer1);
    $reviewersData->reviewer2 = explode(',', $reviewersData->reviewer2);
    $smarty->assign('reviewersData', $reviewersData);

    $smarty->display("carreviewerview/add.html");
    $smarty->display("footer.html");
}else if ($do == 'savedata') {
    $reviewer1 = filter_input(INPUT_POST, 'reviewer1', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
    $reviewer2 = filter_input(INPUT_POST, 'reviewer2', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
    $id = filter_input(INPUT_POST, 'id');

    if (!$id) {
        $reviewers = R::dispense('carreviewer');
    }else{
        $reviewers = R::load('carreviewer',$id);
    }
    $reviewers->sysdate = date("Y-m-d H:i:s");        
    $reviewers->userid = $_SESSION['userid'];
        
        // if (isset($reviewer1)) {
            if (in_array("0", $reviewer1)) {
                $reviewers->reviewer1 = '0';
            } else {
                $reviewers->reviewer1 = implode(",", $reviewer1);
            }
        // }
        // if (isset($reviewer2)) {
            if (in_array("0", $reviewer2)) {
                $reviewers->reviewer2 = '0';
            } else {
                $reviewers->reviewer2 = implode(",", $reviewer2);
            }
        // }

    try {
         R::store($reviewers);
        header("location:carReviewerController.php");
    } catch (Exception $e) {
        header("location:carReviewerController.php");
    }
}



