<?php
if(!isset($_SESSION)) {  session_start(); }
ob_start();
if(!isset($_SESSION["user_id"])){header("location:../logout.php");}
include "../actions/dbPDO.php";
$item_id        = $_POST['item_id'];
$item_name      = $_POST['item_name'];
$description    = $_POST['description'];
$kind           = $_POST['kind'];
$ask            = $_POST['ask'];
$part_no        = $_POST['part_no'];
$next_km        = $_POST['next_km'];
$key_words      = $_POST['key_words'];

$Number ='/^[0-9]+(\.[0-9]{1,2})?$/' ;
$date_update = date("Y-m-d");

if(empty($item_name))
{
  echo "
        <div class='alert alert-warning'>
        <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;' >&times;</a>
        <b>من فضلك سجل اسم الصنف على الاقل ليتم حفظ  ...</b>
        </div>
  ";
  exit();
}else{
  if(!empty($item_name)){  $item_name = htmlspecialchars(strip_tags($item_name)) ;}
  if(!empty($ask)){
    if(!preg_match($Number,$ask)){
      echo "
      <div class='alert alert-warning'>
      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
      <b> تأكد من الرقم !! $ask حد الطلب أرقام فقط</b>
      </div>
    ";
      exit();
    }
    if((strlen($ask) < 0)){
      echo "
      <div class='alert alert-warning'>
      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
      <b>انتبه حد الطلب أقل من الصفر ..<b>
      </div>
      ";
      exit();
    }}else{$ask = null;}
    if(!empty($description)){  $description = htmlspecialchars(strip_tags($description)) ;}else{$description = null;}
    if(!empty($kind)){  $kind = htmlspecialchars(strip_tags($kind)) ;}else{$kind = null;}
    if(!empty($part_no)){  $part_no = htmlspecialchars(strip_tags($part_no)) ;}else{$part_no = null;}
    if(!empty($next_km)){  $next_km = htmlspecialchars(strip_tags($next_km)) ;}else{$next_km = null;}
    if(!empty($key_words)){  $key_words = htmlspecialchars(strip_tags($key_words)) ;}else{$key_words = null;}
// existing item_name in your database
$sql =$db->prepare("SELECT item_id , item_name , description , ask FROM items  WHERE item_name = ? LIMIT 1");
$sql->bindParam(1,$item_name);
$sql->execute();
  if($sql->rowCount()>1){$row = $sql->fetch(PDO::FETCH_ASSOC);
      $item_id      = $row["item_id"];
      $item_name    = $row["item_name"];
      $description  = $row["description"];
      $ask          = $row["ask"];
  echo "
  <div class='alert alert-danger'>
  <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
  <b>تم تسجيل بيانات $item_name  من قبل كود رقم $item_id بموصفات  $description بحد طلب = $ask</b>
  </div>
  ";
  exit();
}else {
  $sql = $db->prepare(" UPDATE items SET item_name = ?, description = ?, kind = ?,
   ask = ?, part_no = ? , next_km = ?, key_words = ? , date_update = ?
     WHERE item_id = ?");
   $sql->bindParam(1,$item_name);
   $sql->bindParam(2,$description);
   $sql->bindParam(3,$kind);
   $sql->bindParam(4,$ask);
   $sql->bindParam(5,$part_no);
   $sql->bindParam(6,$next_km);
   $sql->bindParam(7,$key_words);
   $sql->bindParam(8,$date_update);
   $sql->bindParam(9,$item_id);
   if($sql->execute()){
echo "
    <div class='alert alert-success'>
    <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
    <b>تم تحديث بيانات الصنف بنجاح</b>
    </div>
    ";}
  else{ echo "<div class='alert alert-danger'>
  <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
    <b>يوجد خطأ في تسجيل البيانات ... راجع  من فضلك</b>
  </div>
  ";}
}
}
?>
