<?php
require_once "../magic/session_config.php";
bdr_start_session();
ob_start();
if(!isset($_SESSION["user_id"])){header("location:../logout.php");}

  require_once "../magic/DataBase.php";
	$dataBase = new DataBase();
	$conn = $dataBase->getConnection();
  
  $branch = $_SESSION["branch"];
  $father = $_SESSION["user_id"];
  $sanad_date = date("Y-m-d");

if(isset($_POST["carid"])){
    if($_POST['contract_id'] !=""){
          $contract_id     = $_POST['contract_id'];
          $operating_comm  = $_POST['operating_comm'];
          $free_kilo       = $_POST["free_kilo"];
          $carid           = $_POST["carid"];
          $addtion_desc    = $_POST['addtion_desc']?? "";
          $addtion_cost    = $_POST['addtion_cost']?? 0;
          $violations      = $_POST['violations']?? 0;
          $deduction       = $_POST['deduction']?? 0;


$Number ='/^[0-9]+(\.[0-9]{1,2})?$/' ;
$data = array();
$error = array(); 
    if(!empty($operating_comm)){ $operating_comm = htmlspecialchars(strip_tags($operating_comm)) ;}else{
      $operating_comm = null ;
    }
    if(!empty($addtion_desc)){ $addtion_desc = htmlspecialchars(strip_tags($addtion_desc)) ;}else{
      $addtion_desc = null ;
    }
    if(!empty($free_kilo))     { if(!preg_match($Number,$free_kilo)){ $error["free_kilo"] = "أرقام صحيحة فقط" ;}}
    else{$free_kilo = 250 ;}
    if(!empty($violations))     { if(!preg_match($Number,$violations)){ $error["violations"] = "أرقام صحيحة فقط" ;}}
    else{$violations = 0 ;}
     if(!empty($deduction))     { if(!preg_match($Number,$deduction)){ $error["deduction"] = "أرقام صحيحة فقط" ;}}
    else{$deduction = 0 ;}

if(!empty($error)){
  $data["error"] = $error ;
}else {
    $contt=$conn->prepare(" UPDATE contracts SET operating_comm = ? , free_kilo = ? ,violations = ?, addtion_desc = ? ,
                         addtion_cost = ? , deduction = ?
                        WHERE contract_id = ? ");
   $contt->bindParam(1,$operating_comm);
   $contt->bindParam(2,$free_kilo);
   $contt->bindParam(3,$violations);
   $contt->bindParam(4,$addtion_desc);
   $contt->bindParam(5,$addtion_cost);
   $contt->bindParam(6,$deduction);
   $contt->bindParam(7,$contract_id);
   if($contt->execute()){
 $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);
}
}

?>
