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

if(isset($_POST["cust_basic"])){
                          $cust_basic =$_POST["cust_basic"];
  $cust_name   = $_POST["cust_name"];     $nation    = $_POST["nation"];        $xn           = $_POST["xn"];
  $idSA        = $_POST["idSA"];          $id_From   = $_POST["id_From"];       $id_Expire    = $_POST["id_Expire"];
  $license     = $_POST["license"];       $lic_from  = $_POST["lic_from"];      $lic_expire   = $_POST["lic_expire"];
  $birth       = $_POST["birth"];         $religion  = $_POST["religion"];      $job          = $_POST["job"];
  $maxmoney    = $_POST['maxmoney'];      $minmoney  = $_POST['minmoney'];

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

  if(empty($cust_name) || empty($idSA)|| empty($xn)|| empty($lic_expire))
  {
    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{
  $cust_name = htmlspecialchars(strip_tags($cust_name)) ;
  if((strlen($cust_name) < 7)||(strlen($cust_name) > 110)){
    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();
  }
    if(!preg_match($Number,$idSA)){
    echo "
    <div class='alert alert-warning'>
    <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
    <b>من فضلك راجع ...$idSA رقم الهوية أرقام فقط</b>
    </div> ";
    exit();
  }
  if((strlen($idSA) < 10)||(strlen($idSA) > 10)){
    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();
  }
  if(!preg_match($Number,$xn)){
    echo "
    <div class='alert alert-warning'>
    <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
    <b>من فضلك راجع ...$xn رقم النسخة أرقام فقط</b>
    </div> ";
    exit();
  }
  if(($xn < 0)||($xn > 99)){
    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();
  }
  if(!empty($nation)){ $nation = htmlspecialchars(strip_tags($nation)) ;}
  else{ $nation = 'غير معروف' ;}
   if(!empty($birth)){ $birth = htmlspecialchars(strip_tags($birth)) ;}else{
    $birth = null ;
  }
if(!empty($id_From)){ $id_From = htmlspecialchars(strip_tags($id_From)) ;}
      if(!empty($license)){ if(!preg_match($Number,$license)){
        echo "
        <div class='alert alert-warning'>
        <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
        <b>من فضلك راجع .....$license الديانة حروف فقط </b>
        </div>
        ";
        exit();
      }}
      if(!empty($lic_from)){ $lic_from = htmlspecialchars(strip_tags($lic_from)) ;}
      else{ $lic_from = 'غير معروف' ;}
      if(!empty($job)){ $job = htmlspecialchars(strip_tags($job)) ;}
      else{ $job = 'غير معروف' ;}
      if(!empty($religion)){ $religion = htmlspecialchars(strip_tags($religion)) ;}
        else{ $religion = 'غير معروف' ;}
      if($maxmoney < 0){$maxmoney = 0 ;};
      if($minmoney < 0){$minmoney = 0 ;}
    //   if(empty($lic_expire)){ $lic_expire = null ;}
      $sql=$db->prepare("UPDATE customers SET cust_name = ?, nation = ?, xn = ?, idSA = ?, id_From = ?,
          license = ? , lic_from = ?,lic_expire = ?,  birth = ?, job = ?, religion = ? ,father = ?,
         minmoney = ?, maxmoney = ?
         WHERE cust_id = ?");
      $sql->bindParam(1,$cust_name);
      $sql->bindParam(2,$nation);
      $sql->bindParam(3,$xn);
      $sql->bindParam(4,$idSA);
      $sql->bindParam(5,$id_From);
    //   $sql->bindParam(6,$id_Expire);
      $sql->bindParam(6,$license);
      $sql->bindParam(7,$lic_from);
      $sql->bindParam(8,$lic_expire);
      $sql->bindParam(9,$birth);
      $sql->bindParam(10,$job);
      $sql->bindParam(11,$religion);
      $sql->bindParam(12,$father);
      $sql->bindParam(13,$minmoney);
      $sql->bindParam(14,$maxmoney);
      $sql->bindParam(15,$cust_basic);
      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>
    ";}
} // end if empty
} // end of cust_basic update form

if(isset($_POST["cust_dd"])){
    $cust_dd      = $_POST["cust_dd"];
    $photo_idSA   = $_POST["photo_idSA"];
    $myPhoto      = $_FILES["cust_photo"];
    if(!empty($cust_dd)||!empty($photo_idSA)||!empty($myPhoto)){
      include_once "../op/upload_photo.php";
    $location = "../photo/customers/" ;
      if (!empty($_FILES["cust_photo"]["name"])){
      $cust_photo = upload_file($myPhoto , $photo_idSA,  $location);
          $sql=$db->prepare("UPDATE customers SET  cust_photo = ?
         WHERE cust_id = ?");
         $sql->bindParam(1,$cust_photo);
         $sql->bindParam(2,$cust_dd);
         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>يوجد خطأ في تسجيل البيانات ... راجع  من فضلك455</b>
         </div>
       ";}
       }
       }
     }// end of cust p

if(isset($_POST["cust_tel"])){
            $cust_tel    = $_POST['cust_tel'];
  $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'];

  $name ='/^[-a-zA-Z0-9_\p{Arabic}-+ـ\s]*$/u';
  $Number = "/^[0-9]+$/";
  if(empty($phone) || empty($address)||empty($dgree))
  {
    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(!preg_match($Number,$phone)){
      echo "
      <div class='alert alert-warning'>
      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
      <b>من فضلك راجع ...$phone رقم الجوال أرقام فقط</b>
      </div>
      ";
      exit();
    }
    if(strlen($phone) < 10){
      echo "
      <div class='alert alert-warning'>
      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
      <b>رقم جوال2 اقل من عشرة ارقم انتبه من فضلك ...</b>
      </div>
      ";
      exit();
    }
    if(!preg_match($Number,$dgree)){
      echo "
      <div class='alert alert-warning'>
      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
      <b>من فضلك راجع ...$dgree تقييم المستأجر أرقام فقط من 0 إلي 100 فقط</b>
      </div> ";
      exit();
    }
    if($dgree < 0){$dgree = 0 ;}else if($dgree > 100){$dgree = 99 ;}
  if(!empty($phone2)){ if(!preg_match($Number,$phone2)){
          echo "
          <div class='alert alert-warning'>
          <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
          <b>   من فضلك راجع ... $phone2 رقم جوال2 للمستأجر أرقام فقط</b>
          </div>
        ";
          exit();
        }
        if((strlen($phone2) < 10)){
          echo "
          <div class='alert alert-warning'>
          <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
          <b>رقم جوال2 اقل من عشرة ارقم انتبه من فضلك ...</b>
          </div>
          ";
          exit();
        }  }else{
            $phone2 = null ;
        }
    if(!empty($comm)){  $comm = htmlspecialchars(strip_tags($comm)) ;}

        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 phone number in your database
        $sql = $db->prepare("SELECT cust_id , cust_name , phone , phone2 FROM customers
          WHERE phone = '$phone'  LIMIT 3");
        $sql->execute();
        if($sql->rowCount()>1){
        while($row = $sql->fetch()){
              $cust_id    = $row["cust_id"];
              $cust_name  = $row["cust_name"];
              $phone      = $row["phone"];
          echo "
          <div class='alert alert-danger'>
          <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
          <b>هذا الرقم استخدم من قبل باسم السيد: $cust_name  من قبل كود رقم $cust_id جوال رقم $phone و جوال  $phone2</b>
          </div>
          ";}
          exit();
  }else {
      $sql=$db->prepare("UPDATE customers SET phone =?, phone2 = ? , address =?, dgree = ?,comm =?, father = ?,
              StreetName = ?, BuildingNumber = ? , PlotIdentification = ? ,SubDivisionName = ? ,CityName = ? ,PostalNumber = ?
               WHERE cust_id = ?");
            $sql->bindParam(1,$phone);
            $sql->bindParam(2,$phone2);
            $sql->bindParam(3,$address);
            $sql->bindParam(4,$dgree);
            $sql->bindParam(5,$comm);
            $sql->bindParam(6,$father);

            $sql->bindParam(7,$StreetName);
            $sql->bindParam(8,$BuildingNumber);
            $sql->bindParam(9,$PlotIdentification);
            $sql->bindParam(10,$SubDivisionName);
            $sql->bindParam(11,$CityName);
            $sql->bindParam(12,$PostalNumber);

            $sql->bindParam(13,$cust_tel);
            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>يوجد خطأ في تسجيل البيانات ... راجع  من فضلك455</b>
            </div>
          " ; }
  }}}// end of customer telecomunication

if(isset($_POST["cust_work"])){
  $cust_work          = $_POST["cust_work"];
  $working_lic        = $_POST['working_lic'];          $working_woner      = $_POST['working_woner'];
  $w_phone            = $_POST['w_phone'];              $w_phone2           = $_POST['w_phone2'];
  $working_address    = $_POST['working_address'];      $cust_vat           = $_POST['cust_vat'];

  $Number = '/^[0-9]+(\.[0-9]{1,2})?$/' ;
  if(empty($working_woner))
  {
    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{
    $working_woner = htmlspecialchars(strip_tags($working_woner)) ;
    
  if(!empty($working_lic)){ $working_lic = htmlspecialchars(strip_tags($working_lic)) ;}
    if(!empty($w_phone)){ if(!preg_match($Number,$w_phone)){
                    echo "
                    <div class='alert alert-warning'>
                    <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
                    <b>من فضلك راجع .....$w_phone جوال جهة العمل أرقام فقط </b>
                    </div>
                    ";
                    exit();
    }}
  if(!empty($w_phone2)){ if(!preg_match($Number,$w_phone2)){
                    echo "
                    <div class='alert alert-warning'>
                    <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
                    <b>من فضلك راجع .....$w_phone2 جوال العمل2 أرقام فقط </b>
                    </div>
                    ";
                    exit();
    }}
  if(!empty($working_address)){ $working_address = htmlspecialchars(strip_tags($working_address)) ;}
  if(!empty($cust_vat)){ 
    $cust_vat = htmlspecialchars(strip_tags($cust_vat)) ;   
    if(strlen($cust_vat)!= 15){
      echo "
                    <div class='alert alert-warning'>
                    <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
                    <b>من فضلك راجع .....$cust_vat فضلا راجع الرقم الضريبي</b>
                    </div>
                    ";
                    exit();
    }
  }

$sql=$db->prepare("UPDATE customers SET  working_lic = ?, working_woner = ?, w_phone = ? ,w_phone2 = ? , working_address=? , cust_vat = ?
                     WHERE cust_id = ?");
    $sql->bindParam(1,$working_lic);
    $sql->bindParam(2,$working_woner);
    $sql->bindParam(3,$w_phone);
    $sql->bindParam(4,$w_phone2);
    $sql->bindParam(5,$working_address);
    $sql->bindParam(6,$cust_vat);
    $sql->bindParam(7,$cust_work);
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> ";}
    }}//end of working form
?>
