<?php
    


 require_once __DIR__ . '/../magic/auth.php';

$auth = bdr_require_privilege(26, '../logout.php');
$user = $auth['user_id'];
$father = $auth['user_id'];
$branch = $auth['branch'];
$conn = $auth['conn'];
$db = $auth['conn'];

require_once "../op/wanted.php";

if(isset($_POST["ad_a"])){
      $ad_a  = $_POST["ad_a"] ;
      $br    = $_POST["br"];
      $start = $_POST["start"] ;
      $end   = $_POST["end"] ;

      $output = '';
      $ad_a = "%".$ad_a."%";
      $output = '';

      if($br> 0 ){
            $sql =$conn->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , cars.numbers , cars.model ,
            contracts.contract_id , contracts.date_in , contracts.days_qty ,  contracts.day_cost , contracts.deduction
            from customers , contracts , cars
            WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
            AND contracts.cont_status = 2 AND  cars.kind like ? AND contracts.branch = ? AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.time_in DESC  ");
      $sql->bindParam(1,$ad_a);
      $sql->bindParam(2,$br);
      $sql->bindParam(3,$start);
      $sql->bindParam(4,$end);
   }else{
      $sql =$conn->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , cars.numbers , cars.model ,
         contracts.contract_id , contracts.date_in , contracts.days_qty ,  contracts.day_cost , contracts.deduction
         from customers , contracts , cars
         WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
         AND contracts.cont_status = 2 AND  cars.kind like ? AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.time_in DESC  ");
      $sql->bindParam(1,$ad_a);
      $sql->bindParam(2,$start);
      $sql->bindParam(3,$end);
// customer name
   }}else if(isset($_POST["name"])){
    $namm = $_POST["name"] ;
    $br    = $_POST["br"];
    $start = $_POST["start"] ;
    $end   = $_POST["end"] ;

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

     if($br> 0 ){
     $sql =$conn->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , cars.numbers , cars.model ,
        contracts.contract_id , contracts.date_in , contracts.days_qty ,  contracts.day_cost , contracts.deduction
         from customers , contracts , cars
        WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
         AND contracts.cont_status = 2 AND  customers.cust_name like ? AND contracts.branch = ? AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.time_in DESC  ");
    $sql->bindParam(1,$namm2);
    $sql->bindParam(2,$br);
    $sql->bindParam(3,$start);
    $sql->bindParam(4,$end);
   }else{
      $sql =$conn->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , cars.numbers , cars.model ,
         contracts.contract_id , contracts.date_in , contracts.days_qty ,  contracts.day_cost , contracts.deduction
         from customers , contracts , cars
         WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
         AND contracts.cont_status = 2 AND  customers.cust_name like ?  AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.time_in DESC  ");
      $sql->bindParam(1,$namm2);
      $sql->bindParam(2,$start);
      $sql->bindParam(3,$end);
   }}else  // cars model
   if(isset($_POST["model"])){
     $model = $_POST["model"] ;
     $br    = $_POST["br"];
     $start = $_POST["start"] ;
     $end   = $_POST["end"] ;

    $output = '';
      if($br> 0 ){
      $sql =$conn->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , cars.numbers , cars.model ,
         contracts.contract_id , contracts.date_in , contracts.days_qty ,  contracts.day_cost , contracts.deduction
          from customers , contracts , cars
         WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
          AND contracts.cont_status = 2 AND  cars.model = ? AND contracts.branch = ? AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.time_in DESC  ");
    $sql->bindParam(1,$model);
    $sql->bindParam(2,$br);
    $sql->bindParam(3,$start);
    $sql->bindParam(4,$end);
  }else{
  $sql =$conn->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , cars.numbers , cars.model ,
     contracts.contract_id , contracts.date_in , contracts.days_qty ,  contracts.day_cost , contracts.deduction
      from customers , contracts , cars
     WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
      AND contracts.cont_status = 2 AND  cars.model = ? AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.time_in DESC  ");
$sql->bindParam(1,$model);
$sql->bindParam(2,$start);
$sql->bindParam(3,$end);
}} else // date

if(isset($_POST["datt"])){
  $br    = $_POST["datt"];
  $start = $_POST["start"] ;
  $end   = $_POST["end"] ;

 $output = '';
   if($br> 0 ){
   $sql =$conn->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , cars.numbers , cars.model ,
      contracts.contract_id , contracts.date_in , contracts.days_qty ,  contracts.day_cost , contracts.deduction
       from customers , contracts , cars
      WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
       AND contracts.cont_status = 2  AND contracts.branch = ? AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.time_in DESC  ");
 $sql->bindParam(1,$br);
 $sql->bindParam(2,$start);
 $sql->bindParam(3,$end);
 } else{
 $sql =$conn->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , cars.numbers , cars.model ,
    contracts.contract_id , contracts.date_in , contracts.days_qty ,  contracts.day_cost , contracts.deduction
     from customers , contracts , cars
    WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
     AND contracts.cont_status = 2   AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.time_in DESC  ");
$sql->bindParam(1,$start);
$sql->bindParam(2,$end);
}} else // branches
 if(isset($_POST["branch"])){
   $br    = $_POST["branch"];
   $start = $_POST["start"] ;
   $end   = $_POST["end"] ;

  $output = '';
    if($br > 0){
    $sql =$conn->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , cars.numbers , cars.model ,
       contracts.contract_id , contracts.date_in , contracts.days_qty ,  contracts.day_cost , contracts.deduction
        from customers , contracts , cars
       WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
        AND contracts.cont_status = 2  AND contracts.branch = ? AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.time_in DESC  ");
  $sql->bindParam(1,$br);
  $sql->bindParam(2,$start);
  $sql->bindParam(3,$end);
}else {
$sql =$conn->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , cars.numbers , cars.model ,
   contracts.contract_id , contracts.date_in , contracts.days_qty ,  contracts.day_cost , contracts.deduction
    from customers , contracts , cars
   WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
    AND contracts.cont_status = 2   AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.time_in DESC  ");
$sql->bindParam(1,$start);
$sql->bindParam(2,$end);
}}

  $sql->execute();
                   if($sql->rowCount()>0)
                  {
                     while($row = $sql->fetch())
                     {
                       $contract_id = $row['contract_id'];
                       $wanted     = new  Wanted($conn, $contract_id);
                       $pay       = $wanted->wanted();
                       $ww        = $pay["wanted"];
                       $tot_addtion   = $pay["tot_addtion"] ;
                       $days_qty  = $pay["days_qty"] ;
                       $payed     = $pay["payed"]  ;

                      $output .= '<tr>
                                    <td>
                                    <a href="jaudge/cont2debt.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 ">' . $row['contract_id'] . '</button></a>
                                    </td>
                                    <td>' . $row['cust_name'] . '</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" >' . $row['day_cost'] . '</td>
                                    <td class="text-center" >' . $days_qty . '</td>
                                    <td>' . $payed . '</td>
                                    <td>' . $tot_addtion . '</td>
                                    <td class = "table-info">' . $row['deduction'] . '</td>
                                    <td class="text-center" >' . $row['date_in'] . '</td>
                                 </tr>
                          ';
                     }
                  }
                  else
                  {
                       $output .= '<tr>
                                           <td colspan="11">لا يوجد سجلات ...</td>
                                      </tr>';
                  }
                  echo $output;

  //endddd
  ?>
