<?php
require_once "../magic/session_config.php";
bdr_start_session();
ob_start();
$branch = $_SESSION['branch'];
  require_once "../op/wanted.php";
  require_once "../magic/DataBase.php";
  $database = new DataBase();

  $conn = $database->getConnection();
$tot_wanted = 0 ;
if (isset($_POST["ad_a"])) {
  $ad_a = $_POST["ad_a"];
  $output = '';
  $ad_a = "%" . $ad_a . "%";
  $output = '';
  $nn = 0;

  $sql = $conn->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.branch , contracts.open_at ,  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 = ?
         AND  cars.kind like ?  order by contracts.contract_id");
  $sql->bindParam(1, $branch);
  $sql->bindParam(2, $ad_a);
}
// customer name
if (isset($_POST["name"])) {
  $namm = $_POST["name"];

  $output = '';
  $namm2 = "%" . $namm . "%";

  $output = '';
  $nn = 0;

  $sql = $conn->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.branch , contracts.open_at ,  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 = ?
          AND  customers.cust_name like ?  order by contracts.contract_id");
  $sql->bindParam(1, $branch);
  $sql->bindParam(2, $namm2);}
  //// cars model
if (isset($_POST["numb"])) {
  $numb = $_POST["numb"];
  $output = '';
  $numb2 = "%" . $numb . "%";

  $output = '';
  $nn = 0;

  $sql = $conn->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.branch , contracts.open_at ,  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 = ?
           AND  cars.model like ?
          order by contracts.contract_id");
  $sql->bindParam(1, $branch);
  $sql->bindParam(2, $numb2);
}
// date
if (isset($_POST["datt"])) {
  $datt = $_POST["datt"];

  $output = '';
  $nn = 0;

  $sql = $conn->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.branch , contracts.open_at,  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 = ?
        AND  contracts.date_out < ?
       order by contracts.contract_id");
  $sql->bindParam(1, $branch);
  $sql->bindParam(2, $datt);
}
          $sql->execute();
               if($sql->rowCount()>0)
              { 
     while($row = $sql->fetch())
        { $nn++;
                       $contract_id     = $row['contract_id'];
                       $wanted       = new  Wanted($conn,$contract_id) ;
                          $pay       = $wanted->wanted() ;
                          $ww        = $pay["wanted"] ;
                          $days_qty  = $pay["days_qty"] ;
                          $payed     = $pay["payed"];

                          $open_at   = $row["open_at"] ;
                          
                          $bt_style = "btn-danger";
                          if(($open_at != $branch)||$open_at ==5){
                            $bt_style = "btn-info";
                          }
                           

                       $date1 = new DateTime($row['tamm_expire']);
                      $date2 = new DateTime(date("Y-m-d"));
                      $qty = $date2->diff($date1)->format("%a");
                      $carcheck = $row['carcheck'];
          if($ww > 1000){
              $style = 'style="color:#DA2D07; background-color:#F9F5F5;font-weight:bold;"';}
          else if(($ww < 1000)&&($ww >500)){
              $style = 'style="color:#420000; background-color:#F6E9CA;font-weight:bold;"';}
        else{$style ='';}
         if (!empty($carcheck)) {
      $output .= '<tr><td>' . $nn . '</td>';
    } else {
      $output .= '<tr><td style="color:#DA2D07; background-color:#F0F7E4;font-weight:bold;">' . $nn . '</td>';
    }
              $output .='<td>
                       <a href="rent/cont2close.php?id=' . $contract_id . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn '.$bt_style.' 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" >' . $days_qty . '</td>
                                  <td class="text-center" >' . $row['day_cost'] . '</td>
                                  <td class="text-center" >' . $payed . '</td> ';

          if(($qty == 0)||($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="13">لا يوجد سجلات ...</td></tr>';  }
  echo $output;
