<?php
if(!isset($_SESSION)) {  session_start(); }
  ob_start();
if(!isset($_SESSION["user_id"])){header("location:../logout.php");}
  $father = $_SESSION["user_id"];
include "../actions/dbPDO.php";

$item_name      = $_POST['item_name'];
$ask            = $_POST['ask'];
$description    = $_POST['description'];
$kind           = $_POST['kind'];
$part_no        = $_POST['part_no'];
$key_words      = $_POST['key_words'];

$Number = '/^[0-9]+(\.[0-9]{1,2})?$/' ;

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 رقم جوال2 للمدير  أرقام فقط</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 = 0 ;}
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($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()>0){$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 {
  $maxsql= $db->prepare("SELECT max(item_id) as maxd FROM items");
  $maxsql->execute();
  if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
      $maxD = $row['maxd']+1;} else {$maxD = 1 ; }

  $sql = $db->prepare(" INSERT INTO items
  (item_id , item_name, description, kind, ask, part_no , key_words , father)
   VALUES (?, ?, ?, ?, ?, ?,?,?)");
   $sql->bindParam(1,$maxD);
   $sql->bindParam(2,$item_name);
   $sql->bindParam(3,$description);
   $sql->bindParam(4,$kind);
   $sql->bindParam(5,$ask);
   $sql->bindParam(6,$part_no);
   $sql->bindParam(7,$key_words);
   $sql->bindParam(8,$father);
 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>
    <script>document.getElementById('new_item').reset(); </script>
    ";}
  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>
  ";}
}}
?>
