<?php
include "../actions/dbPDO.php";
if(isset($_POST["branch"])){
    $branch     = $_POST["branch"];
    $woner      = $_POST["woner"];
    $kind       = $_POST["kind"];
    $model      = $_POST["model"];
    // $at         = $_POST["at"];
    $today = date("Y-m-d");
    $at 	= date('Y-m-d', strtotime($today.' + 15 days'));
   
      $insurance_expire = $_POST['insurance_expire'];
      $lic_expire       = $_POST['lic_expire'];
      $operation_expire = $_POST['operation_expire'];
      $pre_expire       = $_POST['pre_expire'];
    
    $sql = "";
      if($branch != "*"){ $sql = $sql." AND branch = ".$branch ;}
      if($woner != "*"){$sql = $sql." AND woner = ".$woner ;}
      if($kind != "*"){$sql = $sql." AND kind LIKE '$kind%'" ;}
      if($model != "*"){$sql = $sql." AND model = ".$model ;}
      if($insurance_expire == "ins"){$sql = $sql." AND (( naql.insurance_expire < DATE_ADD(CURDATE(), INTERVAL 1 MONTH)) OR naql.insurance_expire IS NULL OR naql.insurance_lic IS NULL)" ;}
      if($lic_expire == "lic"){$sql = $sql." AND (( lic_expire < DATE_ADD(CURDATE(), INTERVAL 1 MONTH)) OR lic_expire IS NULL)" ;}
      if($operation_expire == "oper"){$sql = $sql." AND (( operation_expire < DATE_ADD(CURDATE(), INTERVAL 1 MONTH)) OR operation_expire IS NULL  OR operationCard IS NULL)" ;}
      if($pre_expire == "pre"){$sql = $sql." AND ((pre_expire < DATE_ADD(CURDATE(), INTERVAL 1 MONTH)) OR pre_expire IS NULL OR pre_check IS NULL)" ;}
  $output = '';
  // echo $sql;

  $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;";
    $output = '';
    $sql =$db->prepare("SELECT naql.carid, cars.kind, concat(cars.letters ,' ',cars.numbers) as numbers, cars.model,cars.lic_photo ,lic_expire, naql.insurance_expire, 
                        naql.insurance_lic,naql.operationCard,naql.pre_check ,naql.operation_expire ,naql.pre_expire 
                        from cars , naql where cars.carid = naql.carid $sql order by model DESC ");
    $sql->execute();
     if($sql->rowCount()>0)
    { $nn = 0 ; $qty1 = 0; $qty2 = 0; $qty3 = 0; $qty4 = 0; 
       while($row = $sql->fetch())
         { $nn++ ;
          $date1 = new DateTime($row['insurance_expire']);
          $date2 = new DateTime($row['lic_expire']);
          $date3 = new DateTime($row['operation_expire']);
          $date4 = new DateTime($row['pre_expire']);
          $date6 = new DateTime(date("Y-m-d"));
          $qty1 = $date6->diff($date1)->format("%a");
          $qty2 = $date6->diff($date2)->format("%a");
          $qty3 = $date6->diff($date3)->format("%a");
          $qty4 = $date6->diff($date4)->format("%a");
     if(($qty1 < 35)||($date1 < $date6)){ $style1 = $expire ; $back1 = $x2 ; }else{ $style1 = $valed ; $back1 = $v2 ; }
     if(($qty2 < 35)||($date2 < $date6)){ $style2 = $expire ; $back2 = $x2 ; }else{ $style2 = $valed ; $back2 = $v2 ;}
     if(($qty3 < 35)||($date3 < $date6)){ $style3 = $expire ; $back3 = $x2 ; }else{ $style3 = $valed ; $back3 = $v2 ;}
     if(($qty4 < 35)||($date4 < $date6)){ $style4 = $expire ; $back4 = $x2 ; }else{ $style4 = $valed ; $back4 = $v2 ; }
     $carid = $row['carid'] ;
$output .= '<tr><td>'.$nn.'</td>
              <td>
              <a href="naql/naql_update.php?id=' . $carid . '"target="_blank"><button type="button" class="btn btn-success btn-xs ">تحديث</button></a>
              </td>
              <td class="text-center">' . $row['kind'] . '</td>
              <td>' . $row['numbers'] . '</td>
              <td>' . $row['model'] . '</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 class="text-center" style="'.$back4.'"><a href="naql/check_print.php?J=' . $row['pre_check'] . '"target="_blank" style="'.$style4.'">'. $row['pre_expire'] .'</a></td>
            </tr>';
         }
    }
else {  $output .= '<tr><td colspan="9">لا يوجد سجلات ...</td></tr>'; }
  echo $output;
  }
  ?>
