<?php
require_once "../magic/DataBase.php";
	$database = new DataBase();

  	$conn = $database->getConnection();
if(isset($_POST["cust_id"]))
{
	$idd = $_POST["cust_id"] ;
	$output = array();
	$sql = $conn->prepare("SELECT * FROM customer WHERE cust_id = ? LIMIT 1");
	$sql->bindParam(1,$idd);
	$sql->execute();
  if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
		$output["cust_id"]    	= $row["cust_id"];
		$output["idSA"] 		= $row["idSA"];
    	$output["xn"]           = $row["xn"];
		$output["cust_name"]    = $row["cust_name"];
    	$output["id_From"]      = $row["id_From"];
		$output["id_Expire"]    = $row["id_Expire"];
		$output["birth"] 	    = $row["birth"];
    	$output["job"]          = $row["job"];
		$output["nation"]       = $row["nation"];
    	$output["religion"]     = $row["religion"];
		$output["working_lic"]   = $row["working_lic"];
    	$output["working_woner"] = $row["working_woner"];
    	$output["w_phone"]       = $row["w_phone"];
		$output["w_phone2"]      = $row["w_phone2"];
		$output["working_address"] = $row["working_address"];
		$output["license"]      = $row["license"];
		$output["lic_from"] 	  = $row["lic_from"];
		$output["lic_expire"]   = $row["lic_expire"];
		$output["phone"]        = $row["phone"];
		$output["phone2"]       = $row["phone2"];
		$output["address"]      = $row["address"];
		$output["cust_photo"]   = $row["cust_photo"];
		$output["start"]        = $row["start"];
		$output["comm"]         = $row["comm"];
		$output["father"]       = $row["father"];
		$output["dgree"]        = $row["dgree"];
		$output["branch"]       = $row["branch"];
		$output["minmoney"]     = $row["minmoney"];
		$output["maxmoney"]     = $row["maxmoney"];
   }
	echo json_encode($output);
}

if(isset($_POST["idSA"]))
{
	// include "../actions/dbPDO.php";
	$idds = $_POST["idSA"] ;
	$output = array();
	$error = array();

	$Number ='/^[0-9]+(\.[0-9]{1,2})?$/' ;
	if(empty($idds))
	    { $error["msg"] = "من فضلك سجل رقم الهوية ليتم البحث ...";}
	if(!preg_match($Number,$idds)){ $error["msg"] = "رقم الهوية ارقام فقط  ..." ;}
// 	if(strlen($idds) != 10){ $error["msg"] = "تأكد من الرقم " ;	}
	if(!empty($error)){
	  $output["error"] = $error ;
	}else {
	$sql = $conn->prepare("SELECT * FROM customers WHERE idSA = ? LIMIT 1");
	$sql->bindParam(1,$idds);
	$sql->execute();
  if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
		$output["cust_id"]    	= $row["cust_id"];
		$output["idSA"] 				= $row["idSA"];
    	$output["xn"]       		= $row["xn"];
		$output["cust_name"]    = $row["cust_name"];
    	$output["id_From"]      = $row["id_From"];
		$output["id_Expire"]    = $row["id_Expire"];
		$output["birth"] 	      = $row["birth"];
    	$output["job"]          = $row["job"];
		$output["nation"]       = $row["nation"];
    	$output["religion"]     = $row["religion"];
		$output["working_lic"]   = $row["working_lic"];
    	$output["working_woner"] = $row["working_woner"];
    	$output["w_phone"]       = $row["w_phone"];
		$output["w_phone2"]      = $row["w_phone2"];
		$output["working_address"] = $row["working_address"];
		$output["license"]      = $row["license"];
		$output["lic_from"] 	  = $row["lic_from"];
		$output["lic_expire"]   = $row["lic_expire"];
		$output["phone"]        = $row["phone"];
		$output["phone2"]       = $row["phone2"];
		$output["address"]      = $row["address"];
		$output["cust_photo"]   = $row["cust_photo"];
		$output["start"]        = $row["start"];
		$output["comm"]         = $row["comm"];
		$output["father"]       = $row["father"];
		$output["dgree"]        = $row["dgree"];
		$output["branch"]       = $row["branch"];
		$output["minmoney"]     = $row["minmoney"];
		$output["maxmoney"]     = $row["maxmoney"];
		$cust = $row["cust_id"];
			$debt = $conn->prepare("SELECT sum(wanted) as debt from contracts where contracts.cust_id = ?  LIMIT 1");
			$debt->bindParam(1,$cust);
			$debt->execute();
		if($debt->rowCount()>0){$monn = $debt->fetch(PDO::FETCH_ASSOC);
			$output["debt"]       = $monn["debt"];
				}

		}else
		{
			// $output["newUser"]  = "newUser";
			$output["notfound"]  = "<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($output);
 }

	 if(isset($_POST["cust_rent"]))
	 {
	 	// include "../actions/dbPDO.php";
	 	$idds = $_POST["cust_rent"] ;
	 	$sql = $conn->prepare("SELECT contract_id , kind , numbers ,model FROM active
 	 WHERE idSA = ? AND cont_status = 1 LIMIT 1");
 	 $sql->bindParam(1,$idds);
 	 $sql->execute();
 	 if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
 	       $contract_id   = $row["contract_id"];
 	       $kind          = $row["kind"];
 	       $numbers       = $row["numbers"];
 	       $model         = $row["model"];
 	   echo "
 	   <div class='alert alert-danger'>
 	   <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
 	   <b>يوجد عقد ساري رقم $contract_id  سيارة من نوع $kind رقم اللوحة $numbers موديل $model</b>
 	   </div>
 	   ";
 	  }
	  }


?>
