<?php
session_start();
ob_start();
if(!isset($_SESSION["user_id"])){header("location:../logout.php");}
$father = $_SESSION["user_id"];
include "../actions/dbPDO.php";

if(isset($_POST["naql_ins"])){
    include_once "../op/upload_photo.php";
    $carid              = $_POST["naql_ins"];
    $myPhoto            = $_FILES["insurance_lic"];
    $insurance_id       = $_POST["insurance_id"];
    $insurance_expire   = $_POST["insurance_expire"];
    $insurance_no       =  htmlspecialchars(strip_tags($_POST['insurance_no']));
    $insurance_kind     = $_POST['insurance_kind'];
    $insurance_money    = htmlspecialchars(strip_tags($_POST['insurance_money']));
    // if(empty($myPhoto)){
      if ($_FILES["insurance_lic"]["name"] == ''){
      $sql = $db->prepare("UPDATE naql SET insurance_id = ?,insurance_expire = ?,insurance_no = ?,
            insurance_kind = ?,insurance_money = ?, father = ?  WHERE carid = ?");

         $sql->bindParam(1,$insurance_id);
         $sql->bindParam(2,$insurance_expire);
         $sql->bindParam(3,$insurance_no);
         $sql->bindParam(4,$insurance_kind);
         $sql->bindParam(5,$insurance_money);
         $sql->bindParam(6,$father);
         $sql->bindParam(7,$carid);
         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>
       ";} 
    }else{

    $location = "../photo/insurance/" ;
      $myName = $carid."-2022";

    if ($_FILES["insurance_lic"]["name"] != ''){
      $insurance_lic = upload_file($myPhoto , $myName,  $location);
   
         $sql = $db->prepare("UPDATE naql SET  insurance_lic = ?,insurance_id = ?,insurance_expire = ?,insurance_no = ?,
            insurance_kind = ?,insurance_money = ?, father = ?  WHERE carid = ?");
         $sql->bindParam(1,$insurance_lic);
         $sql->bindParam(2,$insurance_id);
         $sql->bindParam(3,$insurance_expire);
         $sql->bindParam(4,$insurance_no);
         $sql->bindParam(5,$insurance_kind);
         $sql->bindParam(6,$insurance_money);
         $sql->bindParam(7,$father);
         $sql->bindParam(8,$carid);
         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>يوجد خطأ في تسجيل البيانات ... راجع  من فضلك455</b>
         </div>
       ";} 
      }}
}

if(isset($_POST["naql_oper"])){
 
  $carid              = $_POST["naql_oper"];
  $myPhoto            = $_FILES["operationCard"];
  $operation_no       = htmlspecialchars(strip_tags($_POST["operation_no"]));
  $operation_expire   = $_POST["operation_expire"];
  $car_serial         = htmlspecialchars(strip_tags($_POST['car_serial']));

  // if(empty($myPhoto)){
    if ($_FILES["operationCard"]["name"] == ''){
    $sql = $db->prepare("UPDATE naql SET operation_no = ?,operation_expire = ?,car_serial = ?,
                          father = ?  WHERE carid = ?");
       $sql->bindParam(1,$operation_no);
       $sql->bindParam(2,$operation_expire);
       $sql->bindParam(3,$car_serial);
       $sql->bindParam(4,$father);
       $sql->bindParam(5,$carid);
       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>
     ";} 
  }else{
  $location = "../photo/operationCard/" ;
    $myName = $carid."-2022.pdf";

  if ($_FILES["operationCard"]["name"] != ''){
    $tname = $_FILES["operationCard"]["tmp_name"];
    move_uploaded_file($tname, $location.$myName);
       $sql = $db->prepare("UPDATE naql SET  operationCard = ?, operation_no = ?,operation_expire = ?,car_serial = ?,
                              father = ?  WHERE carid = ?");
       $sql->bindParam(1,$myName);
       $sql->bindParam(2,$operation_no);
       $sql->bindParam(3,$operation_expire);
       $sql->bindParam(4,$car_serial);
       $sql->bindParam(5,$father);
       $sql->bindParam(6,$carid);
       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>يوجد خطأ في تسجيل البيانات ... راجع  من فضلك455</b>
       </div>
     ";} 
    }else{  echo "do not have photo" ;   }
  }
}
// الفحص الدوري 
if(isset($_POST["naql_check"])){

  $carid        = $_POST["naql_check"];
  $myPhoto      = $_FILES["pre_check"];
  $pre_expire   = $_POST["pre_expire"];

    if ($_FILES["pre_check"]["name"] == ''){
    $sql = $db->prepare("UPDATE naql SET pre_expire = ? WHERE carid = ?");
       $sql->bindParam(1,$pre_expire);
       $sql->bindParam(2,$carid);
       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>
     ";} 
  }else{
    include_once "../op/upload_photo.php";
  $location = "../photo/preCheck/" ;
    $myName = $carid."-2022";

  if ($_FILES["pre_check"]["name"] != ''){
    $pre_check = upload_file($myPhoto , $myName,  $location);
       $sql = $db->prepare("UPDATE naql SET  pre_check = ?, pre_expire = ? , father = ?  WHERE carid = ?");
       $sql->bindParam(1,$pre_check);
       $sql->bindParam(2,$pre_expire);
       $sql->bindParam(3,$father);
       $sql->bindParam(4,$carid);
       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>يوجد خطأ في تسجيل البيانات ... راجع  من فضلك455</b>
             </div>";} 
    }else{  echo "do not have photo" ;   }
  }
}
// gps conneced id 
if(isset($_POST["naql_gps"])){

  $carid        = $_POST["naql_gps"];
  $gpsid        = htmlspecialchars(strip_tags($_POST["gpsid"]));
  $serviceid    = htmlspecialchars(strip_tags($_POST["serviceid"]));

       $sql = $db->prepare("UPDATE naql SET  gpsid = ?, serviceid = ? , father = ?  WHERE carid = ?");
       $sql->bindParam(1,$gpsid);
       $sql->bindParam(2,$serviceid);
       $sql->bindParam(3,$father);
       $sql->bindParam(4,$carid);
       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>يوجد خطأ في تسجيل البيانات ... راجع  من فضلك455</b>
             </div>";} 
    }
?>
