<?php
require_once "../magic/session_config.php";
bdr_start_session();
ob_start();

$branch = $_SESSION['branch'];
$user = $_SESSION["user_id"];
include "../actions/dbPDO.php";
  $master = $db->prepare('SELECT role from users where user_id = ? ');
  $master->bindParam(1, $user);
  $master->execute();
  $ro = $master->fetch(PDO::FETCH_ASSOC);
  $role		  = $ro["role"];

	$contracts_link = "ment/cont2collect.php?id=";
if($role < 4){
	$contracts_link = "ment/cont2close.php?id=";
}else{
  return ;
}
$output = '';
include "../op/wanted.php";
$tot_wanted = 0;
$nn = 0 ;
// start date 
if(isset($_POST["datt"])){
  $datt = $_POST["datt"] ;
   $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);
}
//car kind
 if(isset($_POST["ad_a"])){
   $ad_a = $_POST["ad_a"] ;
  $ad_a = "%".$ad_a."%";
  $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 , contracts.carcheck
       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);
}
  // customer name
  if(isset($_POST["name"])){
    $namm = $_POST["name"] ;
    $namm2 = "%".$namm."%";
     $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 , contracts.carcheck
        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);
}
   // cars model
   if(isset($_POST["numb"])){
     $numb = $_POST["numb"] ;
     $numb2 = "%".$numb."%";
      $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 , contracts.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);
}
 // branches
 if(isset($_POST["br"])){
   $br = $_POST["br"] ;
    $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 , contracts.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) {

   $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'];
     $super_link = $contracts_link.$contract_id ;
     $style_late = '' ;
     $style = '';
     if ($late > 40) {
       $style = 'style="color:#DA2D07; background-color:#d0d0d0;font-weight:bold;"';
       $style_late = $style ;
     } else if (($late < 40) && ($late > 6)) {
       $style = 'style="color:#420000; background-color:#F6E9CA;font-weight:bold;"';
     }

     $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="' . $super_link . '" 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 ' . $style_late . ' >' . $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;

  ?>
