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

$store_name   = $_POST['store_name'];
$spaceM2      =$_POST['spaceM2'];
$area         =$_POST['area'];

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

if(empty($store_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($store_name)){  $store_name = htmlspecialchars(strip_tags($store_name)) ;}
if(!empty($spaceM2)){
    if(!preg_match($Number,$spaceM2)){
      echo "
      <div class='alert alert-warning'>
      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
      <b> تأكد من الرقم !! $spaceM2 المساحة بالمتر المربع ارقام فقط</b>
      </div>";
      exit();
    }
    if((strlen($spaceM2) < 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{$spaceM2 = 9;}
if(!empty($area)){  $area = htmlspecialchars(strip_tags($area)) ;}else{$area = 'الجامعة';}
    // existing store_name in your database
$sql =$db->prepare("SELECT store_id , store_name , area , spaceM2 FROM store  WHERE store_name = ? LIMIT 1");
$sql->bindParam(1,$store_name);
$sql->execute();
  if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
      $store_id      = $row["store_id"];
      $store_name    = $row["store_name"];
      $area          = $row["area"];
      $spaceM2       = $row["spaceM2"];
  echo "
  <div class='alert alert-danger'>
  <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
  <b>تم تسجيل بيانات $store_name  من قبل كود رقم $store_id بمنطقة  $area المساحة = $spaceM2</b>
  </div>
  ";
  exit();
}else {
  $maxsql= $db->prepare("SELECT max(store_id) as maxd FROM store");
  $maxsql->execute();
  if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
      $maxD = $row['maxd']+1;} else {$maxD = 1 ; }

  $sql = $db->prepare(" INSERT INTO store
  (store_id , store_name, area, spaceM2)
   VALUES (?, ?, ?, ?)");
   $sql->bindParam(1,$maxD);
   $sql->bindParam(2,$store_name);
   $sql->bindParam(3,$area);
   $sql->bindParam(4,$spaceM2);
 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>
  ";}
}}
?>
