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

if (empty($do)) {  
    $smarty->display("header.html");
    $smarty->display("vehicletypeview/add.html");
    $smarty->display("footer.html");
} elseif ($do == "show") {
    $vehicletypes = R::findAll('vehicletypes','del < 2');
    $smarty->assign('vehicletypes', $vehicletypes);
    $smarty->display("header.html");
    $smarty->display("vehicletypeview/show.html");
    $smarty->display("footer.html");
} elseif ($do == "edit") {
    $id = filter_input(INPUT_GET, 'id');
    $vehicletype = R::load('vehicletypes', $id);
    $smarty->assign('vehicletype', $vehicletype);
    $smarty->display("header.html");
    $smarty->display("vehicletypeview/edit.html");
    $smarty->display("footer.html");
}else if ($do == 'savedata') {
    $typename = filter_input(INPUT_POST, 'typename');
    $typeid = filter_input(INPUT_POST, 'typeid');
    if (!$typeid) {;
        $vehicletypes = R::dispense('vehicletypes');
        $vehicletypes->del = 0;
        $vehicletypes->addtoday = date("Y-m-d H:i:s");        
        $vehicletypes->adduserid = $_SESSION['userid'];
        $vehicletypes->deltoday = '';      
        $vehicletypes->deluserid = '';
    }else{
        $vehicletypes = R::load('vehicletypes',$typeid);
        $vehicletypes->del = 1;
        $vehicletypes->updatetoday = date("Y-m-d H:i:s");         
        $vehicletypes->updateuserid = $_SESSION['userid'];
    }
         $vehicletypes->typename  = $typename;
    try {
         R::store($vehicletypes);
        header("location:vehicletype.php?do=show");
    } catch (Exception $e) {
        header("location:vehicletype.php");
    }
}else if ($do == 'delete') {
    $id = filter_input(INPUT_GET, 'id');
    $tables = R::load('vehicletypes',$id);
    $tables->del = 2;
    $tables->deltoday = date("Y-m-d H:i:s");
    $tables->deluserid = $_SESSION['userid'];
    R::store($tables);
    header("location:vehicletype.php?do=show");
}else if ($do == 'vehicleTypes') {

    $row_array = array();
    $return_arr = array();
    $name = $_POST['searchTerm'];
    $productsData = R::getAll("SELECT id, typename as name
    FROM vehicleTypes 
    WHERE del < 2 and typename 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);
}







?>