<?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_after/' . $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";
$req_id      = $_POST['req_id'];
$feedback    =$_POST['feedback'];
$done        =$_POST['done'];
$date_done   =$_POST['date_done'];
$next_kmm    =$_POST['next_kmm'];
$kmm         =$_POST['kmm'];
$photo       = $_FILES['photo_after'];

$Number = '/^[0-9]+(\.[0-9]{1,2})?$/' ;
if(!empty($feedback)){ $feedback = htmlspecialchars(strip_tags($feedback)) ;   }else{$feedback = null ;}
if(empty($date_done)){
      $date_done = date("Y-m-d");
}
if(empty($kmm)){
    $kmm = 150 ;
}
if(!empty($next_kmm)){
    if(!preg_match($Number,$next_kmm)){
      echo "
      <div class='alert alert-warning'>
      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
      <b> تأكد من الرقم !! $next_kmm قراءة العداد ارقام صحيحة فقط</b>
      </div>
    ";
      exit();
    }
    if($next_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($next_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{$next_kmm = $kmm + 3500; }
  if (!empty($_FILES['photo_after'])){$photo_after = upload_file($photo,$req_id);}
  else{$photo_after = null ;}

  $sql = $db->prepare(" UPDATE ment_request SET feedback = ?, done =?, date_done = ?, next_kmm = ? ,
     photo_after = ? , tech = ? where req_id = ?"  );
   $sql->bindParam(1,$feedback);
   $sql->bindParam(2,$done);
   $sql->bindParam(3,$date_done);
   $sql->bindParam(4,$next_kmm);
   $sql->bindParam(5,$photo_after);
   $sql->bindParam(6,$father);
   $sql->bindParam(7,$req_id);
   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>
  ";}
