<?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("savetypeview/add.html");
    $smarty->display("footer.html");
} elseif ($do == "show") {
    $savetype = R::findAll('savetype','del < 2');
    $smarty->assign('allSaveType', $savetype);
    $smarty->display("header.html");
    $smarty->display("savetypeview/show.html");
    $smarty->display("footer.html");
} elseif ($do == "edit") {
    $id = filter_input(INPUT_GET, 'id');
    $savetype = R::load('savetype', $id);
    $smarty->assign('savetype', $savetype);
    $smarty->display("header.html");
    $smarty->display("savetypeview/edit.html");
    $smarty->display("footer.html");
}else if ($do == 'savedata') {
    $name = filter_input(INPUT_POST, 'name');
    $id = filter_input(INPUT_POST, 'id');
    if (!$id) {;
        $savetype = R::dispense('savetype');
        $savetype->del = 0;
        $savetype->adddate = date("Y-m-d H:i:s");        
        $savetype->adduserid = $_SESSION['userid'];
        $savetype->deldate = '';      
        $savetype->deluserid = '';
    }else{
        $savetype = R::load('savetype',$id);
        $savetype->del = 1;
        $savetype->updatedate = date("Y-m-d H:i:s");         
        $savetype->updateuserid = $_SESSION['userid'];
    }
         $savetype->name  = $name;
    try {
         R::store($savetype);
        header("location:saveTypeController.php?do=show");
    } catch (Exception $e) {
        header("location:saveTypeController.php");
    }
}else if ($do == 'delete') {
    $id = filter_input(INPUT_GET, 'id');
    $tables = R::load('savetype',$id);
    $tables->del = 2;
    $tables->deldate = date("Y-m-d H:i:s");
    $tables->deluserid = $_SESSION['userid'];
    R::store($tables);
    header("location:saveTypeController.php?do=show");
}else if ($do == 'savetype') {

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

    echo json_encode($return_arr);
}



