<?php
$end   = date("Y-m-d");
$start = date('Y-m-d', strtotime($end. ' + 15 days'));
?>
<div  class="panel panel-success" id="main_supp">
  <div class="panel-heading clearfix"style="font-size:1.2em;">
  <div class="row" style="padding:4px;">
      <div class="col-md-3"style="float:right;">
        <label class="control-label"style="float:right;font-size: 16px">هيئة النقل</label>
      </div>
      <div class="col-md-1"style="float:left;">
      <a id="nn_sr" href=""target="_blank"><button type="button" style="float:right;  margin-right:8px; color: #000 ;" class="btn btn-success">تحديث</button></a>
    </div>
    <div class="col-md-2"style="float:left;">
    <input type="text" id="nn" name="nn" class="form-control"  style="float:left; margin-right:8px; color: #000 ;" placeholder="ابحث بالرقم">
  </div>
    <div class="col-md-2"style="float:left;">
    <input type="text" id="serial" name="serial" class="form-control" onchange="serialChange()"  style="float:left; margin-right:8px; color: #000 ;" placeholder="الرقم التسلسلي">
  </div>
    </div>
    <br />
  <form id="update7" method="post" class="form-horizontal" style="float:center;">
    <div class="row" style="padding:4px;">
        <div class="col-md-2"style="float:right;padding:2px;">
          <div class="input-group">
           <span class="input-group-addon" style="font-size:14px;font-weight:bold;background:transparent;color:black;" >فرع</span>
           <select  class="form-control" id="branch" name="branch">
           <option value= "*" >جميع الفروع </option>
           <?php
            include_once('../op/car_history.php');
            naql_cars();
           include "../actions/dbPDO.php";
           $sql3 = $db->prepare("SELECT branchID , b_name from branches where branchID > 1") ;
           $sql3->execute();
           if($sql3->rowCount()>0)
           {
            while($row = $sql3->fetch()){
                  $id     = $row["branchID"];
                  $label  = $row["b_name"];
           echo "<option value= $id >$label</option>";
              }} ?>
           </select>
         </div>
        </div>
        <div class="col-md-2"style="float:right;padding:2px;">
          <div class="input-group">
           <span class="input-group-addon" style="font-size: 14px;font-weight:bold;background:transparent;color:black;" >موديل</span>
           <select  class="form-control" id="model" name="model">
           <option value= "*" >جميع الموديلات</option>
           <?php
           $sql3 =$db->prepare("SELECT DISTINCT model from cars where car_job_id = 1 and branch > 1 ORDER BY model DESC") ;
           $sql3->execute();
           if($sql3->rowCount()>0)
           {
            while($row = $sql3->fetch()){
                  $id     = $row["model"];
                  $label  = $row["model"];
           echo "<option value= $id >$label</option>";
              }} ?>
           </select>
         </div>
        </div>
        <div class="col-md-5"style="float:right;padding:2px;margin-right:10px;">
            <div class="input-group">
             <span class="input-group-addon" style="font-size:14px;font-weight:bold;background:transparent;color:black;" >الشركات المالكة</span>
             <select  class="form-control" id="woner" name="woner">
             <option value= "*" >جميع الشركات</option>
             <?php
             $sql3 =$db->prepare("SELECT supp_id , supp_name from supplycomp ORDER BY supp_name") ;
             $sql3->execute();
             if($sql3->rowCount()>0)
             {
              while($row = $sql3->fetch()){
                    $id     = $row["supp_id"];
                    $label  = $row["supp_name"];
             echo "<option value= $id >$label</option>";
                }} ?>
             </select>
           </div>
          </div>
          
          <div class="col-md-2"style="float:right;padding:2px;">
            <div class="input-group">
             <span class="input-group-addon" style="font-size: 14px;font-weight:bold;background:transparent;color:black;" >طراز</span>
             <select  class="form-control" id="kind" name="kind">
             <option value= "*" >جميع الأنواع</option>
             <?php
             $sql3 =$db->prepare("SELECT DISTINCT kind from cars where car_job_id = 1 and branch > 1 ORDER BY kind") ;
             $sql3->execute();
             if($sql3->rowCount()>0)
             {
              while($row = $sql3->fetch()){
                    $id     = $row["kind"];
                    $label  = $row["kind"];
             echo "<option value='$id' >$label</option>";
                }} ?>
             </select>
           </div>
          </div>
      </div>
      <div class="row" style="padding:4px;">
          <div class="col-md-2 form-check"style="float:right;padding:2px;margin-right:20px;">
                <input type='hidden' value='0' name='insurance_expire'>
              <input type="checkbox" style="padding-top:20px;" class="form-check-input" id="insurance_expire" name="insurance_expire" value="ins" >
              <label class="form-check-label" style="padding-top:20px;" for="insurance_expire">تأمين منتهي</label>
          </div>
          <div class="col-md-2 form-check"style="float:right;padding:2px;margin-right:20px;">
              <input type='hidden' value='0' name='lic_expire'>
              <input type="checkbox" style="padding-top:20px;" class="form-check-input" id="lic_expire" name="lic_expire" value="lic">
              <label class="form-check-label" style="padding-top:20px;" for="lic_expire"> ترخيص منتهي</label>
          </div>
          <div class="col-md-2 form-check"style="float:right;padding:2px;margin-right:20px;">
              <input type='hidden' value='0' name='operation_expire'>
              <input type="checkbox" style="padding-top:20px;" class="form-check-input" id="operation_expire" name="operation_expire" value="oper"> 
              <label class="form-check-label" style="padding-top:20px;" for="operation_expire">كارت تشغيل منتهي</label>
          </div>
          <div class="col-md-1 form-check"style="float:right;padding:2px;margin-right:20px;">
              <input type='hidden' value='0' name='pre_expire'>
              <input type="checkbox" style="padding-top:20px;" class="form-check-input" id="pre_expire" name="pre_expire" value="pre" >
              <label class="form-check-label" style="padding-top:20px;" for="pre_expire">فحص</label>
          </div>
          <div class="col-md-1" style="float:right;padding:2px;">
              <input type="text" class="form-control" style="float:right;padding:2px;" id="at" name="at" value="<?php echo $start ; ?>" readonly />
            <script>
                 $(function() {
                  var calendar = $.calendars.instance('');
                  $('#at').calendarsPicker({calendar: calendar});
               });
            </script>
        </div>
          <div class="col-md-1"style="float:right;padding:2px;margin-right:20px;">
            <button class="btn btn-danger btn-block" id="search">بحث </button>
          </div>
          <div class="col-md-1"style="float:right;padding:2px;margin-right:20px;">
            <button class="btn btn-success btn-block" id="rep">تقرير</button>
          </div>
        </div>
</form>
    </div>
  <div class="panel-body"  style="background-color:#00324d;color:#cce5ff; ">
    <div class="table-responsive">
         <table class="table table-bordered table-hover">
           <thead>
              <tr>
                   <th class="text-center" width="5%">م</th>
                   <th class="text-center" width="10%">حركة</th>
                   <th class="text-center" width="14%">طراز</th>
                   <th class="text-center" width="10%">لوحة</th>
                   <th class="text-center" width="8%">الموديل</th>
                   <th class="text-center" width="13%">نهاية التأمين</th>
                   <th class="text-center" width="10%">نهاية الترخيص</th>
                   <th class="text-center" width="13%">كارت التشغيل</th>
                   <th class="text-center" width="13%">الفحص الدوري</th>
              </tr>
        </thead>
        <tbody id="myData">
                <?php
                $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 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 ; }

                   $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;
                  ?>
              </tbody>
						<tfoot></tfoot>
          </table>
    </div>
</div>
</div>
<!-- panel body style -->
<?php
$outcar = array();
    $cars_sr = $db->prepare("SELECT carid , concat(letters,' ', numbers) as plate FROM cars 
    where carid in (SELECT carid from naql) ORDER by numbers ");
    $cars_sr->execute();
    $i=0;
    if($cars_sr->rowCount()>0)
      {
         while($row = $cars_sr->fetch())
         { $outcar[$i] = array(
                           "value" => $row["plate"],
                           "data" => $row["carid"]
                           );$i++ ;
           }  }
    else {  $outcar[0]= 'لا يوجد '; }
?>
<script>
$(document).ready(function(){
  $("#search").click(function(event){
              event.preventDefault();
          var at = $("#at45").val();
          var formdata = new FormData
        formdata = $("#update7").serialize() ;
        // formdata.append("at": at);
        // alert(formdata) ;
      $.ajax({
      url       :"naql/naql_search.php",
      method  	: "POST",
      data    	:	formdata,
      success 	: function(data){
       $('#myData').html(data);
     }});
});

$("#rep").click(function(event){
          event.preventDefault();
    var b = $("#branch").val();
    var m = $("#model").val();
    var w = $("#woner").val();
    var k = $("#kind").val();
    var ins = 0;
    if($("#insurance_expire").is(":checked")){ ins = "ins";}
    var lic =  0;
    if($("#lic_expire").is(":checked")){ lic = "lic";}
    var oper =  0;
    if($("#operation_expire").is(":checked")){ oper = "oper";}
    var pre = 0;
    if($("#pre_expire").is(":checked")){ pre = "pre";}

  window.open("naql/naql_rep.php?b="+b+"&m="+m+"&w="+w+"&k="+k+"&ins="+ins+"&lic="+lic+"&oper="+oper+"&pre="+pre   , '_blank');
  
    });

var arr=  JSON.parse('<?php echo json_encode($outcar) ; ?>');
$('#nn').autocomplete({
      lookup: arr,
      maxHeight : 200,
      onSelect: function(suggestion) {
        $('#nn_sr').attr("href","naql/naql_update.php?id="+suggestion.data);
      //  one_invoice(suggestion.data);
            },
        })
});

function serialChange(){
  var serial = $('#serial').val();
  // alert(serial);
  $('#nn_sr').attr("href","naql/naql_update.php?id=0&sr="+serial);
}
</script>
