<?php
session_start();
ob_start();
$father = $_SESSION["user_id"];
include "dbPDO.php";

$realName  = $_POST['realName'];
$user_name = $_POST['user_name'];
$pass       =$_POST['pass'];
$pass2      =$_POST['pass2'];
$email    = $_POST['email'];
$phone1    = $_POST['phone1'];
$phone2   = $_POST['phone2'];
$address  = $_POST["address"];
$birth    = $_POST['birth'];
$start    = $_POST['start'];
$idCard   = $_POST['idcard'];
$gotFrom  = $_POST['gotFrom'];
$expire   = $_POST['expire'];
$nation   = $_POST['nation'];
$job      = $_POST['job'];

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

if(empty($realName) || empty($user_name) ||empty($pass) ||empty($pass2) ||empty($phone1) || empty($address) || empty($idCard)||
 empty($nation)||empty($job)){
  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{
 $realName = htmlspecialchars(strip_tags($realName)) ;
 $user_name = htmlspecialchars(strip_tags($user_name)) ;
 $pass = htmlspecialchars(strip_tags($pass)) ;

if(strlen($pass) < 8){
  echo "
  <div class='alert alert-warning'>
  <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
  <b>كلمة سر ضعيفة جدا من فضلك  كلمة السر يجب ان تزيد علي 8 حروف و ارقام</b>
  </div>
  ";
  exit();
}
if(strlen($pass2) < 8){
  echo "
  <div class='alert alert-warning'>
  <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
  <b>كلمة سر ضعيفة جدا من فضلك  كلمة السر يجب ان تزيد علي 8 حروف و ارقام</b>
  </div>";
  exit();
}
if($pass != $pass2){
  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($email)){$email = htmlspecialchars(strip_tags($email)) ;}
if(!preg_match($Number,$phone1)){
  echo "
  <div class='alert alert-warning'>
  <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
  <b>  $phone1 رقم غلط</b>
  </div>
";
  exit();
}
if(!(strlen($phone1)== 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();
}

// existing idCrad or phone number in your database
$sql =$db->prepare("SELECT user_id , realName , idCard , phone1 FROM users WHERE idCard = '$idCard' OR
   phone1 = '$phone1' OR realName ='$realName' OR user_name ='$user_name' LIMIT 1");
$sql->execute();
if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
      $user_id   = $row["user_id"];
      $realName  = $row["realName"];
      $idCard    = $row["idCard"];
      $phone     = $row["phone1"];
  echo "
  <div class='alert alert-danger'>
  <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
  <b>تم تسجيل بيانات $realName  من قبل كود رقم $user_id ورقم هوية $idCard و رقم جوال  $phone1</b>
  </div>
  ";
  exit();
}else {
  $maxsql= $db->prepare("SELECT max(user_id) as maxd FROM users");
  $maxsql->execute();
  if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
      $maxID = $row['maxd']+1;} else {$maxID = 1 ; }

  $pass = password_hash($pass,PASSWORD_DEFAULT);
  $sql =$db->prepare(" INSERT INTO users
  (user_Id, realName, user_name, pass, email, phone1, phone2,
  address,birth, start,  idCard, gotFrom, expire, nation, job ,father)
   VALUES (?, ?, ?, ?, ?, ?, ?,?,   ?, ? , ?, ? ,  ? , ? ,? , ?)");

    $sql->bindParam(1,$maxID);
    $sql->bindParam(2,$realName);
    $sql->bindParam(3,$user_name);
    $sql->bindParam(4,$pass);
    $sql->bindParam(5,$email);
    $sql->bindParam(6,$phone1);
    $sql->bindParam(7,$phone2);
    $sql->bindParam(8,$address);
    $sql->bindParam(9,$birth);
    $sql->bindParam(10,$start);
    $sql->bindParam(11,$idCard);
    $sql->bindParam(12,$gotFrom);
    $sql->bindParam(13,$expire);
    $sql->bindParam(14,$nation);
    $sql->bindParam(15,$job);
    $sql->bindParam(16,$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>
    ";}
  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>
  ";}
}
}
?>
