<?php
include("../public/impOpreation.php");
include("../library/uploadImages.php");



//Programsetting
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
$Programsettingdata = $ProgramsettingDAO->load(1);
$smarty->assign("Programsettingdata", $Programsettingdata);

//USERGROUP
$UsergroupDAO = new UsergroupMySqlDAO();
$Usergroupdata = $UsergroupDAO->load($_SESSION['usergroupid']);
$smarty->assign("Usergroupdata", $Usergroupdata);

//lang
$langs = $_SESSION['erp_lang'];

$do = $_GET['do'];
$today = date("Y-m-d H:i:s");
$date = date("Y-m-d");

if (empty($do)) {
  $smarty->display("header.html");

  $allProducts = R::getAll('select productId,productName from product where conditions = 0');
  $smarty->assign('allProducts', $allProducts);

  $smarty->display("carview/add.html");
  $smarty->assign("importcontract", 1);
  $smarty->display("footer.html");
} elseif ($do == "show") {
  include_once("../public/authentication.php");
  $show = filter_input(INPUT_GET, 'show');
  if ($show == 2) {
    $smarty->assign('show', 2);
  }

  $allProducts = R::getAll('select productId,productName from product where conditions = 0');
  $smarty->assign('allProducts', $allProducts);

  $smarty->assign('date', $date);
  $smarty->display("header.html");
  $smarty->display("carview/show.html");
  $smarty->assign("importcontract", 1);
  $smarty->display("footer.html");
} elseif ($do == "edit") {
  include_once("../public/authentication.php");

  $id = filter_input(INPUT_GET, 'id');
  $cardata = R::load('cars', $id);
  $smarty->assign('cardata', $cardata);

  $allProducts = R::getAll('select productId,productName from product where conditions = 0');
  $smarty->assign('allProducts', $allProducts);

  $productid = $cardata->productid;
  $productdata = R::getRow('SELECT * FROM product WHERE productid = ?', [$productid]);
  $smarty->assign("productdata", $productdata);

  $smarty->display("header.html");
  $smarty->display("carview/edit.html");
  $smarty->assign("importcontract", 1);
  $smarty->display("footer.html");
} else if ($do == 'savedata') {
  savedata();
} else if ($do == 'showajax') {
  showajax();
} else if ($do == 'removecontroller') {
  removecontroller();
}


function savedata()
{
  $today = date("Y-m-d H:i:s");
  $userid = $_SESSION['userid'];
  $productid = filter_input(INPUT_POST, 'productid');
  $brand = filter_input(INPUT_POST, 'brand');
  $carcolor = filter_input(INPUT_POST, 'carcolor');
  $carmodel = filter_input(INPUT_POST, 'carmodel');
  $chasisno = filter_input(INPUT_POST, 'chasisno');
  $cartype = filter_input(INPUT_POST, 'cartype');
  $carvalue = filter_input(INPUT_POST, 'carvalue');
  $carnumber = filter_input(INPUT_POST, 'carnumber');
  $carmotor = filter_input(INPUT_POST, 'carmotor');
  $notes = filter_input(INPUT_POST, 'notes');

  $carid = filter_input(INPUT_POST, 'carid');

  if (!$carid) {
    $cars = R::dispense('cars');
    $cars->del = 0;
  } else {
    $cars = R::load('cars', $carid);
  }
  $cars->sysdate = $today;
  $cars->userid = $userid;
  $cars->productid = $productid;
  $cars->brand = $brand;
  $cars->carcolor = $carcolor;
  $cars->carmodel = $carmodel;
  $cars->chasisno = $chasisno;
  $cars->cartype = $cartype;
  $cars->carvalue = $carvalue;
  $cars->carnumber = $carnumber;
  $cars->carmotor = $carmotor;
  $cars->notes = $notes;

  try {
    $carid = R::store($cars);
    R::exec('UPDATE storedetail SET productquantity = 0 WHERE productid = ?', [$productid]);
    echo 1;
  } catch (Exception $e) {
    echo 0;
  }
}
function showajax()
{
  $columns = array('id', 'id', 'productName', 'sysdate', 'id', 'id');
  $start_date = $_POST['start_date'];
  $end_date   = $_POST['end_date'];
  $del        = $_POST['del'];
  $data1      = $_POST['data1'];

  $searchQuery = " ";
  if ($del == '') {
    $searchQuery .= " and cars.del = 0 ";
  }

  if ($data1 != '') {
    $searchQuery .= " and cars.productid = " . $data1 . " ";
  }

  if ($start_date != '') {
    $searchQuery .= 'and  DATE(cars.sysdate)  >= "' . $start_date . '"';
  }
  if ($end_date != '') {
    $searchQuery .= 'and DATE(cars.sysdate) <= "' . $end_date . '" ';
  }

  if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {
    $searchQuery .= 'and ( cars.id LIKE " % ' . $_POST["search"]["value"] . ' %" 
                    OR cars.sysdate LIKE "%' . $_POST["search"]["value"] . ' % "
                    OR product.productName like "%' . $_POST["search"]["value"] . '%"
                    )';
  }

  if (isset($_POST["order"])) {
    $searchQuery .= 'ORDER BY ' . $columns[$_POST['order']['0']['column']] . ' ' . $_POST['order']['0']['dir'] . '  ';
  } else {
    $searchQuery .= "ORDER BY  cars.id DESC ";
  }

  $totals = R::getCell('SELECT count(id) FROM `cars` join product on product.productid = cars.productid WHERE 1 ' . $searchQuery . ' ');

  if (isset($_POST['start']) && $_POST['length'] != '-1') {
    $searchQuery .= "LIMIT " . intval($_POST['start']) . ", " .
      intval($_POST['length']);
  }

  $rResult = R::getAll('SELECT cars.*, product.productName FROM `cars` join product on product.productid = cars.productid WHERE 1 ' . $searchQuery . ' ');
  $apps = count($rResult);

  $output = array(
    "draw" => intval($_POST["draw"]),
    "recordsTotal" => intval($apps),
    "recordsFiltered" => $totals ? $totals : 0,
    "data" => array()
  );
  foreach ($rResult as $row) {
    $date = date('Y-m-d');
    $sub_array = array();
    $sub_array[] = $row["id"];
    $sub_array[] = $row['productName'];

    $sub_array[] = $row["sysdate"];
    if ($row["del"] == 0) {
      $sub_array[] = '<a href="carController.php?do=edit&id=' . $row["id"] . '" type="button" class="btn btn-default btn-lg editicon"></a>';
      $sub_array[] = '<a href="javascript:;" data-id="' . $row["id"] . '" data-controll="carController" type="button" class="btn btn-default btn-lg deleteicon removecontroller"  ></a>';
    } else {
      $sub_array[] = '<a href="carController.php?do=edit&id=' . $row["id"] . '" type="button" class="btn btn-default btn-lm ">تفاصيل</a>';
      $sub_array[] = 'محذوف ';
    }

    $output['data'][] = $sub_array;
  }

  echo json_encode($output, JSON_UNESCAPED_SLASHES);
}


function removecontroller()
{
  $today = date("Y-m-d H:i:s");
  $userid = $_SESSION['userid'];
  $id = filter_input(INPUT_POST, 'id');
  $tables = R::load('cars', $id);
  $tables->del = 1;
  $tables->sysdate = $today;
  $tables->userid = $userid;
  try {
    R::store($tables);
    echo 1;
  } catch (Exception $e) {
    echo 0;
  }
}
