<?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'];

$branch = $_REQUEST["br"];
include "../op/wanted.php";
$output = '';
		if ($branch == 5) {
			$sql = $db->prepare("SELECT customers.cust_name ,customers.phone,cars.kind , concat(cars.letters,' ', cars.numbers ) as PL  , cars.model ,  contracts.contract_id ,
			branches.b_name		from customers , contracts , cars , branches
			WHERE contracts.cust_id2 = customers.cust_id AND contracts.carid = cars.carid
			AND contracts.branch = branches.branchID 
			AND contracts.cont_status = 1 
			AND contracts.branch = ? 
			order by contracts.cust_id , contracts.contract_id");
			// 			AND contracts.cust_id = 15824 ,12805 , 8007
		} else {
		$sql = $db->prepare("SELECT customers.cust_name ,customers.phone,cars.kind , concat(cars.letters,' ', cars.numbers ) as PL  , cars.model ,  contracts.contract_id ,
							branches.b_name		from customers , contracts , cars , branches
				WHERE contracts.cust_id = customers.cust_id AND contracts.carid = cars.carid
				AND contracts.branch = branches.branchID
				AND contracts.cont_status = 1 AND contracts.branch = ? order by contracts.contract_id");
		}
	$sql->bindParam(1,$branch);
	$sql->execute();
if($sql->rowCount()>0)
				{$tot_wanted = 0 ; $no = 0;
					$b_name = "";
	while($row = $sql->fetch())
			{
				$no++;
				$contract_id  = $row['contract_id'];
				$wanted       = new  Wanted($db, $contract_id);
				$pay       = $wanted->wanted();
				$ww        = $pay["wanted"];
				$days_qty  = $pay["days_qty"];
				$payed     = $pay["payed"];
				$tot_add   = $pay["tot_addtion"] ;
			
			$output .='<tr style="height:35px;"><td>' . $no . '</td>
						<td>' . $contract_id . ' </td>
						<td>' . $row['cust_name'] . '</td>
						<td>'.$ww.'</td>
						<td>' . $row['phone'] . '</td>
						<td>' . $row['kind'] . '</td>
						<td>' . $row['PL'] . '</td>
						<td></td></tr>';
					$tot_wanted += $ww ;
				$b_name = $row['b_name'];
				}
			}
			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="row" style="padding:5 ;">
				<div class="col-xs-10"style="float:right;">
					<h6 style="font-weight:bold;padding:3px;font-size:10pt;" ><?php echo $myday." ".$b_name ; ?> </h6>
				</div>
				<div class="col-xs-2"style="float:right;">
				</div>
			</div>
	       <?php $output1 =  '<table class="table table-bordered">
	           <thead>
	              <tr>
	                   <th class="text-center" width="4%">م</th>
					   <th class="text-center" width="7%">رقم العقد</th>
	                   <th class="text-center" width="28%">الإسم</th>
	                   <th class="text-center" width="6%">متأخرات</th>
	                   <th class="text-center" width="9%">الجوال</th>
					   <th class="text-center" width="8%">النوع</th>
					   <th class="text-center" width="11%">لوحة رقم </th>
	                   <th class="text-center" width="27%">ملاحظات</th>
	              </tr>
	            </thead>
	              <tbody>';?>
<?php $output2 ='</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>';

$output3 = $output1.$output.$output2 ;
		// header('Content-Type: application/vnd.ms-excel');
	 	// header('Content-Disposition: attachment; filename=حركة السداد.xls');
	 echo $output3;
		 ?>
