<?php
   require_once "../magic/session_config.php";
bdr_start_session();
  ob_start();
      if(!isset($_SESSION["user_id"])){header("location:../logout.php");}
      $father = $_SESSION["user_id"];
    
      $branch = $_SESSION['branch'];
      $Car = $_REQUEST['dd'] ;
      include "../actions/dbPDO.php";
      include "../op/wanted.php";

      $myCar = $db->prepare("SELECT * from cars where carid = ? ");
      $myCar->bindParam(1,$Car);
      $myCar->execute();
    if($myCar->rowCount()>0){$result = $myCar->fetch(PDO::FETCH_ASSOC);}
?>
    <!DOCTYPE html>
    <html lang="ar" dir="rtl">
    <head>
    	<meta charset="utf-8">
    	<meta name="viewport" content="width=device-width, initial-scale=1.0">
    	<meta name="robots" content="index,follow">
    	<meta name="author" content="سامح دياب">
    	<link rel="icon" href="../images/perfect.ico">
    	<title>عقود السيارة</title>
    	<link rel="stylesheet" href="../dist/css/bootstrap.css"/>
      <link rel="stylesheet" href="../css/font-awesome.min.css"/>
    	<link rel="stylesheet" href="../css/myCar.css"/>
    	<script src="../js/jquery.js"></script>
      <script src="../dist/js/umd/popper.min.js"></script>
    	<script src="../dist/js/bootstrap.js"></script>
    </head>
<body style="background-color:#00324d ; color:#cce5ff;">
  <br />
    <div class="container-fluid">
      <button type="button" class="btn btn-outline-danger btn-sm align-middle" name="close" id="close" ><i style="font-size:14pt ;" class="fa fa-window-close" aria-hidden="true"></i></button>
        <center>
          <h4 style="font-size:20px">عقود سيارة</h4>
        </center>
        <h4 style="text-align:center"><?php echo $result['brand'].' '.$result['kind']; ?> لوحة رقم <?php echo $result['letters'].' '.$result['numbers']; ?> موديل <?php echo $result['model'].' '.$result['color']; ?></h4>
				   <br />
           <table class="table table-bordered table-hover">
             <thead  class="thead-inverse">
                <tr>
                     <th class="align-middle text-center" width="6%">رقم العقد</th>
                      <th class="align-middle text-center" width="5%">متأخرات</th>
                      <th class="align-middle text-center" width="19%">الإسم</th>
                      <th class="align-middle text-center" width="3%">عدد أيام</th>
                      <th class="align-middle text-center" width="5%">إيجار</th>
                      <th class="align-middle text-center" width="5%">إضافي</th>
                      <th class="align-middle text-center" width="45%">بيان التعويض </th>
                      <th class="align-middle text-center" width="8%">من</th>
                      <th class="align-middle text-center" width="4%">إلي</th>
                </tr>
              </thead>
    <tbody id="myData">
        <?php
            $output = '';
          $sql =$db->prepare("SELECT contracts.contract_id , contracts.cust_id ,customers.cust_name ,contracts.date_in , contracts.date_out , contracts.addtion_desc
                       from customers , contracts
                       WHERE contracts.cust_id = customers.cust_id AND contracts.carid = ? order by contracts.date_out DESC LIMIT 12");
                   $sql->bindParam(1,$Car);
                    $sql->execute();
                     if($sql->rowCount()>0)
                    {
                       while($row = $sql->fetch())
                         {
                           $last = $row['date_in'] ;
                           if(!empty($last)){
                           $last = date_create($row['date_in']);
                       $last = date_format($last,"m/d"); }
                          $contract_id  = $row['contract_id'];
                          $wanted       = new  Wanted($db, $contract_id);
                          $pay          = $wanted->wanted();
                          $ww           = $pay["wanted"];
                          $days_qty     = $pay["days_qty"] ;
                          $rent_cost    = $pay["rent_cost"] ;
                          $tot_add      = $pay["tot_addtion"] ;

                    if($ww > 1000){ $output .='<tr><td>
                             <a href="cont2close.php?id=' . $contract_id . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:13px;" class="btn btn-outline-danger btn-sm btn-block ">' . $contract_id . '</button></a>
                             </td> <td style="color:#DA2D07; background-color:#F0F7E4;font-weight:bold;">' . $ww . '</td>';
                             }
                              else
                               { $output .= '<tr><td>
                               <a href="cont2close.php?id=' . $contract_id . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:13px;" class="btn btn-outline-success btn-sm btn-block ">' . $contract_id . '</button></a>
                               </td> <td>' . $ww . '</td>' ;}
                              $output .= '
                                        <td><a href="../a/cust_update.php?id=' . $row['cust_id'] . '"target="_blank" style="color:green;">' . $row['cust_name'] . '</a></td>
                                        <td>' . $days_qty. '</td>
                                        <td>' . $rent_cost. '</td>
                                        <td>' . $tot_add. '</td>
                                        <td class="text-center" >' . $row['addtion_desc'] . '</td>
                                        <td class="text-center" >' . $row['date_out'] . '</td>
                                        <td class="text-center" >' . $last  . '</td> </tr>
                              ';
                         }
                    }
                    else {  $output .= '<tr><td colspan="9">لا يوجد سجلات ...</td></tr>';   }
                    echo $output;
                    ?>
                </tbody>
  						<tfoot></tfoot>
            </table>
  </div>
   <?php
  $outcont = array();
      $cont_sr = $db->prepare("SELECT contract_id , concat(contract_id,' ', numbers) as ko  from active where branch = ? AND cont_status = 1");
      $cont_sr->bindParam(1,$branch) ;
      $cont_sr->execute();
      $i= 0;
      if($cont_sr->rowCount()>0)
        {
           while($row = $cont_sr->fetch())
           {
               $outcont[$i] = array( "value" => $row["ko"],  "data" => $row["contract_id"]  );
               $i++ ;
             }
          }
      else {   $outcont[0]= 'لا يوجد '; }
  ?>
  <!-- <script>
  $(document).ready(function(){
$('#close').click(function(){close();});
    var arr=  JSON.parse('<?php echo json_encode($outcont) ; ?>');
    $('#nn').autocomplete({
     lookup: arr,
     maxHeight : 200,
     onSelect: function(suggestion) {
      $('#nn_sr').attr("href","rent/cont2close.php?id= " + suggestion.data);
           },
      }) ;

      $("#ad_a").click(function(event){
          event.preventDefault();
          var ad_a = $("#ad").val();
     $.ajax({
            url     : "rent/action_flow.php",
            method  : "POST",
             data    :{ad_a:ad_a},
            success : function(data){ $("#myData").html(data); },
            error   :function(result){
              alert(result.statusText);
              }
          });
        });

        $("#namm_a").click(function(event){
            event.preventDefault();
            var name = $("#namm").val();
       $.ajax({
              url     : "rent/action_flow.php",
              method  : "POST",
               data    :{name:name},
              success : function(data){ $("#myData").html(data); },
              error   :function(result){
                alert(result.statusText);
                }
            });
          });
          $("#numb_a").click(function(event){
              event.preventDefault();
              var numb = $("#numb").val();
         $.ajax({
                url     : "rent/action_flow.php",
                method  : "POST",
                 data    :{numb:numb},
                success : function(data){ $("#myData").html(data); },
                error   :function(result){
                  alert(result.statusText);
                  }
              });
            });

        $("#datt_a").click(function(event){
            event.preventDefault();
            var datt = $("#datt").val();
             if(datt.length = 0 ){
               var d = new Date();

             }
       $.ajax({
              url     : "rent/action_flow.php",
              method  : "POST",
               data    :{datt:datt},
              success : function(data){ $("#myData").html(data); },
              error   :function(result){
                alert(result.statusText);
                }
            });
          });
  });

  </script> -->
</body>
</html
