<?php
if (!isset($_SESSION)) {	session_start();}
ob_start();
$father = $_SESSION["user_id"];
$file='';
function upload_file($file,$my)
{
  $new_name="";
    if(isset($file["name"]))
          {
           $name = $file["name"];
           $size = $file["size"];
           $ext = explode('.', $file["name"]);
           $allowed_ext = array("png", "jpg", "jpeg");
           error_reporting(0);
           if(in_array($ext[1], $allowed_ext))
           {
             if($size < (2000*2000))
              {
                 $new_image = '';
                 $new_name = $my. '.' . $ext[1];
                 $destination = '../photo/ment_before/' . $new_name;
                 list($width, $height) = getimagesize($file["tmp_name"]);
               if($ext[1] == 'png')
                 {  $new_image = imagecreatefrompng($file["tmp_name"]); }
               if($ext[1] == 'jpg' || $ext[1] == 'jpeg')
                      {   $new_image = imagecreatefromjpeg($file["tmp_name"]);}
                      $new_width=700;
                      $new_height = ($height/$width)*700;
                      $tmp_image = imagecreatetruecolor($new_width, $new_height);
                      imagecopyresampled($tmp_image, $new_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                      $quality = 80 ;
                      if($size < (512*512)){$quality = 70 ;}else if($size < (800*800)){$quality = 60 ;}
                      else if($size < (1030*1030)){$quality = 65 ;}else {$quality = 55 ;}
                      imagejpeg($tmp_image, $destination,$quality);
                      imagedestroy($new_image);
                      imagedestroy($tmp_image);
                      echo '<img src="'.$destination.'" />';

             }else { echo 'الصورة اكبر من 1.5 ميجا من فضلك اختار صورة أقل';}
           }
          //  else{ echo 'يجب إختيار صورة ..';  }
        }
        // else { echo 'من فضلك إختار صوره'; }
          return $new_name;
     }

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

$car_id        = $_POST['car_id'];
$ask_date      = $_POST['ask_date'];
$kmm           = $_POST['kmm'];
$description   = $_POST['description'];
$photo         = $_FILES['photo_before'];

$Number = '/^[0-9]+(\.[0-9]{1,2})?$/' ;

if(empty($car_id) || empty($description))
{
  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($description)){ $description = htmlspecialchars(strip_tags($description)) ;   }

if(empty($ask_date)){
      $ask_date = date("Y-m-d");
}
if(!empty($kmm)){
    if(!preg_match($Number,$kmm)){
      echo "
      <div class='alert alert-warning'>
      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
      <b> تأكد من الرقم !! $kmm قراءة العداد ارقام صحيحة فقط</b>
      </div>
    ";
      exit();
    }
    if($kmm< 10){
      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();
    }
    if((strlen($kmm) > 6)){
      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{
  $km = $db->prepare("select km_in  from contracts where carid = ?
                        AND date_in = (select max(date_in) from contracts where carid = ?)");
  $km->bindParam(1,$car_id);
  $km->bindParam(2,$car_id);
  $km->execute();
  if($km->rowCount()>0){$do = $km->fetch(PDO::FETCH_ASSOC);
  $kmm1   		= $do["km_in"];
  }
  $km2 = $db->prepare("SELECT max(current_km) as current_km from car_in where carid = ? ");
  $km2->bindParam(1,$car_id);
  $km2->execute();
  if($km2->rowCount()>0){$do2 = $km2->fetch(PDO::FETCH_ASSOC);
  $kmm2   		= $do2["current_km"];
  }
  if($kmm1 > $kmm2){$kmm = $kmm1 ;}else{$kmm = $kmm2 ;}
}

// max dd

 $maxsql= $db->prepare("SELECT max(req_id) as maxd FROM ment_request");
  $maxsql->execute();
  if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
      $maxD = $row['maxd']+1;} else {$maxD = 1 ; }
  // if(isset($_FILES['photo_before'])){$photo_before = upload_file($photo,$maxD);}
  if (!empty($_FILES['photo_before'])){$photo_before = upload_file($photo,$maxD);}
  else{$photo_before = "" ;}

  $sql = $db->prepare(" INSERT INTO ment_request
          (req_id, car_id, ask_date, kmm , description, photo_before ,father)
          VALUES (?, ?, ?, ?, ? , ?, ?)");
   $sql->bindParam(1,$maxD);
   $sql->bindParam(2,$car_id);
   $sql->bindParam(3,$ask_date);
   $sql->bindParam(4,$kmm);
   $sql->bindParam(5,$description);
   $sql->bindParam(6,$photo_before);
   $sql->bindParam(7,$father);
   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>
  ";}
}
?>
