<?php
require_once "../magic/session_config.php";
bdr_start_session();
ob_start();
$branch = $_SESSION['branch'];
include "../op/aksat_wanted.php";
include "../actions/dbPDO.php";
$today = date("Y-m-d");

 if(isset($_POST["ad_a"])){
   $ad_a = $_POST["ad_a"] ;
   $output = '';
   $ad_a = "%".$ad_a."%";
   $sql = $db->prepare("SELECT customers.cust_name ,customers.phone,cars.kind , cars.numbers , cars.model,cars.insurance_expire , aksat_contract.day_cost , aksat_contract.tamm_expire ,
                        aksat_contract.contract_id , aksat_contract.start , aksat_contract.kest
                       from customers , aksat_contract , cars
                       WHERE aksat_contract.cust_id = customers.cust_id AND aksat_contract.carid = cars.carid
                       AND aksat_contract.status = 1 AND aksat_contract.branch = ? 
                       AND  cars.kind like ?
                       order by aksat_contract.contract_id ");
 
   $sql->bindParam(1, $branch);
   $sql->bindParam(2, $ad_a);
   $sql->execute();
   if ($sql->rowCount() > 0) {
    $nn = 0;
     while ($row = $sql->fetch()) {
       
       $nn++ ;
       $ss = $row['contract_id'];
       $wk = next_wanted($ss);
       $tot_debt    = $wk["tot_debt"];
       $late        = $wk["late"];
       $next_ak_id   = $wk["next_ak_id"];
       $next_wanted = $wk["next_wanted"];
       $next_start   = $wk["next_start"];
       $next_ak_no   = $wk["next_ak_no"];
       $insurance_expire   = $row["insurance_expire"];
 
       $date1 = new DateTime($row['tamm_expire']);
       $date2 = new DateTime(date("Y-m-d"));
       $qty = $date2->diff($date1)->format("%a");
       $ins = new DateTime($insurance_expire);
       $ins_qty = $date2->diff($ins)->format("%a");
       if($ins < $date2){
         $style_ins = 'style="color:#DA2D07; background-color:#F9F5F5;font-weight:bold;"';
       }else if($ins_qty < 11){
         $style_ins = 'style="color:#DA2D07; background-color:#F9F5F5;font-weight:bold;"';
       }
       else{
         $style_ins ="";
       }
 
       if ($late > 0) {
         $style = 'style="color:#DA2D07; background-color:#F9F5F5;font-weight:bold;"';
       } else {
         $style = '';
         $tot_debt = $next_wanted;
         $date1 = new DateTime($next_start);
         $date2 = new DateTime($today);
         $late = $date2->diff($date1)->format("%a");
       }
   $output .= '<tr>
                   <td>'.$nn.'</td>
                    <td>
                    <a href="aksat/aksat2pay.php?id=' . $ss . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-danger btn-xs btn-block ">' . $ss . '</button></a>
                    </td>
                    <td>' . $row['cust_name'] . '</td>
                    <td ' . $style . '>' . $tot_debt . '</td>
                    <td>' . $row['phone'] . '</td>
                    <td ' . $style . '>' . $next_start . '</td>
                    <td class="text-center" >' . $row['kind'] . '</td>
                    <td class="text-center" >' . $row['numbers'] . '</td>
                    <td class="text-center" >' . $row['model'] . '</td>
                    <td class="text-center"'.$style_ins.' >' . $insurance_expire . '</td>
                    <td class="text-center" ' . $style . ' >' . $late . '</td>';
       if (($qty < 8) || ($date1 < $date2)) {
         $output .= '    <td class="text-center" style="color:#DA2D07;background-color:#F0F7E4;" >' . $row['tamm_expire'] . '</td>   </tr>';
       } else {
         $output .= '    <td class="text-center">' . $row['tamm_expire'] . '</td>  </tr>';
       }
      
     }
   } else {
     $output .= '<tr><td colspan="12">لا يوجد سجلات ...</td></tr>';
   }
   echo $output;
  }
  // customer name
  if(isset($_POST["name"])){
    $namm = $_POST["name"] ;

    $output = '';
    $namm2 = "%".$namm."%";
   $sql = $db->prepare("SELECT customers.cust_name ,customers.phone,cars.kind , cars.numbers , cars.model,cars.insurance_expire , aksat_contract.day_cost , aksat_contract.tamm_expire ,
                        aksat_contract.contract_id , aksat_contract.start , aksat_contract.kest
                       from customers , aksat_contract , cars
                       WHERE aksat_contract.cust_id = customers.cust_id AND aksat_contract.carid = cars.carid
                       AND aksat_contract.status = 1 AND aksat_contract.branch = ? 
                       AND  customers.cust_name like ?
                       order by aksat_contract.contract_id ");

   $sql->bindParam(1, $branch);
   $sql->bindParam(2,$namm2);
   $sql->execute();
   if ($sql->rowCount() > 0) {
    $nn = 0;
     while ($row = $sql->fetch()) {
       
       $nn++ ;
       $ss = $row['contract_id'];
       $wk = next_wanted($ss);
       $tot_debt    = $wk["tot_debt"];
       $late        = $wk["late"];
       $next_ak_id   = $wk["next_ak_id"];
       $next_wanted = $wk["next_wanted"];
       $next_start   = $wk["next_start"];
       $next_ak_no   = $wk["next_ak_no"];
       $insurance_expire   = $row["insurance_expire"];

       $date1 = new DateTime($row['tamm_expire']);
       $date2 = new DateTime(date("Y-m-d"));
       $qty = $date2->diff($date1)->format("%a");
       $ins = new DateTime($insurance_expire);
       $ins_qty = $date2->diff($ins)->format("%a");
       if($ins < $date2){
         $style_ins = 'style="color:#DA2D07; background-color:#F9F5F5;font-weight:bold;"';
       }else if($ins_qty < 11){
         $style_ins = 'style="color:#DA2D07; background-color:#F9F5F5;font-weight:bold;"';
       }
       else{
         $style_ins ="";
       }

       if ($late > 0) {
         $style = 'style="color:#DA2D07; background-color:#F9F5F5;font-weight:bold;"';
       } else {
         $style = '';
         $tot_debt = $next_wanted;
         $date1 = new DateTime($next_start);
         $date2 = new DateTime($today);
         $late = $date2->diff($date1)->format("%a");
       }
   $output .= '<tr>
                   <td>'.$nn.'</td>
                    <td>
                    <a href="aksat/aksat2pay.php?id=' . $ss . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-danger btn-xs btn-block ">' . $ss . '</button></a>
                    </td>
                    <td>' . $row['cust_name'] . '</td>
                    <td ' . $style . '>' . $tot_debt . '</td>
                    <td>' . $row['phone'] . '</td>
                    <td ' . $style . '>' . $next_start . '</td>
                    <td class="text-center" >' . $row['kind'] . '</td>
                    <td class="text-center" >' . $row['numbers'] . '</td>
                    <td class="text-center" >' . $row['model'] . '</td>
                    <td class="text-center"'.$style_ins.' >' . $insurance_expire . '</td>
                    <td class="text-center" ' . $style . ' >' . $late . '</td>';
       if (($qty < 8) || ($date1 < $date2)) {
         $output .= '    <td class="text-center" style="color:#DA2D07;background-color:#F0F7E4;" >' . $row['tamm_expire'] . '</td>   </tr>';
       } else {
         $output .= '    <td class="text-center">' . $row['tamm_expire'] . '</td>  </tr>';
       }
      
     }
   } else {
     $output .= '<tr><td colspan="12">لا يوجد سجلات ...</td></tr>';
   }
   echo $output;
   }


   // cars model
   if(isset($_POST["numb"])){
     $numb = $_POST["numb"] ;
     $output = '';
    $sql = $db->prepare("SELECT customers.cust_name ,customers.phone,cars.kind , cars.numbers , cars.model,cars.insurance_expire , aksat_contract.day_cost , aksat_contract.tamm_expire ,
                         aksat_contract.contract_id , aksat_contract.start , aksat_contract.kest
                        from customers , aksat_contract , cars
                        WHERE aksat_contract.cust_id = customers.cust_id AND aksat_contract.carid = cars.carid
                        AND aksat_contract.status = 1 AND aksat_contract.branch = ? 
                        AND  ((cars.model =  ?)OR(cars.numbers = ?))
                        order by aksat_contract.contract_id ");
 
    $sql->bindParam(1, $branch);
    $sql->bindParam(2,$numb);
    $sql->bindParam(3,$numb);
    $sql->execute();
    if ($sql->rowCount() > 0) {
     $nn = 0;
      while ($row = $sql->fetch()) {
        
        $nn++ ;
        $ss = $row['contract_id'];
        $wk = next_wanted($ss);
        $tot_debt    = $wk["tot_debt"];
        $late        = $wk["late"];
        $next_ak_id   = $wk["next_ak_id"];
        $next_wanted = $wk["next_wanted"];
        $next_start   = $wk["next_start"];
        $next_ak_no   = $wk["next_ak_no"];
        $insurance_expire   = $row["insurance_expire"];
 
        $date1 = new DateTime($row['tamm_expire']);
        $date2 = new DateTime(date("Y-m-d"));
        $qty = $date2->diff($date1)->format("%a");
        $ins = new DateTime($insurance_expire);
        $ins_qty = $date2->diff($ins)->format("%a");
        if($ins < $date2){
          $style_ins = 'style="color:#DA2D07; background-color:#F9F5F5;font-weight:bold;"';
        }else if($ins_qty < 11){
          $style_ins = 'style="color:#DA2D07; background-color:#F9F5F5;font-weight:bold;"';
        }
        else{
          $style_ins ="";
        }
 
        if ($late > 0) {
          $style = 'style="color:#DA2D07; background-color:#F9F5F5;font-weight:bold;"';
        } else {
          $style = '';
          $tot_debt = $next_wanted;
          $date1 = new DateTime($next_start);
          $date2 = new DateTime($today);
          $late = $date2->diff($date1)->format("%a");
        }
    $output .= '<tr>
                    <td>'.$nn.'</td>
                     <td>
                     <a href="aksat/aksat2pay.php?id=' . $ss . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-danger btn-xs btn-block ">' . $ss . '</button></a>
                     </td>
                     <td>' . $row['cust_name'] . '</td>
                     <td ' . $style . '>' . $tot_debt . '</td>
                     <td>' . $row['phone'] . '</td>
                     <td ' . $style . '>' . $next_start . '</td>
                     <td class="text-center" >' . $row['kind'] . '</td>
                     <td class="text-center" >' . $row['numbers'] . '</td>
                     <td class="text-center" >' . $row['model'] . '</td>
                     <td class="text-center"'.$style_ins.' >' . $insurance_expire . '</td>
                     <td class="text-center" ' . $style . ' >' . $late . '</td>';
        if (($qty < 8) || ($date1 < $date2)) {
          $output .= '    <td class="text-center" style="color:#DA2D07;background-color:#F0F7E4;" >' . $row['tamm_expire'] . '</td>   </tr>';
        } else {
          $output .= '    <td class="text-center">' . $row['tamm_expire'] . '</td>  </tr>';
        }
       
      }
    } else {
      $output .= '<tr><td colspan="12">لا يوجد سجلات ...</td></tr>';
    }
    echo $output;
    }
// date
if(isset($_POST["datt"])){
  $datt = $_POST["datt"] ;
  $output = '';
  $sql = $db->prepare("SELECT customers.cust_name ,customers.phone,cars.kind , cars.numbers , cars.model,cars.insurance_expire , aksat_contract.day_cost , aksat_contract.tamm_expire ,
                       aksat_contract.contract_id , aksat_contract.start , aksat_contract.kest
                      from customers , aksat_contract , cars
                      WHERE aksat_contract.cust_id = customers.cust_id AND aksat_contract.carid = cars.carid
                      AND aksat_contract.status = 1 AND aksat_contract.branch = ? 
                      AND  aksat_contract.start < ?
                      order by aksat_contract.contract_id ");

  $sql->bindParam(1, $branch);
  $sql->bindParam(2,$datt);
  $sql->execute();
  if ($sql->rowCount() > 0) {
   $nn = 0;
    while ($row = $sql->fetch()) {
      
      $nn++ ;
      $ss = $row['contract_id'];
      $wk = next_wanted($ss);
      $tot_debt    = $wk["tot_debt"];
      $late        = $wk["late"];
      $next_ak_id   = $wk["next_ak_id"];
      $next_wanted = $wk["next_wanted"];
      $next_start   = $wk["next_start"];
      $next_ak_no   = $wk["next_ak_no"];
      $insurance_expire   = $row["insurance_expire"];

      $date1 = new DateTime($row['tamm_expire']);
      $date2 = new DateTime(date("Y-m-d"));
      $qty = $date2->diff($date1)->format("%a");
      $ins = new DateTime($insurance_expire);
      $ins_qty = $date2->diff($ins)->format("%a");
      if($ins < $date2){
        $style_ins = 'style="color:#DA2D07; background-color:#F9F5F5;font-weight:bold;"';
      }else if($ins_qty < 11){
        $style_ins = 'style="color:#DA2D07; background-color:#F9F5F5;font-weight:bold;"';
      }
      else{
        $style_ins ="";
      }

      if ($late > 0) {
        $style = 'style="color:#DA2D07; background-color:#F9F5F5;font-weight:bold;"';
      } else {
        $style = '';
        $tot_debt = $next_wanted;
        $date1 = new DateTime($next_start);
        $date2 = new DateTime($today);
        $late = $date2->diff($date1)->format("%a");
      }
  $output .= '<tr>
                  <td>'.$nn.'</td>
                   <td>
                   <a href="aksat/aksat2pay.php?id=' . $ss . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-danger btn-xs btn-block ">' . $ss . '</button></a>
                   </td>
                   <td>' . $row['cust_name'] . '</td>
                   <td ' . $style . '>' . $tot_debt . '</td>
                   <td>' . $row['phone'] . '</td>
                   <td ' . $style . '>' . $next_start . '</td>
                   <td class="text-center" >' . $row['kind'] . '</td>
                   <td class="text-center" >' . $row['numbers'] . '</td>
                   <td class="text-center" >' . $row['model'] . '</td>
                   <td class="text-center"'.$style_ins.' >' . $insurance_expire . '</td>
                   <td class="text-center" ' . $style . ' >' . $late . '</td>';
      if (($qty < 8) || ($date1 < $date2)) {
        $output .= '    <td class="text-center" style="color:#DA2D07;background-color:#F0F7E4;" >' . $row['tamm_expire'] . '</td>   </tr>';
      } else {
        $output .= '    <td class="text-center">' . $row['tamm_expire'] . '</td>  </tr>';
      }
     
    }
  } else {
    $output .= '<tr><td colspan="12">لا يوجد سجلات ...</td></tr>';
  }
  echo $output;
 }
 ?>
