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

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

$start 	= $_REQUEST["start"];
	$end 		= $_REQUEST["end"];
include "../op/wanted.php";
		$output = '';
		$sql = $db->prepare("SELECT customers.cust_name ,customers.phone,cars.kind , concat(cars.letters,' ',cars.numbers) as PL ,
		 											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 contract_id ");
		$sql->bindParam(1,$start);
		$sql->bindParam(2,$end);
		$sql->execute();
	if($sql->rowCount()>0)
				{$tot_wanted = 0 ; $no =1;
					$b_name = "";
			while($row = $sql->fetch())
							{
								$contract_id = $row['contract_id'];
								$wanted = new  Wanted($db,$contract_id) ;
								   $pay = $wanted->wanted() ;
								   $ww  = $pay["wanted"] ;
								$output .='<tr style="height:21px;"><td>' . $no . '</td>
																<td>' . $contract_id . ' </td>
												 				<td>' . $row['cust_name'] . '</td>
																<td>' . $ww . '</td>
																<td>' . $row['date_in'] . '</td>
																<td>' . $row['kind'] . '</td>
																<td>' . $row['PL'] . '</td>
																<td>' . $row['model'] . '</td>
																<td>' . $row['phone'] . '</td>
															</tr>';
													$tot_wanted += $ww ; $no += 1;
								 }
								 $output .= '<tr><td colspan="9"></td></tr>';
								 $output .= '<tr>
								 								<td colspan="3">إجمالي متأخرات</td>
																<td colspan="9">'.$tot_wanted.'</td>
								 						 </tr>';
								}
								else {  $output .= '<tr><td colspan="9">لا يوجد سجلات ...</td></tr>';  }

				$myday = date("Y-m-d");
				?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        	<title>العقود المديونة</title>
         <link rel="stylesheet" href="../dist/css/bootstrap.min.css"/>
      	 <link rel="stylesheet" href="../dist/css/day.css" />
	 </head>
    <body onload="javascript: window.print(); setTimeout(window.close, 1000);">
			<div class="container-fluid">
			<div class="form-row">
				<div class="col-10">
					<h6 style="font-weight:bold;padding:3px;font-size:10pt;" >كشف حصر العقود المديونة من <?php echo $start." ".$end ; ?> </h6>
				</div>
				<div class="col-2">
					<?php echo $myday ;?>
				</div>
			</div>
	         <table class="table table-bordered">
	           <thead>
	              <tr>
	                   <th class="align-middle text-center" width="5%">م</th>
										 <th class="align-middle text-center" width="7%">رقم العقد</th>
	                   <th class="align-middle text-center" width="31%">الإسم</th>
	                   <th class="align-middle text-center" width="8%">متأخرات</th>
										 <th class="align-middle text-center" width="10%">إقفال</th>
										 <th class="align-middle text-center" width="11%">طراز</th>
										 <th class="align-middle text-center" width="11%">لوحة</th>
										 <th class="align-middle text-center" width="6%">موديل</th>
	                   <th class="align-middle text-center" width="11%">الجوال</th>
	              </tr>
	            </thead>
	              <tbody>
	                <?php
	                  echo $output;
	                  ?>
	              </tbody>
							<tfoot></tfoot>
	          </table>
		</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>
