<?php
session_start();
ob_start();
include "dbPDO.php";
// include "../op/upload_photo.php";
$woner      = $_POST["woner"];  $skilton      = $_POST["skilton"]; $letters =$_POST["letters"]; $numbers = $_POST["numbers"];
$brand      = $_POST["brand"];          $kind_use     = $_POST["kind_use"];           $kind              = $_POST["kind"];
$color      = $_POST["color"];          $model        =$_POST["model"];               $lic_expire        = $_POST["lic_expire"];
$insurance_id = $_POST["insurance_id"]; $insurance_no = $_POST["insurance_no"];       $insurance_expire  = $_POST["insurance_expire"];

$costday  = $_POST["costday"];  $monthly = $_POST["monthly"];    $minn = $_POST["minn"];   $start    = $_POST["start"];
$km1      = $_POST["km1"];      $km2     = $_POST["km2"];        $km3  = $_POST["km3"];    $freekilo = $_POST["freekilo"];
$branch   = $_POST["branch"];          $carstatus   = $_POST["carstatus"];          $car_job_id    = $_POST["car_job_id"];

$cont_id    = $_POST["cont_id"];  $woner_permeation = $_POST["woner_permeation"];     $total_cost    = $_POST["total_cost"];
$first_pay  = $_POST["first_pay"];   $kest = $_POST["kest"];     $kest_qty  = $_POST["kest_qty"];  $last_pay  = $_POST["last_pay"];

$comm   = $_POST["comm"];

// $emailValidation = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9]+(\.[a-z]{2,7})$/";
 $Number ='/^[0-9]+(\.[0-9]{1,3})?$/' ;

if(empty($brand) || empty($model)||empty($kind)||empty($letters) || empty($numbers)|| empty($color))
{
  echo "
        <div class='alert alert-warning'>
        <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;' >&times;</a>
        <b>من فضلك  سجل   البيانات الأساسية للسيارة الماركة و اللون و الموديل و حروف و رقم اللوحة ...!</b>
        </div>
  ";
  exit();
}else{
  
  if(!empty($brand)){ $brand = htmlspecialchars(strip_tags($brand)) ;   }
  if(!empty($kind)){ $kind = htmlspecialchars(strip_tags($kind)) ;   }
if(!preg_match($Number,$model)){
  echo "
  <div class='alert alert-warning'>
  <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
  <b>من فضلك راجع .....$model يوجد خطأ في الموديل</b>
  </div>
  ";
  exit();
}
if(!empty($letters)){ $letters = htmlspecialchars(strip_tags($letters)) ;   }
if(!preg_match($Number,$numbers)){
  echo "
  <div class='alert alert-warning'>
  <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
  <b>من فضلك راجع .....$numbers يوجد خطأ في أرقام اللوحة</b>
  </div>
  ";
  exit();
    }
  if(!empty($color)){ $color = htmlspecialchars(strip_tags($color)) ;   }
  if(!empty($kind_use)){ $kind_use = htmlspecialchars(strip_tags($kind_use)) ;   }
  if(!empty($skilton)){
     $skilton = htmlspecialchars(strip_tags($skilton)) ;  

  if((strlen($skilton) < 17)){
      echo "
      <div class='alert alert-warning'>
      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
      <b>رقم الهيكل أقل من 17 حرف و رقم</b>
      </div>
      ";
      exit();
    }
    if((strlen($skilton) > 17)){
      echo "
      <div class='alert alert-warning'>
      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
      <b>رقم الهيكل أكبر من 17 رقم و حرف ....</b>
      </div>
      ";
      exit();
    }
     

    $sql = $db->prepare("SELECT carid , brand , letters , numbers FROM cars WHERE skilton = '$skilton' LIMIT 1");
    $sql->execute();
    if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
          $carid      = $row["carid"];
          $brand      = $row["brand"];
          $letters    = $row["letters"];
          $numbers    = $row["numbers"];
      echo "
      <div class='alert alert-danger'>
      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
      <b>تم تسجيل بيانات $$brand  من قبل كود رقم $carid لوحة رقم $letters  $numbers</b>
      </div>
      ";
      exit();
    }
  }else{ $skilton = null ;}


if(!empty($costday)){
    if(!preg_match($Number,$costday)){
      echo "
      <div class='alert alert-warning'>
      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
      <b>راجع من فضلك  $costday قيمة الإيجار اليومي أرقام فقط </b>
      </div>
      ";
      exit();
    }
}else{
  $costday = 120;
}
if(!empty($monthly)){
    if(!preg_match($Number,$monthly)){
      echo "
      <div class='alert alert-warning'>
      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
      <b>راجع من فضلك  $monthly قيمة الإيجار اليومي أرقام فقط </b>
      </div>
      ";
      exit();
    } }else{ $monthly = 75 ;}
    if(!empty($minn)){
        if(!preg_match($Number,$minn)){
          echo "
          <div class='alert alert-warning'>
          <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
          <b>راجع من فضلك  $minn قيمة الإيجار اليومي أرقام فقط </b>
          </div>
          ";
          exit();
        } }else{ $minn = 75 ;}
    if(!empty($km1)){
            if(!preg_match($Number,$km1)){
              echo "
              <div class='alert alert-warning'>
              <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
              <b>راجع من فضلك  $km1سعر الكيلو أرقام فقط   </b>
              </div>
              ";
              exit();
            } }else{ $km1 = 65 ;}
  if(!empty($km2)){
          if(!preg_match($Number,$km2)){
              echo "
                <div class='alert alert-warning'>
                <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
                <b>راجع من فضلك  $km2 سعر الكيلو أرقام فقط   </b>
                </div>";
                exit();
                    } }else{ $km2 = 45 ;}
    if(!empty($km3)){
         if(!preg_match($Number,$km3)){
                echo "
                      <div class='alert alert-warning'>
                      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
                      <b>راجع من فضلك  $km3 سعر الكيلو أرقام فقط   </b>
                      </div>";
          exit();  } }else{ $km3 = 35 ;}
          if(!empty($freekilo)){
               if(!preg_match($Number,$freekilo)){
                      echo "
                            <div class='alert alert-warning'>
                            <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
                            <b>راجع من فضلك  $freekilo الكيلو المسموح في اليوم أرقام فقط </b>
                            </div>";
                exit();  } }else{ $freekilo = 250 ;}
      
  if(!empty($insurance_no)){
     $insurance_no = htmlspecialchars(strip_tags($insurance_no)) ;}
      else{
         $insurance_no = null;
        }
  if(!empty($cont_id)){ $cont_id = htmlspecialchars(strip_tags($cont_id)) ;   }else{$cont_id = null ;}

  if(!empty($first_pay)){
          if(!preg_match($Number,$first_pay)){
                echo " <div class='alert alert-warning'>
                      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
                      <b>راجع من فضلك  $first_pay الدفعة المقدمة أرقام فقط </b>
                      </div>";
  exit();  } }else{ $first_pay = 0 ;}
  if(!empty($kest)){
        if(!preg_match($Number,$kest)){
              echo " <div class='alert alert-warning'>
                    <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
                    <b>راجع من فضلك  $kest قيمة القسط أرقام فقط </b>
                    </div>";
exit();  } }else{$kest = 0 ;}
if(!empty($kest_qty)){
      if(!preg_match($Number,$kest_qty)){
            echo " <div class='alert alert-warning'>
                  <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
                  <b>راجع من فضلك  $kest_qty عدد الأقساط أرقام فقط </b>
                  </div>";
exit();  } }else{$kest_qty = 36 ;}
if(!empty($last_pay)){
      if(!preg_match($Number,$last_pay)){
            echo " <div class='alert alert-warning'>
                  <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
                  <b>راجع من فضلك  $last_pay الدفعة الأخيرة أرقام فقط </b>
                  </div>";
exit();  } }else{ $last_pay = 0;}
if(!empty($total_cost)){
      if(!preg_match($Number,$total_cost)){
            echo " <div class='alert alert-warning'>
                  <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
                  <b>راجع من فضلك  $total_cost إجمالي سعر السيارة أرقام فقط </b>
                  </div>";
exit();  } }else { $total_cost = 0;}
if(!empty($comm)){ $comm = htmlspecialchars(strip_tags($comm)) ;   }else{ $comm = null ;}
if(empty($woner_permeation)){$woner_permeation = null;}
if(empty($insurance_expire)){$insurance_expire =  null;}

$month_cost = round(($minn * 34.5),3) ;
// existing car number in your database
$sql = $db->prepare("SELECT carid , brand , letters , numbers FROM cars WHERE letters = '$letters' AND numbers = '$numbers' LIMIT 1");
$sql->execute();
if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
      $carid      = $row["carid"];
      $brand      = $row["brand"];
      $letters    = $row["letters"];
      $numbers    = $row["numbers"];
  echo "
  <div class='alert alert-danger'>
  <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
  <b>تم تسجيل بيانات $$brand  من قبل كود رقم $carid لوحة رقم $letters  $numbers</b>
  </div>
  ";
  exit();
}else {
          $maxsql= $db->prepare("SELECT max(carid) as maxd FROM cars");
          $maxsql->execute();
          if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
              $maxCar = $row['maxd']+1;} else {$maxCar = 1 ; }

      $sql=$db->prepare(" INSERT INTO cars
      (carid, brand, kind, model, kind_use , color, letters, numbers,skilton, lic_expire, costday, monthly, minn,
         km1, km2, km3,freekilo , branch, carstatus, car_job_id, insurance_id, insurance_no, insurance_expire,
          woner,cont_id, woner_permeation, first_pay, kest, kest_qty,last_pay, total_cost, start, comm,father,month_cost)
  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?,?,? ,? ,? ,?,?)");
    $sql->bindParam(1,$maxCar);
    $sql->bindParam(2,$brand);
    $sql->bindParam(3,$kind);
    $sql->bindParam(4,$model);
    $sql->bindParam(5,$kind_use);
    $sql->bindParam(6,$color);
    $sql->bindParam(7,$letters);
    $sql->bindParam(8,$numbers);
    $sql->bindParam(9,$skilton);
    $sql->bindParam(10,$lic_expire);
    $sql->bindParam(11,$costday);
    $sql->bindParam(12,$monthly);
    $sql->bindParam(13,$minn);
    $sql->bindParam(14,$km1);
    $sql->bindParam(15,$km2);
    $sql->bindParam(16,$km3);
    $sql->bindParam(17,$freekilo);
    $sql->bindParam(18,$branch);
    $sql->bindParam(19,$carstatus);
    $sql->bindParam(20,$car_job_id);
    $sql->bindParam(21,$insurance_id);
    $sql->bindParam(22,$insurance_no);
    $sql->bindParam(23,$insurance_expire);
    $sql->bindParam(24,$woner);
    $sql->bindParam(25,$cont_id);
    $sql->bindParam(26,$woner_permeation);
    $sql->bindParam(27,$first_pay);
    $sql->bindParam(28,$kest);
    $sql->bindParam(29,$kest_qty);
    $sql->bindParam(30,$last_pay);
    $sql->bindParam(31,$total_cost);
    $sql->bindParam(32,$start);
    $sql->bindParam(33,$comm);
    $sql->bindParam(34,$_SESSION["user_id"]);
    $sql->bindParam(35,$month_cost);
    if($sql->execute()){

      $brandsql= $db->prepare("SELECT brand_id FROM brand where brand_name = ? AND kinds = ?");
      $brandsql->bindParam(1,$brand);
      $brandsql->bindParam(2,$kind);
      $brandsql->execute();
      if(!$brandsql->rowCount()>0){
        $maxsql= $db->prepare("SELECT max(brand_id) as maxd FROM brand");
        $maxsql->execute();
        if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
            $maxbb = $row['maxd']+1;} else {$maxbb = 1 ; }
        $ins = $db->prepare("INSERT into brand values(?,?,?)");
        $ins->bindParam(1,$maxbb);
        $ins->bindParam(2,$brand);
        $ins->bindParam(3,$kind);
        $ins->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>
  ";}
}
}

?>
