<?php
require_once "../magic/session_config.php";
bdr_start_session();
ob_start();
$father = $_SESSION["user_id"];
$branch = $_SESSION['branch'];
include "dbPDO.php";
// include "../op/upload_photo.php";
$idSA         = $_POST["idSA"];             $xn             = $_POST["xn"];              $cust_name   = $_POST["cust_name"];
$id_From      = $_POST["id_From"];          $id_Expire      = $_POST["id_Expire"];
// $birth        = $_POST["birth"];
$job            = $_POST["job"];            $nation         = $_POST["nation_sr"];       $birth    = $_POST["birth"];
$birth_hijri    = $_POST["birth_hijri"];

$cust_vat    = $_POST["cust_vat"];          $working_woner  = $_POST["working_woner"];
$w_phone     = $_POST["w_phone"];           $w_phone2       = $_POST["w_phone2"];
$working_address   = $_POST["working_address"];

$license     = $_POST["license"];           $lic_from       = $_POST["lic_from"];        $lic_expire   = $_POST["lic_expire"];
$phone       = $_POST["phone"];             $phone2         = $_POST["phone2"];
$address     = $_POST["address"];           $dgree          = $_POST["dgree"];
$comm        = $_POST["comm"];

$StreetName         = $_POST["StreetName"];
$BuildingNumber     = $_POST["BuildingNumber"];
$PlotIdentification = $_POST["PlotIdentification"];
$SubDivisionName    = $_POST["SubDivisionName"];
$CityName           = $_POST["CityName"];
$PostalNumber       = $_POST["PostalNumber"];

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

if(empty($idSA) || empty($cust_name)|| empty($xn)|| empty($lic_expire)|| empty($phone))
{
  $error["new6"] = "
        <div class='alert alert-warning'>
        <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;' >&times;</a>
        <b>من فضلك سجل رقم الهوية و رقم النسخة و الاسم و نهاية الهوية و الرخصة و رقم الجوال  ليتم تسجيل المستأجر ....</b>
        </div>
  ";

}else{
if(!preg_match($Number,$idSA)){
  $error["idSA"] ="من فضلك راجع ... رقم الهوية أرقام فقط ";
}

if((strlen($idSA) < 10)||(strlen($idSA) > 10)){  $error["idSA"] = "من فضلك راجع عدد ارقام الهوية ..."; }
if(!empty($cust_vat)){
  if((strlen($cust_vat) < 15)||(strlen($cust_vat) > 15)){  $error["cust_vat"] = "من فضلك راجع عدد ارقام الرقم الضريبي للعميل..."; }
}


if(!preg_match($Number,$xn)){  $error["xn"] = "من فضلك راجع ... رقم النسخة ارقام فقط ";  }

if(($xn < 0)||($xn > 99)){  $error["xn"] = "من فضلك راجع رقم النسحة... "; }
if(!empty($cust_name)){ $cust_name = htmlspecialchars(strip_tags($cust_name)) ;   }else{$cust_name = null;}
if((strlen($cust_name) < 7)||(strlen($cust_name) > 110)){  $error["cust_name"] = "من فضلك تأكد من اسم المستاجر .. "; }
if(!empty($id_From)){ $id_From = htmlspecialchars(strip_tags($id_From)) ;   }else{$id_From = null;}
if(!empty($job)){ $job = htmlspecialchars(strip_tags($job)) ;   }else{$job = null;}
if(!empty($nation)){ $nation = htmlspecialchars(strip_tags($nation)) ;   }else{$nation = null;}

if(empty($birth)){
  $birth =   date('Y-m-d', strtotime(' - 21 year'));
}
if(empty($birth_hijri)){
  $birth_hijri =   date('Y-m-d', strtotime(' - 21 year'));
}

// if(!empty($cust_vat)){ $cust_vat = htmlspecialchars(strip_tags($cust_vat)) ;   }else{$cust_vat = null;}

if(!empty($working_woner)){ $working_woner = htmlspecialchars(strip_tags($working_woner)) ;   }else{$working_woner = null;}

if(!empty($working_address)){ $working_address = htmlspecialchars(strip_tags($working_address)) ;   }else{$working_address = null;}
if(!empty($w_phone)){ if(!preg_match($Number,$w_phone)){
                $error["w_phone"] = "من فضلك راجع ..... جوال جهة العمل أرقام فقط ";}}else{$w_phone = null;}
if(!empty($w_phone2)){ if(!preg_match($Number,$w_phone2)){
                $error["w_phone2"] = "من فضلك راجع ..... جوال العمل2 أرقام فقط"; }}else{$w_phone2 = null;}

if(!empty($license)){ if(!preg_match($Number,$license)){
                $error["license"] = "رقم الرخصة أرقام فقط راجع من فضلك"; }}else{$license = null;}

if(!empty($lic_from)){ $lic_from = htmlspecialchars(strip_tags($lic_from)) ;   }else{$lic_from = null;}
if(!preg_match($Number,$phone)){  $error["phone"] = "رقم الجوال أرقام صحيحة فقط ";}
if((strlen($phone) < 10)){   $error["phone"] = "رقم الجوال أقل من 10 ارقام"; }
if(!empty($phone2)){ if(!preg_match($Number,$phone2)){ $error["phone2"] = "من فضلك راجع ... رقم الهيكل خطأ "; }
    if((strlen($phone2) < 10)){ $error["phone2"] = "رقم الجوال2 أقل من عشر أرقام";  }}else{$phone2 = null;}
if(!empty($address)){ $address = htmlspecialchars(strip_tags($address)) ;   }else{$address = null;}
if(!empty($dgree)){ if(!preg_match($Number,$dgree)){ $error["dgree"] = "تقيم المستاجر من 0 إلي 100 فقط";   }}else{$dgree = 65;}
if(!empty($comm)){ $comm = htmlspecialchars(strip_tags($comm)) ;   }else{$comm = null;}

if(!empty($StreetName)){ $StreetName = htmlspecialchars(strip_tags($StreetName)) ;   }else{$StreetName = null;}
if(!empty($BuildingNumber)){ $BuildingNumber = htmlspecialchars(strip_tags($BuildingNumber)) ;   }else{$BuildingNumber = null;}
if(!empty($PlotIdentification)){ $PlotIdentification = htmlspecialchars(strip_tags($PlotIdentification)) ;   }else{$PlotIdentification = null;}
if(!empty($SubDivisionName)){ $SubDivisionName = htmlspecialchars(strip_tags($SubDivisionName)) ;   }else{$SubDivisionName = null;}
if(!empty($CityName)){ $CityName = htmlspecialchars(strip_tags($CityName)) ;   }else{$CityName = null;}
if(!empty($PostalNumber)){ $PostalNumber = htmlspecialchars(strip_tags($PostalNumber)) ;   }else{$PostalNumber = null;}

  // existing car number in your database
$sql = $db->prepare("SELECT cust_id , idSA , cust_name , nation FROM customers WHERE idSA = '$idSA' OR cust_name = '$cust_name' LIMIT 1");
$sql->execute();
if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
      $cust_id2      = $row["cust_id"];
      $idSA2         = $row["idSA"];
      $cust_name2    = $row["cust_name"];
      $nation2       = $row["nation"];
  $error["new6"] = "
  <div class='alert alert-danger'>
  <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
  <b>تم تسجيل بيانات $cust_name2  من قبل كود رقم $cust_id2 هوية رقم $idSA2 الجنسية   $nation2</b>
  </div>
  ";

}
if(!empty($error)){
  $data["error"] = $error ;
}else {
          $maxsql= $db->prepare("SELECT max(cust_id) as maxd FROM customers");
          $maxsql->execute();
          if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
              $maxD = $row['maxd']+1;} else {$maxD = 1 ; }
    if(empty($id_Expire)){$id_Expire = null;}
$start = date("Y-m-d");
      $sql=$db->prepare(" INSERT INTO customers
      (cust_id, idSA , xn ,cust_name ,id_From , id_Expire , job , nation , birth , cust_vat , working_woner, w_phone , w_phone2 ,
         working_address , license , lic_from , lic_expire , phone , phone2 , address 
         , dgree , comm , start , branch , father,birth_hijri ,StreetName ,BuildingNumber ,PlotIdentification ,SubDivisionName,
      CityName ,PostalNumber)
  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?,?, ?,?  , ?, ?, ?,?, ?,?)");
    $sql->bindParam(1,$maxD);
    $sql->bindParam(2,$idSA);
    $sql->bindParam(3,$xn);
    $sql->bindParam(4,$cust_name);
    $sql->bindParam(5,$id_From);
    $sql->bindParam(6,$id_Expire);
    $sql->bindParam(7,$job);
    $sql->bindParam(8,$nation);
    $sql->bindParam(9,$birth);
    $sql->bindParam(10,$cust_vat);
    $sql->bindParam(11,$working_woner);
    $sql->bindParam(12,$w_phone);
    $sql->bindParam(13,$w_phone2);
    $sql->bindParam(14,$working_address);
    $sql->bindParam(15,$license);
    $sql->bindParam(16,$lic_from);
    $sql->bindParam(17,$lic_expire);
    $sql->bindParam(18,$phone);
    $sql->bindParam(19,$phone2);
    $sql->bindParam(20,$address);
    $sql->bindParam(21,$dgree);
    $sql->bindParam(22,$comm);
    $sql->bindParam(23,$start);
    $sql->bindParam(24,$branch);
    $sql->bindParam(25,$father);
    $sql->bindParam(26,$birth_hijri);

    $sql->bindParam(27,$StreetName);
    $sql->bindParam(28,$BuildingNumber);
    $sql->bindParam(29,$PlotIdentification);
    $sql->bindParam(30,$SubDivisionName);
    $sql->bindParam(31,$CityName);
    $sql->bindParam(32,$PostalNumber);

      if($sql->execute()){
  $data["new6"] = "
    <div class='alert alert-success'>
    <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
    <b> تم تسجيل بيانات المستأجر بنجاح  ....   </b>
  </div>";
$data["cust_dd"]    = $maxD ;
$data["photo_idSA"] = $idSA ;

  }
  else{ $data["new6"] = "<div class='alert alert-danger'>
  <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
    <b>يوجد خطأ في تسجيل البيانات ... راجع  من فضلك</b>
    </div>
  ";}

}
echo json_encode($data);
}

?>
