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

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

if (!isset($_SESSION)) {
}
$contract_id = $_REQUEST['id'];
$branchCONT = $db->prepare('SELECT father,status FROM aksat_contract WHERE contract_id = ?');
$branchCONT->bindParam(1, $contract_id);
$branchCONT->execute();
if ($branchCONT->rowCount() < 1) {
	header("location:../logout.php");
}
?>
<!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="../hj2/humanity.calendars.picker.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>
	<script src="../hj2/jquery.plugin.js"></script>
	<script src="../hj2/jquery.calendars.js"></script>
	<script src="../hj2/jquery.calendars.plus.js"></script>
	<script src="../hj2/jquery.calendars.picker.js"></script>
	<script src="../hj2/jquery.calendars.ummalqura.js"></script>
</head>
<style>
	.control-label,
	.table,
	.h6,
	h6 {
		color: #CADDD8;
	}

	.form-control {
		font-size: 16px;
		font-weight: bold;
		background-color: inherit;
	}
</style>

<body style="background-color:#051603;">
	<div class="container-fluid fixed-top" style="background-color:black;">
		<br />
		<div class="form-row align-items-center">
			<div class="col-1">
				<button style="margin-right:30px;" type="submit" class="btn btn-outline-danger" id="close2">خروج</button>
			</div>
			<div class="col-11 text-center">
				<h3 style="font-weight:bold; color:#CADDD8;" id="title">متابعة عقد رقم <?php echo $contract_id; ?></h3>
			</div>
		</div>
		<?php
		$sql = $db->prepare('SELECT aksat_contract.contract_id, aksat_contract.cust_id2,cars.carid,customers.cust_id ,customers.cust_name,customers.phone , customers.phone2,aksat_contract.tamm_expire , cars.kind ,
			 concat(cars.letters ," ", cars.numbers) as PL , cars.model,aksat_contract.status , aksat_contract.used_area ,aksat_contract.rent_days , aksat_contract.hj
			   FROM cars,aksat_contract,customers
	    WHERE aksat_contract.carid = cars.carid AND aksat_contract.cust_id = customers.cust_id AND aksat_contract.contract_id = ? ');
		$sql->bindParam(1, $contract_id);
		$sql->execute();
		if ($sql->rowCount() > 0) {
			$row = $sql->fetch(PDO::FETCH_ASSOC);
			$date1 = new DateTime($row['tamm_expire']);
			$date2 = new DateTime(date("Y-m-d"));
			$qty   = $date2->diff($date1)->format("%a");
			if ($date1 < $date2) {
				$qty = 3;
			}
			$ddy = date("Y/m/d", strtotime($row['tamm_expire']));
			$cust_id = $row['cust_id'];
		?>
			<div class="form-row" style="margin-top:10px;">
				<div class="col-1">
					<a href="../a/cust_update.php?id=<?php echo $cust_id; ?>" target="_blank"><button type="button" class="btn btn-outline-success btn-sm ">بيانات المستأجر</button></a>
				</div>
				<div class="col-10">
					<center class="control-label" style="font-size:1.3em; padding:0px; margin:0;font-weight:bold; color:#CADDD8;">
						<?php if ($qty > 3) {
							echo $row['cust_name'] . " ~ " . $row['phone'] . " ~ " . $row['phone2'] . " ~ انتهاء تم " . $ddy . " ~ سيارة " . $row['kind'] . " " . $row['PL'] . " موديل " . $row['model'];
						} else {
							echo $row['cust_name'] . " ~" . $row['phone'] . "~" . $row['phone2'] . " ~<span style='color:#DA2D07;font-size:1.3em;'> انتهاء تم " . $ddy . " ~</span>سيارة " . $row['kind'] . " " . $row['PL'] . " موديل " . $row['model'];
						}
						$cust22 = $row['cust_id2'];
						?>
					</center>
				</div>
				<div class="col-1">
					<a href="../naql/lic_print.php?J=<?php echo $row['carid']; ?>" target="_blank"><button type="button" class="btn btn-outline-success btn-sm ">رخصة السيارة</button></a>
				</div>
			</div>
		<?php if ($cust22 > 0) {
				$cust2 = $db->prepare("SELECT cust_id , cust_name , phone , phone2 from customers where cust_id = ?");
				$cust2->bindParam(1, $cust22);
				$cust2->execute();
				if ($cust2->rowCount() > 0) {
					$wow = $cust2->fetch(PDO::FETCH_ASSOC);
					echo '<div class="form-row" style="margin-top:10px;">
			<div class="col-1">
			<a href="../a/cust_update.php?id=' . $cust22 . '" target="_blank"><button type="button" class="btn btn-outline-success btn-sm ">مستأجر إضافي</button></a>
		</div >
			<div class="col-11">
				<center class="control-label" style="font-size:1.3em; font-weight:bold; color:#CADDD8;">';
					echo $wow['cust_name'] . "~" . $wow['phone'] . "~" . $wow['phone2'];
					echo "</center>
			</div>
		</div>
		</div>";
				}
			}
		}

		$debt = $db->prepare("SELECT sum(wanted) as debt from aksat_contract where cust_id = ?  LIMIT 1");
		$debt->bindParam(1, $cust_id);
		$debt->execute();
		if ($debt->rowCount() > 0) {
			$monn = $debt->fetch(PDO::FETCH_ASSOC);
			$debt     = $monn["debt"];
		}
		?>
	</div>
	<p><br /></p>
	<div class="container-fluid" style="margin-top:130px;">
		<div class="form-row">
			<div class="col-2">
				<div class="form-row">
					<div class="col-9">
						<label for="used_area" class="control-label">منطقة الإستخدام</label>
					</div>
					<div class="col-3">
						<button type="button" id="area" class="btn btn-primary" style="padding:5px;background-color:transparent;"><i class="fa fa-tasks" style="font-size: 15px;font-weight:bold;color:#ff9966;" aria-hidden="true"></i></button>
					</div>
				</div>
				<div class="form-row">
					<div class="col-11">
						<input type="text" class="form-control" id="used_area" name="used_area" value="<?php echo $row['used_area']; ?>" />
						<div class="text-danger" style="font-size:12px;" id="used_area_msg"></div>
					</div>
				</div>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-11">
						<input type="hidden" id="hjv" name="hjv" value="<?php echo $row['hj']; ?>" />
						<input type="hidden" id="contract_id" name="contract_id" value="<?php echo $contract_id; ?>" />
						<input type="text" class="form-control" id="rent_days" name="rent_days" value="<?php echo $row['rent_days']; ?>" />
						<div class="text-danger" style="font-size:12px;" id="rent_days_msg"></div>
					</div>
				</div>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-5">
						<label for="wanted" class="control-label">المطلوب</label>
					</div>
					<div class="col-6">
						<input type="number" class="form-control needo " disabled="disabled" readonly />
					</div>
				</div>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-11">
						<button type="button" id="rent_print" class="btn btn-outline-success btn-block">طباعة العقد </button>
					</div>
				</div>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-11">
						<a href="aksat_rep.php?id=<?php echo $contract_id; ?>" target="_blank"><button type="button" class="btn btn-outline-success btn-block">كشف حساب </button></a>
					</div>
				</div>
				<div class="form-row" style="margin-top:3px;">
					<div class="col-11">
						<input style="font-weight:bold;" type="submit" class="btn btn-outline-danger btn-block" id="close" value=" خروج " />
					</div>
				</div>
			</div>
			<div class="col-10">
				<!-- main body start her -->
				<?php
				$sql = $db->prepare('SELECT aksat_contract.contract_id, aksat_contract.cust_id2,cars.carid,customers.cust_id ,customers.cust_name,customers.phone , customers.phone2, cars.kind ,
					 concat(cars.letters ," ", cars.numbers) as PL , cars.model,aksat_contract.status , aksat_contract.used_area ,aksat_contract.rent_days , aksat_contract.hj
						 FROM cars,aksat_contract,customers
					WHERE aksat_contract.carid = cars.carid AND aksat_contract.cust_id = customers.cust_id AND aksat_contract.contract_id = ? ');
				$sql->bindParam(1, $contract_id);
				$sql->execute();
				if ($sql->rowCount() > 0) {
					$row = $sql->fetch(PDO::FETCH_ASSOC);
					$cust_id = $row['cust_id'];
				}
				$debt = $db->prepare("SELECT sum(wanted) as debt from aksat_contract where cust_id = ?  LIMIT 1");
				$debt->bindParam(1, $cust_id);
				$debt->execute();
				if ($debt->rowCount() > 0) {
					$monn = $debt->fetch(PDO::FETCH_ASSOC);
					$debt     = $monn["debt"];
				}
				$bb = $db->prepare("SELECT sum(wanted) as tot_wanted , sum(payed) as tot_payed from aksat_in where contract_id = ?  LIMIT 1");
				$bb->bindParam(1, $contract_id);
				$bb->execute();
				if ($bb->rowCount() > 0) {
					$bbc = $bb->fetch(PDO::FETCH_ASSOC);
					$tot_wanted     = $bbc["tot_wanted"];
					$tot_payed     = $bbc["tot_payed"];
				}
				?>
				<div class="form-row align-items-center">
					<div class="col text-center">
						<label class="control-label " style="font-size:1.3rem; font-weight:bold;">قيمة السيارة <?php echo $tot_wanted; ?> المدفوع <?php echo $tot_payed; ?> المتبقي من ثمن السيارة <?php echo $tot_wanted - $tot_payed; ?> </label>
					</div>
				</div>
				<br />
				<div class="form-row">
					<table class="table table-bordered table-hover">
						<thead>
							<tr>
								<th class="text-center" width="6%">كود</th>
								<th class="text-center" width="4%">قسط</th>
								<th class="text-center" width="16%">استحقاق</th>
								<th class="text-center" width="7%">المستحق</th>
								<th class="text-center" width="6%">سند</th>
								<th class="text-center" width="18%">تاريخ</th>
								<th class="text-center" width="7%">المدفوع</th>
								<th class="text-center" width="4%">تأخير</th>
								<th class="text-center" width="10%">سداد</th>
								<th class="text-center" width="18%">تعليق</th>
							</tr>
						</thead>
						<tbody id="tstandby">
							<?php
							$output = '';
							$sql = $db->prepare("SELECT *	FROM aksat_in	WHERE contract_id = ? ");
							$sql->bindParam(1, $contract_id);
							$sql->execute();
							if ($sql->rowCount() > 0) {
								while ($row = $sql->fetch()) {
									$bb = $row['payMethod'];
									if (!empty($bb)) {
										switch ($bb) {
											case "1":
												$pay =  "كاش";
												break;
											case "2":
												$pay = "تحويل الراجحي";
												break;
											case "3":
												$pay = "نقاط بيع";
												break;
											case "5":
													$pay = "تحويل الاهلي";
													break;
										}
									} else {
										$pay = "";
									}
									$output .= '<tr><td>' . $row['ak_id'] . '</td>
															<td>' . $row['ak_no'] . '</td>
															<td style="padding:0px; margin:0px;"><input class="hj1 form-control"name="hj1[]" value="' . $row['start'] . '"/></td>
															<td>' . $row['wanted'] . '</td>
															<td>' . $row['printed_no'] . '</td>
															<td style="padding:0px; margin:0px;"> <input class="hj2 form-control"name="hj2[]" value="' . $row['date_payed'] . '"/></td>
															<td>' . $row['payed'] . '</td>
															<td>' . $row['late'] . '</td>
															<td>' . $pay . '</td>
															<td>' . $row['comm'] . '</td>
													</tr>';
								}
							} else {
								$output .= '<tr><td colspan="10">لا يوجد سجلات ...</td></tr>';
							}
							echo $output;
							?>
						</tbody>
						<tfoot></tfoot>
					</table>
				</div>
				<!-- end main body -->
				<div class="form-row">
					<div class="col">
						<h6></h6>
						<table class="table table-bordered table-hover">
							<thead>
								<tr>
									<th class="text-center" width="8%">كود</th>
									<th class="text-center" width="10%">المبلغ</th>
									<th class="text-center" width="14%">التاريخ</th>
									<th class="text-center" width="23%">المستلم</th>
									<th class="text-center" width="15%">طريقة الدفع</th>
									<th class="text-center" width="25%">ملاحظات</th>
									<th class="text-center" width="5%">قسط</th>
								</tr>
							</thead>
							<tbody>
								<?php
								$output = '';
								$sql = $db->prepare("SELECT aksat_payed.payed_id , aksat_payed.money_in , aksat_payed.date_payed , users.realName , aksat_payed.payMethod , aksat_payed.comm , aksat_payed.ak_no
										FROM aksat_payed , users
										WHERE aksat_payed.father = users.user_id AND contract_id = ? AND tarheel = 1 order by payed_id DESC  ");
								$sql->bindParam(1, $contract_id);
								$sql->execute();
								if ($sql->rowCount() > 0) {
									$ss = 1;
									$sam = 0;
									while ($row = $sql->fetch()) {
										$bb = $row['payMethod'];
										switch ($bb) {
											case "1":
												$pay =  "كاش";
												break;
											case "2":
												$pay = "تحويل الراجحي";
												break;
											case "3":
												$pay = "نقاط بيع";
												break;
											case "5":
													$pay = "تحويل الأهلي";
													break;
										}
										if ($ss == 1) {
											$output .= '<tr>
	 															<td style="background-color:#D8DBD0;"><a  href="../aksat/payed_print.php?sanad=' . $row['payed_id'] . '"target="_blank">' . $row['payed_id'] . '</a></td>';
										} else {
											$output .= '<tr><td>' . $row['payed_id'] . '</td>';
										}
										$output .= '<td>' . $row['money_in'] . '</td>
																<td>' . $row['date_payed'] . '</td>
																<td>' . $row['realName'] . '</td>
																<td>' . $pay . '</td>
																<td>' . $row['comm'] . '</td>
																<td>' . $row['ak_no'] . '</td>
															</tr>  ';
										$ss = $ss + 1;
										$sam = $sam +	$row['money_in'];
									}
								} else {
									$output .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';
								}
								echo $output;
								?>
							</tbody>
							<tfoot></tfoot>
						</table>
					</div>
				</div>
			</div><!-- end of main row 2 and 10  -->
		</div>
	</div><!-- end of fluid2  -->
	<script>
		$(document).ready(function() {
			$('#close').click(function() {
				close();
			});
			$('#close2').click(function() {
				close();
			});
			$("#rent_print").click(function(event) {
				event.preventDefault();
				var contt = $("#contract_id").val();
				window.open("rent_print.php?contt=" + contt, '_blank');
			});
			$("#area").click(function(event) {
				event.preventDefault();
				var contract_id = $("#contract_id").val();
				var used_area = $('#used_area').val();
				var rent_days = $('#rent_days').val();
				$("#rent_days_msg").html("");
				$("#used_area_msg").html("");
				$.ajax({
					url: "update_aksat.php",
					method: "POST",
					data: {
						used_area: used_area,
						rent_days: rent_days,
						contract_id: contract_id
					},
					dataType: "json",
					success: function(data) {
						if (data.error) {
							$("#used_area_msg").html(data.error.used_area);
							$("#rent_days_msg").html(data.error.rent_days);
						} else {
							location.reload();
						}
					}
				});
			});
			$("#area_print").click(function(event) {
				event.preventDefault();
				var contt = $("#contract_id").val();
				window.open("print2.php?contt=" + contt, '_blank');
			});
			function change_date(date) {
				var date = date.split('-');
				var year = parseInt(date[0], 10);
				var name = 'ummalqura';
				if (year > 1800) {
					name = 'gregorian';
				}
				var month = parseInt(date[1], 10);
				var day = parseInt(date[2], 10);
				try {
					var jd = $.calendars.instance(name).newDate(year, month, day).toJD();
				} catch (e) {
					day = day - 1;
					try {
						var jd = $.calendars.instance(name).newDate(year, month, day).toJD();
					} catch (e) {
						day = day - 1;
						try {
							var jd = $.calendars.instance(name).newDate(year, month, day).toJD();
						} catch (e) {
							alert(e);
							return;
						}
					}
				}
				name == 'ummalqura' ? name = 'gregorian' : name = 'ummalqura';
				var re_date = $.calendars.instance(name).fromJD(jd);
				try {
					re_date = re_date.formatYear() + '-' + re_date.month() + '-' + re_date.day();
				} catch (e) {
					alert(e);
					return null;
				}
				return re_date;
			}
			var ss = $("#hjv").val();
			if (ss == 1) {
				$('.hj1').each(function() {
					var dd = $(this).val();
					dd = change_date(dd);
					$(this).val(dd);
				});

				$('.hj2').each(function() {
					var dd = $(this).val();
					if (dd == "") {} else {
						dd = change_date(dd);
						$(this).val(dd);
					}
				});
			}
		});
	</script>
</body>
</html>