<?php
require_once __DIR__ . '/../magic/auth.php';

$auth = bdr_require_privilege([20, 30], '../logout.php');
$user = $auth['user_id'];
$father = $auth['user_id'];
$branch = $auth['branch'];
$conn = $auth['conn'];
$db = $auth['conn'];

?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        	<title>كشف حصر</title>
		 <link rel="icon" href="../perfect.ico">
         <link rel="stylesheet" href="../dist/css/bootstrap.min.css"/>
      	 <link rel="stylesheet" href="../dist/css/day.css" />
	 </head>
	 <!-- onLoad="javscript:window.close( window.print() )" -->
    <body>
			<div class="container-fluid">
			<div class="row" style="padding:5 ;">
				<div class="col-xs-10"style="float:right;">
					<h6 style="font-weight:bold;padding:3px;">كشف حصر </h6>
				</div>
				<div class="col-xs-2"style="float:right;">
				</div>
			</div>
			<br />
			<div class="table-responsive">
				<table class="table table-bordered">
			<thead>
            <tr>
                   <th class="text-center" width="5%">م</th>
                   <th class="text-center" width="18%">طراز</th>
                   <th class="text-center" width="14%">لوحة</th>
                   <th class="text-center" width="8%">الموديل</th>
                   <th class="text-center" width="13%">نهاية التأمين</th>
                   <th class="text-center" width="13%">نهاية الترخيص</th>
                   <th class="text-center" width="13%">كارت التشغيل</th>
                   <th class="text-center" width="13%">الفحص الدوري</th>
              </tr>
			</thead>
			<tbody >
	<?php
   if(isset($_REQUEST["b"])){
        $branch     = $_REQUEST["b"];
        $woner      = $_REQUEST["w"];
        $kind       = $_REQUEST["k"];
        $model      = $_REQUEST["m"];

        $insurance_expire = $_REQUEST['ins'];
        $lic_expire       = $_REQUEST['lic'];
        $operation_expire = $_REQUEST['oper'];
        $pre_expire       = $_REQUEST['pre'];

        $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 = '';
      $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;-webkit-print-color-adjust: exact !important";
        $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 < 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 ; }
    
    $output .= '<tr><td>'.$nn.'</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=' . $row['lic_photo'] . '"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="8">لا يوجد سجلات ...</td></tr>'; }
  echo $output;
  }
  ?></tbody>
  </div>
</div>
  <script src="../js/jquery.js"></script>
	<script src="../dist/js/umd/popper.min.js"></script>
	<script src="../dist/js/bootstrap.min.js"></script>
</body>
</html>