<?php
require_once "../op/CarHistory.php";

require_once "../magic/DataBase.php";
 $database = new DataBase();

$conn = $database->getConnection();

$carHistory = new CarHistory($conn);

 if(isset($_POST["branch"])){
      $branch     = $_POST["branch"];
      $status     = $_POST["status"];
      $model      = $_POST["model"];
      $kind       = $_POST["kind"];
  $output = '';
  $sql = "";
    if($branch != "*"){ $sql = $sql." AND branch = ".$branch ;}
    if($status != "*"){$sql = $sql." AND carstatus = ".$status ;}
    if($kind != "*"){$sql = $sql." AND kind LIKE '$kind%'" ;} 
    if($model != "*"){$sql = $sql." AND model = ".$model ;}

  $valed ="text-decoration:none;color:#B6F5AC;";
  $v2 =" background-color:rgba(0,0,0,0.55);";
  $expire = "text-decoration:none;color:#AA0202;";
  $x2 = "background-color:#F0F7E4;";

    $sql = $conn->prepare("SELECT naql.carid, cars.branch, cars.kind,cars.color, concat(cars.letters ,' ',cars.numbers) as numbers, cars.model,cars.lic_photo ,cars.lic_expire, cars.carstatus , naql.insurance_expire, 
                      naql.insurance_lic,naql.operationCard, naql.pre_check ,naql.operation_expire ,naql.pre_expire ,branches.b_name
                  from cars , naql ,branches
                  where cars.carid = naql.carid 
                    AND cars.branch = branches.branchID
                      $sql 
                  AND cars.car_job_id = 1 order by carstatus , model DESC");
    $sql->execute();
  if($sql->rowCount()>0)
    { 
      $nn = 0 ; $qty1 = 0; $qty2 = 0; $qty3 = 0; $qty4 = 0; 

      $date6 = new DateTime(date("Y-m-d"));

       while($row = $sql->fetch())
         { 
           $style="";
          $nn++ ;
          $output .="<tr><td>$nn</td>";
          $carid = $row['carid'];
          $qty   = $carHistory->car_state($carid);
          $date1 = new DateTime($row['insurance_expire']);
          $date2 = new DateTime($row['lic_expire']);
          $date3 = $row['operation_expire'];

           $mybarnch = $row['branch'];
                      if( $mybarnch != $branch)
                        {
                          $style="color:#DA2D07; background-color:#F0F7E4;font-weight:bold;";
                        }

        if(!empty($date3)){
            $date3 = new DateTime($date3);
            $qty3  = $date6->diff($date3)->format("%a");
          }else{
            $date3 ="";
          }
          // $date4 = new DateTime($row['pre_expire']);
          $qty1 = $date6->diff($date1)->format("%a");
          $qty2 = $date6->diff($date2)->format("%a");
         
          // $qty4 = $date6->diff($date4)->format("%a");
     if(($qty1 < 8)||($date1 < $date6)){ $style1 = $expire ; $back1 = $x2 ; }else{ $style1 = $valed ; $back1 = $v2 ; }
     if(($qty2 < 8)||($date2 < $date6)){ $style2 = $expire ; $back2 = $x2 ; }else{ $style2 = $valed ; $back2 = $v2 ;}
     if(($qty3 < 8)||($date3 < $date6)){ $style3 = $expire ; $back3 = $x2 ; }else{ $style3 = $valed ; $back3 = $v2 ;}
    //  if(($qty4 < 8)||($date4 < $date6)){ $style4 = $expire ; $back4 = $x2 ; }else{ $style4 = $valed ; $back4 = $v2 ; }
     $koki = 0 ;
 $stat = $row['carstatus'] ;

if($stat == 2){

  $comy = $conn->prepare("SELECT contract_id  from contracts WHERE cont_status = 1 and carid = ? limit 1 ");
  $comy->bindParam(1,$carid);
  $comy->execute();
  if($comy->rowCount()>0){$do = $comy->fetch(PDO::FETCH_ASSOC);
        $koki      = $do["contract_id"];}
        $output .= '<td>
        <a href="rent/cont2close.php?id='.$koki.'"target="_blank"><button type="button" style="font-weight:bold;font-size:13px;color:black;" class="btn btn-info btn-block btn-xs ">سند قبض</button></a>' ;
      }
else if($stat == 1){
  $output .= '<td>
      <a href="rent/newContract.php?carid='.$carid.'"target="_blank"><button type="button" style="font-weight:bold;font-size:14px;color:black;padding:5px;" class="btn btn-success btn-xs ">فتح عقد</button></a>';
     }
     else if($stat > 2){
       $output .= '<td>
       <a href="#"<button type="button" st="1"  mycar='. $carid .' class="btn btn-danger btn-block btn-xs car_ment " style="font-weight:bold;font-size:13px;color:black;">إستعداد</button></a>';
          }

$output .= '</td>
              <td >' . $qty . '</td>
              <td class="text-center">' . $row['kind'] . '</td>
              <td>' . $row['numbers'] . '</td>
              <td>' . $row['model'] . '</td>
              <td>' . $row['color'] . '</td>
              <td  style="'.$style.'">' . $row['b_name'] . '</td>
              <td class="text-center" style="'.$back1.'" ><a href="naql/insurance_print.php?J=' . $row['insurance_lic'] .'"target="_blank" style="'.$style1.'" >'. $row['insurance_expire'] .'</a></td>
              <td class="text-center" style="'.$back2.'" ><a href="naql/lic_print.php?J=' . $carid . '"target="_blank" style="'.$style2.'">'. $row['lic_expire'] .'</a></td>
              <td class="text-center" style="'.$back3.'"><a href="naql/operation_print.php?J=' . $row['operationCard'] . '"target="_blank" style="'.$style3.'">'. $row['operation_expire'] .'</a></td>
              <td>
                  <a href="a/ment_b_r.php?id='.$carid.' "target="_blank"><button style="font-weight:bold;font-size:13px;color:black;" class="btn btn-danger btn-m ">صيانة</button></a>
                  <a href="ment/car_ment_br.php?id='.$carid.' "target="_blank"><button style="font-size:11px;color:#E5FDCD;" class="btn btn-success btn-m ">ص</button></a>
                  <a href="rent/car_contracts.php?dd='.$carid.' "target="_blank"><button style="font-size:11px;color:#620316;" class="btn btn-info btn-m ">ع</button></a>
              </td>
            </tr>';
         }
      }
  else {  $output .= '<tr><td colspan="12">لا يوجد سجلات ...</td></tr>'; }

            echo $output;
                        }
  ?>