<?php
if(isset($_POST["car_no"])){
include_once("../op/CarHistory.php");
require_once "../op/wanted.php";
require_once "../magic/DataBase.php";

$dataBase = new DataBase();
$conn = $dataBase->getConnection();

$car_no = $_POST["car_no"] ;
$carHistory = new CarHistory($conn);
 $output = '';
  $sql =$conn->prepare("SELECT carid , kind , concat(letters ,' ',numbers) as PL , model , color ,branches.b_name ,carstatus, carstatus.status ,branches.branchID
    from cars , branches ,carstatus
       WHERE cars.branch = branches.branchID AND cars.carstatus = carstatus.status_id AND cars.car_job_id = 1
        AND cars.numbers = ? ");
    $sql->bindParam(1,$car_no);
    $sql->execute();
     if($sql->rowCount()>0)
    {
    while($row = $sql->fetch())
         {
           $carid = $row['carid']; $br = $row['branchID'];
           $qty = $carHistory->car_state($carid);
           $state = $row['carstatus'];
     $output .='<div class="card text-center card-inverse " style="background-color: #1a1a1a; border-color: #333;">
               <div class="card-header">
                  <h4 class="card-title" style="font-weight:bold;style="font-weight:bold;"> سيارة '.$row['kind'].' لوحة رقم '.$row['PL'].'</h4>
              </div>
               <div class="card-block" style="background-color: #333; border-color: #3d5c5c;">
                    <p class="card-text" style="font-weight:bold; font-size:1.3rem;"> موديل <span style="font-weight:bold; font-size:1.7rem; color:#e6ffe6 ;">'. $row['model'] .'</span> اللون <span style="font-weight:bold; font-size:1.7rem;color:#e6ffe6 ;">'. $row['color'] .'</span></p>
                    <p class="card-text" style="font-weight:bold; font-size:1.3rem;"> فرع <span style="font-weight:bold; font-size:1.7rem;color:#e6ffe6 ;">'. $row['b_name'] .'</span> حالة السيارة <span style="font-weight:bold; font-size:1.7rem;color:#e6ffe6 ;">' . $row['status'] .'  '.$qty. ' يوم</span></p>
                </div></div>';
   $output .='<br />';
   $sql2 =$conn->prepare("SELECT customers.cust_name ,customers.phone,customers.phone2,cars.kind , cars.numbers , cars.model , contracts.day_cost ,contracts.contract_id
                        from customers , contracts , cars
                        WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
                        AND contracts.cont_status = 1 AND cars.carid = ? ");
  $sql2->bindParam(1,$carid);
  $sql2->execute();
              if($sql2->rowCount()>0){$ro = $sql2->fetch(PDO::FETCH_ASSOC);
                          $contract_id = $ro['contract_id'];
                          $wanted = new  Wanted($conn,$contract_id) ;
                          $pay = $wanted->wanted() ;
                          $ww  = $pay["wanted"] ;
                          $days_qty = $pay["days_qty"] ;
                          $payed    = $pay["payed"] ;
                          $phone    = $ro['phone']; $phone2    = $ro['phone2'];
  $output .='<div class="card text-center card-inverse " style="background-color: #1a1a1a; border-color: #333;">
             <div class="card-header">
             <h4 class="card-title" style="font-weight:bold;style="font-weight:bold;">'.$ro['cust_name'].'</h4>
             </div>
             <div class="card-block" style="background-color: #333; border-color: #3d5c5c;">' ;

if($ww < 100){ $output .='<p class="card-text" style="font-weight:bold; font-size:1.3rem;"> رقم العقد <span style="font-weight:bold; font-size:1.7rem; color:#e6ffe6 ;">' . $ro['contract_id'] . '</span> المطلوب <span style="color:#DA2D07; background-color:#F0F7E4;font-weight:bold;font-size:1.7rem;padding:3px;">' . $ww . '</span></p>';
        }else{ $output .= '<p class="card-text" style="font-weight:bold; font-size:1.3rem;"> رقم العقد <span style="font-weight:bold; font-size:1.7rem; color:#e6ffe6 ;">' . $ro['contract_id'] . '</span> المطلوب <span style="font-weight:bold; font-size:1.7rem;color:#e6ffe6 ;">' . $ww . '</span></p>';}

        $output .='<p class="card-text" style="font-weight:bold; font-size:1.3rem;"> جوال <a style="font-weight:bold; font-size:1.7rem; color:#e6ffe6 ;" href="tel:'.$phone.'">'. $phone .'</a> جوال2 <a style="font-weight:bold; font-size:1.7rem; color:#e6ffe6 ;" href="tel:'.$phone2.'">'. $phone2 .'</a></p>
                       <p class="card-text" style="font-weight:bold; font-size:1.3rem;">ايام الإيجار <span style="font-weight:bold; font-size:1.7rem;color:#e6ffe6 ;">'.$days_qty.'</span> يومي <span style="font-weight:bold; font-size:1.7rem;color:#e6ffe6 ;">' . $ro['day_cost'] . '</span> المدفوع <span style="font-weight:bold; font-size:1.7rem;color:#e6ffe6 ;">'.$payed .'</span></p>
                       </div></div>';
        $output .='<br />';
  }else{
        $output .= '<p class="card-text text-center" style="font-weight:bold; font-size:1.7rem;">عفوا لا يوجد عقود سارية</p><br />';
        $output .= '<div class="form-row align-items-center" ><div class="col-3">
             							<label for="branch" class="control-label "style="float:left;font-weight:bold; font-size:1.5rem;">الفرع</label>
             							</div><div class="col-5">
             							<select class="form-control" id="branch" name="branch" style="padding:1px;font-weight:bold; font-size:1.3rem;">';

        $sql =$conn->prepare("SELECT branchID , b_name from branches ") ;
        $sql->execute();
    if($sql->rowCount()>0)
        {
    while($row = $sql->fetch()){
          $branchid   = $row["branchID"];
          $b_name     = $row["b_name"];
    if($branchid == $br){$output .= "<option selected value= $branchid >$b_name</option>";}
      else{$output .= "<option value= $branchid >$b_name</option>";}
             											 									}}
        $output .= '</select></div>';
        $output .='<div class="col-3">
        <button type="button" style="font-weight:bold; font-size:1.4rem;" id="branch_btn" name="branch_btn" class="btn btn-success">نقل السيارة</button>
        <input type="hidden" id="car" name="car" value="'.$carid.'" />
        </div></div>';
        $output.='<script>$("#branch_btn").click(function(event){
          event.preventDefault();
          var branch = $("#branch").val();
          var carid = $("#car").val();
          $.ajax({
            url     : "car_br.php",
            method  : "POST",
            data    :{branch:branch,carid:carid},
            success : function(data){
            $("#br_save").html(data);
            $("#br_save").delay(3000).slideUp(function(){  $("#br_save").html(""); });
          },
            error   :function(result){
              alert(result.statusText);
              }
          });
        });</script>
        ';
        
        // status

        $output .= '
        <br /><br />
        <div class="form-row align-items-center" ><div class="col-3">
        <label for="status" class="control-label "style="float:left;font-weight:bold; font-size:1.5rem;">الحالة</label>
        </div><div class="col-5">
        <select class="form-control" id="status" style="padding:1px;font-weight:bold; font-size:1.3rem;">';

  $sql =$conn->prepare("SELECT status_id , status from carstatus ") ;
    $sql->execute();
  if($sql->rowCount()>0)
  {
    while($row = $sql->fetch()){
    $status_id   = $row["status_id"];
    $status       = $row["status"];
if($status_id == $state){$output .= "<option selected value= $status_id >$status</option>";}
else{$output .= "<option value= $status_id >$status</option>";}
                                   }}
$output .= '</select></div>';
$output .='<div class="col-3">
<button type="button" style="font-weight:bold; font-size:1.4rem;" id="status_btn" class="btn btn-success">تغيير الحالة</button>

</div></div>';
$output.='<script>$("#status_btn").click(function(event){
event.preventDefault();
var status = $("#status").val();
var carid = $("#car").val();
$.ajax({
url     : "car_br.php",
method  : "POST",
data    :{status:status,carid:carid},
success : function(data){
$("#br_save").html(data);

},
error   :function(result){
alert(result.statusText);
}
});
});</script>
';
        //
                      }
         }//main while loop
    }  else
    {
         $output .= '<p class="card-text text-center" style="font-weight:bold; font-size:1.7rem;">عفواً لا توجد سيارة بهذا الرقم</p>';
    }
    echo $output ;
    // value="'.$carid.'"
}
