<?php
if (!isset($_SESSION)) {
	session_start();
}
ob_start();
if (!isset($_SESSION["user_id"])) {
	header("location:../logout.php");
}
require_once "../magic/DataBase.php";
require_once "../op/wanted.php";

   $dataBase = new DataBase();
   $conn = $dataBase->getConnection();
 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
        from customers , contracts , cars
       WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
        AND contracts.cont_status = 3 AND  cars.kind like ? AND contracts.branch = ? AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.contract_id  ");
  $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
    from customers , contracts , cars
   WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
    AND contracts.cont_status = 3 AND  cars.kind like ? AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.contract_id  ");
$sql->bindParam(1,$ad_a);
$sql->bindParam(2,$start);
$sql->bindParam(3,$end);
}
  $sql->execute();
   if($sql->rowCount()>0)
   { $nn = 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"] ;
         $tot_add   = $pay["tot_addtion"] ;

        if($ww < 200){ $output .='<tr class="bg-success" style="color:black;  border: 0.5px solid #000000;">'; } else { $output .= "<tr>" ;}
         $output .= '
                   <td>' . $nn . '</td><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 ">' . $contract_id . '</button></a>
                   </td>
                      <td>' . $row['cust_name'] . '</td>
                      <td>' . $ww . '</td>
                      <td>' . $row['phone'] . '</td>
                      <td class="text-center" >' . $days_qty . '</td>
                      <td class = "table-info">' . $row['date_in'] . '</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" >' . $tot_add . '</td>
                   </tr>';
       }
  }
  else { $output .= '<tr><td colspan="10">لا يوجد سجلات ...</td></tr>';  }
  echo $output;
  }
  // customer name
  if(isset($_POST["name"])){
    $namm = $_POST["name"] ;
    $br    = $_POST["br"];
    $start = $_POST["start"] ;
    $end   = $_POST["end"] ;

    // include "../actions/dbPDO.php";
    $output = '';
    $namm2 = "%".$namm."%";
   $output = '';
    //  include "../op/wanted.php";
     if($br> 0 ){
     $sql = $conn->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , cars.numbers , cars.model ,
        contracts.contract_id , contracts.date_in
         from customers , contracts , cars
        WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
         AND contracts.cont_status = 3 AND  customers.cust_name like ? AND contracts.branch = ? AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.contract_id  ");
   $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
     from customers , contracts , cars
    WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
     AND contracts.cont_status = 3 AND  customers.cust_name like ?  AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.contract_id  ");
$sql->bindParam(1,$namm2);
$sql->bindParam(2,$start);
$sql->bindParam(3,$end);
}
   $sql->execute();
    if($sql->rowCount()>0)
    { $nn = 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"] ;
           $tot_add   = $pay["tot_addtion"] ;

          if($ww < 200){ $output .='<tr class="bg-success" style="color:black;  border: 0.5px solid #000000;">'; } else { $output .= "<tr>" ;}
           $output .= '
                     <td>' . $nn . '</td><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 ">' . $contract_id . '</button></a>
                     </td>
                       <td>' . $row['cust_name'] . '</td>
                       <td>' . $ww . '</td>
                       <td>' . $row['phone'] . '</td>
                       <td class="text-center" >' . $days_qty . '</td>
                       <td class = "table-info">' . $row['date_in'] . '</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" >' . $tot_add . '</td>
                    </tr>';
        }
   }
   else { $output .= '<tr><td colspan="10">لا يوجد سجلات ...</td></tr>';  }
   echo $output;
   }
   // cars model
   if(isset($_POST["model"])){
     $model = $_POST["model"] ;
     $br    = $_POST["br"];
     $start = $_POST["start"] ;
     $end   = $_POST["end"] ;
  //  include "../actions/dbPDO.php";
  //  $numb2 = "%".$numb."%";
    $output = '';
      // include "../op/wanted.php";
      if($br> 0 ){
      $sql =$conn->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , cars.numbers , cars.model ,
         contracts.contract_id , contracts.date_in
          from customers , contracts , cars
         WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
          AND contracts.cont_status = 3 AND  cars.model = ? AND contracts.branch = ? AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.contract_id  ");
    $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
      from customers , contracts , cars
     WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
      AND contracts.cont_status = 3 AND  cars.model = ? AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.contract_id  ");
$sql->bindParam(1,$model);
$sql->bindParam(2,$start);
$sql->bindParam(3,$end);
}
    $sql->execute();
     if($sql->rowCount()>0)
     { $nn = 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"] ;
           $tot_add   = $pay["tot_addtion"] ;

          if($ww < 200){ $output .='<tr class="bg-success" style="color:black;  border: 0.5px solid #000000;">'; } else { $output .= "<tr>" ;}
           $output .= '
                     <td>' . $nn . '</td><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 ">' . $contract_id . '</button></a>
                     </td>
                        <td>' . $row['cust_name'] . '</td>
                        <td>' . $ww . '</td>
                        <td>' . $row['phone'] . '</td>
                        <td class="text-center" >' . $days_qty . '</td>
                        <td class = "table-info">' . $row['date_in'] . '</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" >' . $tot_add . '</td>
                     </tr>';
         }
    }
    else { $output .= '<tr><td colspan="10">لا يوجد سجلات ...</td></tr>';    }
    echo $output;
    }

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

    // include "../actions/dbPDO.php";
 $output = '';
  //  include "../op/wanted.php";
   if($br> 0 ){
   $sql =$conn->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , cars.numbers , cars.model ,
      contracts.contract_id , contracts.date_in
       from customers , contracts , cars
      WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
       AND contracts.cont_status = 3  AND contracts.branch = ? AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.contract_id  ");
 $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
     from customers , contracts , cars
    WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
     AND contracts.cont_status = 3   AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.contract_id  ");
$sql->bindParam(1,$start);
$sql->bindParam(2,$end);
}
 $sql->execute();
  if($sql->rowCount()>0)
  { $nn = 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"] ;
         $tot_add   = $pay["tot_addtion"] ;

        if($ww < 200){ $output .='<tr class="bg-success" style="color:black;  border: 0.5px solid #000000;">'; } else { $output .= "<tr>" ;}
         $output .= '
                   <td>' . $nn . '</td><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 ">' . $contract_id . '</button></a>
                   </td>
                     <td>' . $row['cust_name'] . '</td>
                     <td>' . $ww . '</td>
                     <td>' . $row['phone'] . '</td>
                     <td class="text-center" >' . $days_qty . '</td>
                     <td class = "table-info">' . $row['date_in'] . '</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" >' . $tot_add . '</td>
                  </tr>';
      }
 }
 else {  $output .= '<tr><td colspan="10">لا يوجد سجلات ...</td></tr>'; }
 echo $output;
 }
 // branches
 if(isset($_POST["branch"])){
   $br    = $_POST["branch"];
   $start = $_POST["start"] ;
   $end   = $_POST["end"] ;

    //  include "../actions/dbPDO.php";
  $output = '';
    // include "../op/wanted.php";
    if($br > 0){
    $sql =$conn->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , cars.numbers , cars.model ,
       contracts.contract_id , contracts.date_in
        from customers , contracts , cars
       WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
        AND contracts.cont_status = 3  AND contracts.branch = ? AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.contract_id  ");
  $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
    from customers , contracts , cars
   WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
    AND contracts.cont_status = 3   AND contracts.date_in between ? AND ? order by contracts.date_in DESC , contracts.contract_id  ");
$sql->bindParam(1,$start);
$sql->bindParam(2,$end);
}
  $sql->execute();
   if($sql->rowCount()>0)
   { $nn = 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"] ;
         $tot_add   = $pay["tot_addtion"] ;

        if($ww < 200){ $output .='<tr class="bg-success" style="color:black;  border: 0.5px solid #000000;">'; } else { $output .= "<tr>" ;}
         $output .= '
                   <td>' . $nn . '</td><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 ">' . $contract_id . '</button></a>
                   </td>
                      <td>' . $row['cust_name'] . '</td>
                      <td>' . $ww . '</td>
                      <td>' . $row['phone'] . '</td>
                      <td class="text-center" >' . $days_qty . '</td>
                      <td class = "table-info">' . $row['date_in'] . '</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" >' . $tot_add . '</td>
                   </tr>';
       }
  }  else  {  $output .= '<tr><td colspan="10">لا يوجد سجلات ...</td></tr>';  }
  echo $output;
  }
  ?>