<?php

 if(isset($_POST["ad_a"])){

   $ad_a  = $_POST["ad_a"] ;
   $br    = $_POST["br"];
   $start = $_POST["start"] ;
   $end   = $_POST["end"] ;
     include "../actions/dbPDO.php";
   $output = '';
   $ad_a = "%".$ad_a."%";
  $output = '';
    include "../op/co_wanted.php";
    if($br> 0 ){
    $sql =$db->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 = 4 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 =$db->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 = 4 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)
  {
     while($row = $sql->fetch())
       {
         $ss        = $row['contract_id'];
         $wwc       =   wanted($ss);
         $ww        = $wwc["wanted"] ;
         $tot_add   = $wwc["tot_add"] ;
         $days_qty  =  $wwc["days_qty"] ;
     if($ww < 200){ $output .='<tr class="bg-success" style="color:black;  border: 0.5px solid #000000;">'; } else { $output .= "<tr>" ;}
            $output .= '
                      <td>
                      <a href="jaudge/cont2debt.php?id=' . $ss . '"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>' . $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/co_wanted.php";
     if($br> 0 ){
     $sql =$db->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 = 4 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 =$db->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 = 4 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)
   {
      while($row = $sql->fetch())
        {
          $ss        = $row['contract_id'];
          $wwc       =   wanted($ss);
          $ww        = $wwc["wanted"] ;
          $tot_add   = $wwc["tot_add"] ;
          $days_qty  =  $wwc["days_qty"] ;
      if($ww < 200){ $output .='<tr class="bg-success" style="color:black;  border: 0.5px solid #000000;">'; } else { $output .= "<tr>" ;}
             $output .= '
                       <td>
                       <a href="jaudge/cont2debt.php?id=' . $ss . '"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>' . $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"><h4>لا يوجد سجلات ...</h4></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/co_wanted.php";
      if($br> 0 ){
      $sql =$db->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 = 4 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 =$db->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 = 4 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)
    {
       while($row = $sql->fetch())
         {
           $ss        = $row['contract_id'];
           $wwc       =   wanted($ss);
           $ww        = $wwc["wanted"] ;
           $tot_add   = $wwc["tot_add"] ;
           $days_qty  =  $wwc["days_qty"] ;
       if($ww < 200){ $output .='<tr class="bg-success" style="color:black;  border: 0.5px solid #000000;">'; } else { $output .= "<tr>" ;}
              $output .= '
                        <td>
                        <a href="jaudge/cont2debt.php?id=' . $ss . '"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>' . $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"><h4>لا يوجد سجلات ...</h4></td></tr>';    }
    echo $output;
    }

// date

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

    include "../actions/dbPDO.php";
 $output = '';
   include "../op/co_wanted.php";
   if($br> 0 ){
   $sql =$db->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 = 4  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 =$db->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 = 4   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)
 {
    while($row = $sql->fetch())
      {
        $ss        = $row['contract_id'];
        $wwc       =   wanted($ss);
        $ww        = $wwc["wanted"] ;
        $tot_add   = $wwc["tot_add"] ;
        $days_qty  =  $wwc["days_qty"] ;
    if($ww < 200){ $output .='<tr class="bg-success" style="color:black;  border: 0.5px solid #000000;">'; } else { $output .= "<tr>" ;}
           $output .= '
                     <td>
                     <a href="jaudge/cont2debt.php?id=' . $ss . '"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>' . $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/co_wanted.php";
    if($br > 0){
    $sql =$db->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 = 4  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 =$db->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 = 4   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)
  {
     while($row = $sql->fetch())
       {
         $ss        = $row['contract_id'];
         $wwc       =   wanted($ss);
         $ww        = $wwc["wanted"] ;
         $tot_add   = $wwc["tot_add"] ;
         $days_qty  =  $wwc["days_qty"] ;
     if($ww < 200){ $output .='<tr class="bg-success" style="color:black;  border: 0.5px solid #000000;">'; } else { $output .= "<tr>" ;}
        $output .= ' <td>
                      <a href="jaudge/cont2debt.php?id=' . $ss . '"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>' . $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;
  }
  ?>