<?php

require_once __DIR__ . '/../magic/auth.php';

$auth = bdr_require_privilege(1, '../logout.php');
$user = $auth['user_id'];
$father = $auth['user_id'];
$branch = $auth['branch'];
$conn = $auth['conn'];
$db = $auth['conn'];

include "../op/close_up.php";


$time_in = date("H:i:s");
$date_in = date("Y-m-d");
$audate = date("Y-m-d H:i:s");

if(isset($_POST["km_in"])){
if($_POST['contract_id'] !=""){
    $contract_id   = $_POST['contract_id'];
    $open_kilo     = $_POST['open_kilo'];
    $km_in         = $_POST['km_in'];
    $kilo_cost     = $_POST['kilo_cost'];
    $addtion_cost  = $_POST['addtion_cost'];
    $addtion_desc  = $_POST['addtion_desc'];
    $violations    = $_POST['violations'];
    $deduction     = $_POST["deduction"];
    $comm          = $_POST["comm"];
    // $carid         = $_POST["carid"];

    $out_info = $db->prepare("SELECT  carid ,date_out , km_out , time_out from contracts where contract_id = ? ");
    $out_info->bindParam(1,$contract_id);
    $out_info->execute();
      if($out_info->rowCount()>0){$basic = $out_info->fetch(PDO::FETCH_ASSOC);
          $date_out 	= $basic["date_out"];
          $km_out 		= $basic["km_out"];
          $time_out   = $basic["time_out"];
          $carid      = $basic["carid"];

    $last = $db->prepare("SELECT  km3  from cars where carid = ? ");
    $last->bindParam(1,$carid);
    $last->execute();
if($last->rowCount()>0){$free = $last->fetch(PDO::FETCH_ASSOC);
        $km3 			= $free["km3"];
}}
      $dc ='/^[0-9]+(\.[0-9]{1,2})?$/' ;
      $Number ="/^[0-9]+$/";
      $data  = array();
      $error = array();

  if(empty($free_kilo))
          {
            $free_kilo = 300 ;
          }

    if($open_kilo > 0){
      $kilo_cost = 0 ;
    }else{
      if(empty($kilo_cost))
      {
        $error["kilo_cost"] = "من فضلك سجل سعر الكيلو بالهللة";
      }
    if(!preg_match($Number,$kilo_cost)){ $error["kilo_cost"] = "أرقام فقط" ;}
    if($km3 > $kilo_cost){  $kilo_cost = $km3  ;}
    }

    if(empty($km_in)){ $error["km_in"] = "سجل عداد الدخول من فضلك ليتم غلق العقد ..." ; }
    else   if(!preg_match($Number,$km_in)){ $error["km_in"] = "أرقام صحيحة فقط" ;}
    if(!empty($addtion_cost))  { if(!preg_match($dc,$addtion_cost)){ $error["addtion_cost"] = "تأكد من الرقم" ;}}else{$addtion_cost = 0 ;}
    if(!empty($violations))    { if(!preg_match($Number,$violations)){ $error["violations"] = "أرقام صحيحة فقط" ;}}else{$violations = 0 ;}
    if(!empty($deduction))     { if(!preg_match($dc,$deduction)){ $error["deduction"] = "تأكد من الرقم" ;}}else{$deduction = 0 ;}
    if(!empty($addtion_desc)){  $addtion_desc = htmlspecialchars(strip_tags($addtion_desc)) ;}
    if(!empty($comm)){  $comm = htmlspecialchars(strip_tags($comm)) ;}
if(!empty($error)){
  $data["error"] = $error ;
}else {
  // $close_at = null;
$contt = $db->prepare(" UPDATE contracts SET date_in = ?,  km_in = ? , kilo_cost = ?,
  time_in = ? , addtion_cost = ? , addtion_desc = ?,
  violations = ? ,  comm = ?,deduction = ?,  closer = ?, audate = ? ,cont_status = 3
    WHERE contract_id = ? ");
        $contt->bindParam(1,$date_in);
        $contt->bindParam(2,$km_in);
        $contt->bindParam(3,$kilo_cost);
        $contt->bindParam(4,$time_in);
        $contt->bindParam(5,$addtion_cost);
        $contt->bindParam(6,$addtion_desc);
        $contt->bindParam(7,$violations);
        $contt->bindParam(8,$comm);
        $contt->bindParam(9,$deduction);
        $contt->bindParam(10,$father);
        $contt->bindParam(11,$audate);
        $contt->bindParam(12,$contract_id);
  if($contt->execute()){
    auto_close($db,$contract_id) ;
        $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>" ;
  $car  = $db->prepare("UPDATE cars set carstatus = 1 where carid = $carid LIMIT 1");
  $car->execute();

  }
 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 ' خطاء داتا لبسي';
            
            }
}
 echo json_encode($data);
}
}
if(isset($_POST["audate"])){
  $contract_id = $_POST["audate"];
  $au = $db->prepare(" UPDATE contracts SET printed = 1 WHERE contract_id = ? ");
  $au->bindParam(1,$contract_id);
  $au->execute();
} 
?>