<?php
session_start();
ob_start();

$father = $_SESSION["user_id"];
$time_out = date("H:i");
$date_out = date("Y-m-d");

include "../actions/dbPDO.php";
include "../op/wanted.php";

if($_POST['contract_id'] !=""){
  $contract_id   = $_POST["contract_id"];
      $wanted = new  Wanted($db,$contract_id) ;
      $pay    = $wanted->wanted() ;
      $wanted = $pay["wanted"] ;

      $money    = $ww + 5000 ;
$data = array();
$error = array();

if(empty($contract_id)){ $money_in = 0 ;}
if(empty($carid) || empty($cust_id)||empty($tamm_days) || empty($day_cost)|| empty($km_out))
    {
    $error["msg22"] ="<div class='alert alert-danger'>
      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
        <b>من فضلك سجل بيانات العقد كاملة ليتم حفظ العقد  </b>
      </div> " ;
    }
    if(!empty($carid)){
      $sql = $db->prepare("SELECT monthly , freekilo , km2 , carstatus FROM cars WHERE carid = '$carid' LIMIT 1");
      $sql->execute();
      if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
            $minn       = $row["monthly"];
            $freekilo   = $row["freekilo"];
            $km3        = $row["km2"];
            $carstatus  = $row["carstatus"];
          }
          if($carstatus != 1){exit() ;}
      if($day_cost < $minn){  $day_cost  = $minn ;}
      if($kilo_cost < $km3){  $kilo_cost = $km3 ;}
      if($free_kilo < $freekilo){  $free_kilo = $freekilo ;}
    }

    // existing contract to current customer
    $sql = $db->prepare("SELECT dgree FROM customers  WHERE cust_id = ? LIMIT 1");
    $sql->bindParam(1,$cust_id);
    $sql->execute();
    if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
      $dgree   = $row["dgree"];}
      if($dgree < 50){$error["open_comm"] = "مستاجر قائمة سوداء إنتبه من فضلك" ;}

if(!empty($error)){
  $data["error"] = $error ;
  }
  else
  {
  $maxCONT = "0";
            $maxsql= $db->prepare("SELECT max(court_id) as maxd FROM courts");
            $maxsql->execute();
        if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
              $maxCONT = $row['maxd'] + 1 ;}
        else { $maxCONT = $maxCONT + 1 ; }

    $sql=$db->prepare(" INSERT INTO courts
        (court_id , contract_id , start_date , money , father)
    VALUES (?, ?, ?, ?, ?)");
      $sql->bindParam(1,$maxCONT);
      $sql->bindParam(2,$contract_id);
      $sql->bindParam(3,$start_date);
      $sql->bindParam(4,$money);
      $sql->bindParam(5,$father);
      if($sql->execute()){
  $data["contract_id"] = $maxCONT ;
  $data["msg"]= "<div class='alert alert-success'>
 <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
 <b> تم تسجيل العقد بنجاح </b>
  </div>" ;
  }
 else{ $data["msg"] = "<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);
 ?>
