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

 include "../actions/dbPDO.php";
 if(isset($_POST["col_name"])){
$col = $_POST["col_name"];
$val = $_POST["col_val"];
include "../op/co_wanted.php";
$output = '';
$sql =$db->prepare("SELECT customers.cust_name ,customers.phone , cars.kind , cars.numbers , cars.model ,
   contracts.contract_id , contracts.date_in , contracts.days_qty , contracts.addtion_cost , contracts.violations , contracts.ins_claim
      from customers , contracts , cars
          WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
          AND contracts.cont_status = 3 AND cars.$col = ? order by contracts.contract_id   ");
          $sql->bindParam(1,$val);
          $sql->execute();
          if($sql->rowCount()>0)
          {
            while($row = $sql->fetch())
            {
       $ss = $row['contract_id'];
       $wwc =   wanted($ss);
       $ww = $wwc["wanted"] ;
         $add = $row['addtion_cost']; $vio = $row['violations']; $ins = $row['ins_claim'];
         $ddd = $add + $vio + $ins ;
         if($ww < 200){ $output .='<tr class="bg-success" style="color:black;  border: 0.5px solid #000000;">'; } else { $output .= "<tr>" ;}
          $output .= '
                    <td>
                    <a href="ment/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" >' . $row['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" >' . $ddd . '</td>
                 </tr>
          ';
     }
}
else
{
     $output .= '<tr><td colspan="10">لا يوجد سجلات ...</td></tr>';
}
echo $output;
  }
  ?>
