<?php
require_once "../magic/session_config.php";
bdr_start_session();
ob_start();
$branch = $_SESSION['branch'];
 if(isset($_POST["ad_a"])){
   $ad_a = $_POST["ad_a"] ;
  include "../actions/dbPDO.php";
   $output = '';
   $ad_a = "%".$ad_a."%";

  $output = ''; $nn = 0;
    include "../op/wanted.php";
    $sql =$db->prepare("SELECT customers.cust_name ,customers.phone,cars.kind , cars.numbers , cars.model , contracts.day_cost , contracts.tamm_expire ,
       contracts.contract_id , contracts.date_out from customers , contracts , cars
       WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
        AND contracts.cont_status = 1   AND  cars.kind like ?
        order by contracts.contract_id");
    $sql->bindParam(1,$ad_a);
  $sql->execute();
   if($sql->rowCount()>0)
   {
    $tot_wanted = 0;
    $backRed = "color:#DA2D07; background-color:#F0F7E4;font-weight:bold;";
    while ($row = $sql->fetch()) {
      $nn++;
      $contract_id = $row['contract_id'];
      $wanted = new  Wanted($db, $contract_id);
      $pay = $wanted->wanted();
      $ww  = $pay["wanted"];
      $late = $pay["late"];
      $date1 = new DateTime($row['tamm_expire']);
      $date2 = new DateTime(date("Y-m-d"));
      $qty = $date2->diff($date1)->format("%a");
      $carcheck = $row['carcheck'];
      if ($late > 45) {
        $style = 'style="color:#DA2D07; background-color:#d0d0d0;font-weight:bold;"';
      } else if (($late < 45) && ($late > 6)) {
        $style = 'style="color:#420000; background-color:#F6E9CA;font-weight:bold;"';
      } else {
        $style = '';
      }
  
      $output .= '<tr>';
      if (!empty($carcheck)) {
        $output .= '<td>' . $nn . '</td>';
      } else {
        $output .= '<td style="color:#DA2D07; background-color:#F0F7E4;font-weight:bold;">' . $nn . '</td>';
      }
  
      $output .= '<td>
                 <a href="acc/cont2close.php?id=' . $contract_id . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-danger btn-xs btn-block ">' . $contract_id . '</button></a>
                 </td> <td>' . $row['cust_name'] . '</td>
                 <td ' . $style . '>' . $ww . '</td>
                 <td>' . $row['phone'] . '</td>
                  <td class = "table-info">' . $row['date_out'] . '</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" >' . $late . '</td>
                  <td class="text-center" >' . $row['day_cost'] . '</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>';
      }
      if ($ww > 0) {
        $tot_wanted += $ww;
      }
    }
  } else {
    $output .= '<tr><td colspan="12">لا يوجد سجلات ...</td></tr>';
  }
  echo $output; 
  }
  // customer name
  if(isset($_POST["name"])){
    $namm = $_POST["name"] ;
   include "../actions/dbPDO.php";
    $output = ''; $nn = 0;
    $namm2 = "%".$namm."%";

   $output = '';
     include "../op/wanted.php";
     $sql =$db->prepare("SELECT customers.cust_name ,customers.phone,cars.kind , cars.numbers , cars.model , contracts.day_cost , contracts.tamm_expire ,
        contracts.contract_id , contracts.date_out from customers , contracts , cars
        WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
         AND contracts.cont_status = 1  AND  customers.cust_name like ?
         order by contracts.contract_id");
     $sql->bindParam(1,$namm2);

   $sql->execute();
    if($sql->rowCount()>0)
    {
      $tot_wanted = 0;
      $backRed = "color:#DA2D07; background-color:#F0F7E4;font-weight:bold;";
      while ($row = $sql->fetch()) {
        $nn++;
        $contract_id = $row['contract_id'];
        $wanted = new  Wanted($db, $contract_id);
        $pay = $wanted->wanted();
        $ww  = $pay["wanted"];
        $late = $pay["late"];
        $date1 = new DateTime($row['tamm_expire']);
        $date2 = new DateTime(date("Y-m-d"));
        $qty = $date2->diff($date1)->format("%a");
        $carcheck = $row['carcheck'];
        if ($late > 45) {
          $style = 'style="color:#DA2D07; background-color:#d0d0d0;font-weight:bold;"';
        } else if (($late < 45) && ($late > 6)) {
          $style = 'style="color:#420000; background-color:#F6E9CA;font-weight:bold;"';
        } else {
          $style = '';
        }
    
        $output .= '<tr>';
        if (!empty($carcheck)) {
          $output .= '<td>' . $nn . '</td>';
        } else {
          $output .= '<td style="color:#DA2D07; background-color:#F0F7E4;font-weight:bold;">' . $nn . '</td>';
        }
    
        $output .= '<td>
                   <a href="acc/cont2close.php?id=' . $contract_id . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-danger btn-xs btn-block ">' . $contract_id . '</button></a>
                   </td> <td>' . $row['cust_name'] . '</td>
                   <td ' . $style . '>' . $ww . '</td>
                   <td>' . $row['phone'] . '</td>
                    <td class = "table-info">' . $row['date_out'] . '</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" >' . $late . '</td>
                    <td class="text-center" >' . $row['day_cost'] . '</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>';
        }
        if ($ww > 0) {
          $tot_wanted += $ww;
        }
      }
    } else {
      $output .= '<tr><td colspan="12">لا يوجد سجلات ...</td></tr>';
    }
   echo $output;
   }
   // cars model
   if(isset($_POST["numb"])){
     $numb = $_POST["numb"] ;
    include "../actions/dbPDO.php";
     $output = ''; $nn = 0;
     $numb2 = "%".$numb."%";

    $output = '';
      include "../op/wanted.php";
      $sql =$db->prepare("SELECT customers.cust_name ,customers.phone,cars.kind , cars.numbers , cars.model , contracts.day_cost , contracts.tamm_expire ,
         contracts.contract_id , contracts.date_out ,carcheck
         from customers , contracts , cars
         WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
          AND contracts.cont_status = 1   AND  cars.model like ?
          order by contracts.contract_id");
      $sql->bindParam(1,$numb2);

    $sql->execute();
     if($sql->rowCount()>0)
     {
      $tot_wanted = 0;
      $backRed = "color:#DA2D07; background-color:#F0F7E4;font-weight:bold;";
      while ($row = $sql->fetch()) {
        $nn++;
        $contract_id = $row['contract_id'];
        $wanted = new  Wanted($db, $contract_id);
        $pay = $wanted->wanted();
        $ww  = $pay["wanted"];
        $late = $pay["late"];
        $date1 = new DateTime($row['tamm_expire']);
        $date2 = new DateTime(date("Y-m-d"));
        $qty = $date2->diff($date1)->format("%a");
        $carcheck = $row['carcheck'];
        if ($late > 45) {
          $style = 'style="color:#DA2D07; background-color:#d0d0d0;font-weight:bold;"';
        } else if (($late < 45) && ($late > 6)) {
          $style = 'style="color:#420000; background-color:#F6E9CA;font-weight:bold;"';
        } else {
          $style = '';
        }
    
        $output .= '<tr>';
        if (!empty($carcheck)) {
          $output .= '<td>' . $nn . '</td>';
        } else {
          $output .= '<td style="color:#DA2D07; background-color:#F0F7E4;font-weight:bold;">' . $nn . '</td>';
        }
    
        $output .= '<td>
                   <a href="acc/cont2close.php?id=' . $contract_id . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-danger btn-xs btn-block ">' . $contract_id . '</button></a>
                   </td> <td>' . $row['cust_name'] . '</td>
                   <td ' . $style . '>' . $ww . '</td>
                   <td>' . $row['phone'] . '</td>
                    <td class = "table-info">' . $row['date_out'] . '</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" >' . $late . '</td>
                    <td class="text-center" >' . $row['day_cost'] . '</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>';
        }
        if ($ww > 0) {
          $tot_wanted += $ww;
        }
      }
    } else {
      $output .= '<tr><td colspan="12">لا يوجد سجلات ...</td></tr>';
    }
    echo $output;
    }
// date
if(isset($_POST["datt"])){
  $datt = $_POST["datt"] ;
 include "../actions/dbPDO.php";
 $output = ''; $nn = 0;
   include "../op/wanted.php";
   $sql =$db->prepare("SELECT customers.cust_name ,customers.phone,cars.kind , cars.numbers , cars.model , contracts.day_cost , contracts.tamm_expire ,
      contracts.contract_id , contracts.date_out ,carcheck
      from customers , contracts , cars
      WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
       AND contracts.cont_status = 1 AND  contracts.date_out < ?
       order by contracts.contract_id");
   $sql->bindParam(1,$datt);

 $sql->execute();
  if($sql->rowCount()>0)
  {
    $tot_wanted = 0;
    $backRed = "color:#DA2D07; background-color:#F0F7E4;font-weight:bold;";
    while ($row = $sql->fetch()) {
      $nn++;
      $contract_id = $row['contract_id'];
      $wanted = new  Wanted($db, $contract_id);
      $pay = $wanted->wanted();
      $ww  = $pay["wanted"];
      $late = $pay["late"];
      $date1 = new DateTime($row['tamm_expire']);
      $date2 = new DateTime(date("Y-m-d"));
      $qty = $date2->diff($date1)->format("%a");
      $carcheck = $row['carcheck'];
      if ($late > 45) {
        $style = 'style="color:#DA2D07; background-color:#d0d0d0;font-weight:bold;"';
      } else if (($late < 45) && ($late > 6)) {
        $style = 'style="color:#420000; background-color:#F6E9CA;font-weight:bold;"';
      } else {
        $style = '';
      }
  
      $output .= '<tr>';
      if (!empty($carcheck)) {
        $output .= '<td>' . $nn . '</td>';
      } else {
        $output .= '<td style="color:#DA2D07; background-color:#F0F7E4;font-weight:bold;">' . $nn . '</td>';
      }
  
      $output .= '<td>
                 <a href="acc/cont2close.php?id=' . $contract_id . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-danger btn-xs btn-block ">' . $contract_id . '</button></a>
                 </td> <td>' . $row['cust_name'] . '</td>
                 <td ' . $style . '>' . $ww . '</td>
                 <td>' . $row['phone'] . '</td>
                  <td class = "table-info">' . $row['date_out'] . '</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" >' . $late . '</td>
                  <td class="text-center" >' . $row['day_cost'] . '</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>';
      }
      if ($ww > 0) {
        $tot_wanted += $ww;
      }
    }
  } else {
    $output .= '<tr><td colspan="12">لا يوجد سجلات ...</td></tr>';
  }
 echo $output;
 }

 // branches
 if(isset($_POST["br"])){
   $br = $_POST["br"] ;
  include "../actions/dbPDO.php";
   $output = '';  $nn = 0;
    include "../op/wanted.php";
    $sql =$db->prepare("SELECT customers.cust_name ,customers.phone,cars.kind , cars.numbers , cars.model , contracts.day_cost , contracts.tamm_expire ,
       contracts.contract_id , contracts.date_out ,carcheck
        from customers , contracts , cars
       WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
        AND contracts.cont_status = 1   AND  contracts.branch = ?
        order by contracts.contract_id");
    $sql->bindParam(1,$br);

  $sql->execute();
   if($sql->rowCount()>0)
   {
    $tot_wanted = 0;
    $backRed = "color:#DA2D07; background-color:#F0F7E4;font-weight:bold;";
    while ($row = $sql->fetch()) {
      $nn++;
      $contract_id = $row['contract_id'];
      $wanted = new  Wanted($db, $contract_id);
      $pay = $wanted->wanted();
      $ww  = $pay["wanted"];
      $late = $pay["late"];
      $date1 = new DateTime($row['tamm_expire']);
      $date2 = new DateTime(date("Y-m-d"));
      $qty = $date2->diff($date1)->format("%a");
      $carcheck = $row['carcheck'];
      if ($late > 45) {
        $style = 'style="color:#DA2D07; background-color:#d0d0d0;font-weight:bold;"';
      } else if (($late < 45) && ($late > 6)) {
        $style = 'style="color:#420000; background-color:#F6E9CA;font-weight:bold;"';
      } else {
        $style = '';
      }
  
      $output .= '<tr>';
      if (!empty($carcheck)) {
        $output .= '<td>' . $nn . '</td>';
      } else {
        $output .= '<td style="color:#DA2D07; background-color:#F0F7E4;font-weight:bold;">' . $nn . '</td>';
      }
  
      $output .= '<td>
                 <a href="acc/cont2close.php?id=' . $contract_id . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-danger btn-xs btn-block ">' . $contract_id . '</button></a>
                 </td> <td>' . $row['cust_name'] . '</td>
                 <td ' . $style . '>' . $ww . '</td>
                 <td>' . $row['phone'] . '</td>
                  <td class = "table-info">' . $row['date_out'] . '</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" >' . $late . '</td>
                  <td class="text-center" >' . $row['day_cost'] . '</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>';
      }
      if ($ww > 0) {
        $tot_wanted += $ww;
      }
    }
  } else {
    $output .= '<tr><td colspan="12">لا يوجد سجلات ...</td></tr>';
  }
  echo $output;
  }
  ?>
