<?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");
    
    $allTags = R::getAll('select * from associatedtags where conditions = 0');
    $smarty->assign('allTags', $allTags);

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

    $smarty->display("carfilesview/add.html");
  $smarty->assign("importcontract", 1);
    $smarty->display("footer.html");
} elseif ($do == "show") {
    $qs = '';
    if (isset($_POST['chasis']) && $_POST['chasis'] != '') {
        $qs .= " and chasis = '" . $_POST['chasis'] . "'";
    }
    $files = R::findAll('carfile','del < 2 '.$qs);
    $smarty->assign('files', $files);
    
    $allChasis = R::getCol('select chasisno from importcontract where chasisno != "" and del <2');
    $smarty->assign('allChasis', $allChasis);

    $smarty->display("header.html");
    $smarty->display("carfilesview/show.html");
  $smarty->assign("importcontract", 1);
    $smarty->display("footer.html");
} elseif ($do == "edit") {
    $id = filter_input(INPUT_GET, 'id');
    $files = R::load('carfile', $id);
    $files->tagids = explode(',', $files->tagids);
    $smarty->assign('files', $files);

    $allTags = R::getAll('select * from associatedtags where conditions = 0');
    $smarty->assign('allTags', $allTags);

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

    $smarty->display("header.html");
    $smarty->display("carfilesview/edit.html");
  $smarty->assign("importcontract", 1);
    $smarty->display("footer.html");
}else if ($do == 'savedata') {
    $name = filter_input(INPUT_POST, 'name');
    $id = filter_input(INPUT_POST, 'id');
    $tagids = filter_input(INPUT_POST, 'tagids', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
    $chasis = filter_input(INPUT_POST, 'chasis');

    if (!$id) {
        $handle = new upload($_FILES['image']);
        $image = uploadImageswhioutresize($handle, '../upload/carFile');

        $files = R::dispense('carfile');
        $files->del = 0;
        $files->addtoday = date("Y-m-d H:i:s");        
        $files->adduserid = $_SESSION['userid'];
        $files->deltoday = '';      
        $files->deluserid = '';
    }else{
        $handle = new upload($_FILES['image']);
        $image = updateImagesWithoutresiz($handle, "oldfile", '../upload/carFile');

        $files = R::load('carfile',$id);
        $files->del = 1;
        $files->updatetoday = date("Y-m-d H:i:s");         
        $files->updateuserid = $_SESSION['userid'];
    }
         $files->name  = $name;
         $files->image  = $image;
        $files->chasis = $chasis;
        
        if (isset($tagids)) {
            if (in_array("0", $tagids)) {
                $files->tagids = '0';
            } else {
                $files->tagids = implode(",", $tagids);
            }
        }
    try {
         R::store($files);
        header("location:fileController.php?do=show");
    } catch (Exception $e) {
        header("location:fileController.php");
    }
}else if ($do == 'delete') {
    $id = filter_input(INPUT_GET, 'id');
    $tables = R::load('carfile',$id);
    $tables->del = 2;
    $tables->deltoday = date("Y-m-d H:i:s");
    $tables->deluserid = $_SESSION['userid'];
    R::store($tables);
    header("location:fileController.php?do=show");
}else if ($do == 'files') {

    $row_array = array();
    $return_arr = array();
    $name = $_POST['searchTerm'];
    $productsData = R::getAll("SELECT id, name as name
    FROM carfile 
    WHERE del < 2 and name LIKE '%" . $name . "%'  limit 50");
    foreach ($productsData as $pro) {
        $row_array['id'] = $pro['id'];
        $row_array['text'] = $pro['name'];
        array_push($return_arr, $row_array);
    }

    echo json_encode($return_arr);
}



