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

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



