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

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

include "../op/wanted.php";
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        	<title>متابعة العقود السارية</title>
		 <link rel="icon" href="../perfect.ico">
         <link rel="stylesheet" href="../dist/css/bootstrap.min.css"/>
      	 <link rel="stylesheet" href="../dist/css/day.css" />
	 </head>
	 <!-- onLoad="javscript:window.close( window.print() )" -->
    <body>
			<div class="container-fluid">
			<div class="row" style="padding:5 ;">
				<div class="col-xs-10"style="float:right;">
					<h6 style="font-weight:bold;padding:3px;">كشف حصر العقود السارية </h6>
				</div>
				<div class="col-xs-2"style="float:left;">
        <h6 style="padding:3px;"><?php echo(date("Y-m-d")); ?> </h6>
				</div>
			</div>
			<br />
			<div class="table-responsive">
				<table class="table table-bordered">
        <thead>
              <tr>
                   <th class="text-center" width="4%">م</th>
                   <th class="text-center" width="6%">رقم العقد</th>
                   <th class="text-center" width="33%">الإسم</th>
                   <th class="text-center" width="8%">متأخرات</th>
                   <th class="text-center" width="10%">الجوال</th>
                   <th class="text-center" width="11%">فتح العقد</th>
                   <th class="text-center" width="9%">طراز</th>
                   <th class="text-center" width="6%">رقم</th>
                   <th class="text-center" width="8%">موديل</th>
                    <th class="text-center" width="5%">يومي</th>
              </tr>
            </thead>
            <tbody>
                <?php
                $output = '';
                  $sql =$db->prepare("SELECT customers.cust_name ,customers.phone,cars.kind , cars.numbers , cars.model , contracts.day_cost , contracts.tamm_expire ,
                     contracts.contract_id , contracts.date_out from customers , contracts , cars
                     WHERE contracts.cust_id = customers.cust_id 
                     AND contracts.carid = cars.carid AND contracts.branch !=5
                      AND contracts.cont_status = 1 order by contracts.contract_id");
                  $sql->execute();
                   if($sql->rowCount()>0)
                  {   $tot_wanted = 0 ; $nn = 0;
                      while($row = $sql->fetch())
                      { $nn++;
                        $contract_id = $row['contract_id'];
                        $wanted = new  Wanted($db,$contract_id) ;
                           $pay = $wanted->wanted() ;
                           $ww  = (int)$pay["wanted"] ;
                           $date1 = new DateTime($row['tamm_expire']);
                   				$date2 = new DateTime(date("Y-m-d"));
                   				$qty = $date2->diff($date1)->format("%a");
                           $output .='<tr><td>' . $nn . '</td>';
               $output .='<td>' . $contract_id . '</td>
                           <td>' . $row['cust_name'] . '</td>
                           <td style="font-weight:bold;">' . $ww . '</td>';
                   $output .= '
                                <td>' . $row['phone'] . '</td>
                                <td class = "table-info">' . $row['date_out'] . '</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> </tr> ';
                       }
                  }
          else
              {  $output .= '<tr><td colspan="9">لا يوجد سجلات ...</td></tr>';  }
          echo $output;        ?>
              </tbody>
  </div>
</div>
  <script src="../js/jquery.js"></script>
	<script src="../dist/js/umd/popper.min.js"></script>
	<script src="../dist/js/bootstrap.min.js"></script>
</body>
</html>