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

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

$today = date("Y-m-d");
?>
<!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.min.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="../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: #EDDFDF;
	}
</style>

<body style="background-color:#272E34;">
	<div class="container-fluid fixed-top" style="background-color:#111A21;padding:10px;">
		<br />
		<div class="form-row align-items-center">
			<div class="col-2">
				<input style="margin-right:30px;" type="submit" class="btn btn-danger" id="close" value=" خروج " />
			</div>
			<div class="col-8">
				<center for="info_comm2" class="control-label text-center" style="text-align:center;font-size:1.3em; padding:2px; margin:0;font-weight:bold; color:#EDDFDF;background-color:transparent;"> فتح عقد تشغيل سيارة <span id="brand" style="color:#e6ffe6;font-size:1.2em; padding:2px; margin:0;font-weight:bold;"></span> <span id="kind" style="color:#e6ffe6;font-size:1.2em; padding:2px; margin:0;font-weight:bold;">
					</span> لوحة رقم <span id="letters" style="color:#e6ffe6;font-size:1.2em; padding:2px; margin:0;font-weight:bold;"></span> <span id="numbers" style="color:#e6ffe6;font-size:1.2em; padding:2px; margin:0;font-weight:bold;"></span> موديل <span id="model" style="color:#e6ffe6;font-size:1.2em; padding:2px; margin:0;font-weight:bold;">
					</span></center>
			</div>
		</div>
	</div>
	<p><br /></p>
	<div class="container-fluid" style="margin-top: 90px;">
		<div class="form-row" style="margin-top:5px;">
			<div class="col-6">
				<div id="contract_msg"></div>
				<form method="post" class="form-horizontal " style="float:center; font-size:0.9em; " id="new_contract">
					<div class="form-row">
						<input type="hidden" id="contract_id" name="contract_id" />
						<input type="hidden" id="monthly" name="monthly" />
						<input type="hidden" id="km2" name="km2" />
						<input type="hidden" id="freekm" name="freekm" />
						<input type="hidden" id="lastkm" name="lastkm" />
						<input type="hidden" id="carid" name="carid" />
						<div class="col-2">
							<label for="rent_days" class="control-label text-success" style="float:left;">مدة الإيجار</label>
						</div>
						<div class="col-3">
							<input type="text" class="form-control" id="rent_days" name="rent_days" value="شهر" />
							<div class="text-danger" style="font-size:12px;" id="rent_days_msg"></div>
						</div>
						<div class="col-3">
							<label for="used_area" class="control-label" style="float:left;">منطقة الإستخدام</label>
						</div>
						<div class="col-4">
							<input type="text" class="form-control" id="used_area" name="used_area" value="جدة و مكة" />
							<div class="text-danger" style="font-size:12px;" id="used_area_msg"></div>
						</div>
					</div>
					<div class="form-row" style="margin-top:5px;">
						<div class="col-2">
							<label for="cust_search" class="control-label" style="float:left;">رقم الهوية</label>
						</div>
						<div class="col-4" style="padding-left:2px;">
							<input type="text" class="form-control" id="cust_search" name="cust_search" placeholder="رقم الهوية" />
							<input type="hidden" class="form-control" id="cust_id" name="cust_id" />
						</div>
						<div class="col-1">
							<button type="button" id="cust_btn1" class="btn btn-outline-primary"><i class="fa fa-search" style="font-size:15px;font-weight:bold;color:#ff9966;" aria-hidden="true"></i></button>
						</div>
					</div>
					<div class="form-row" style="margin-top:5px;">
						<div class="col-2">
							<label for="tamm_days" class="control-label" style="float:left;">أيام التفويض</label>
						</div>
						<div class="col-3">
							<input type="number" class="form-control" onchange="max_tamm()" id="tamm_days" name="tamm_days" />
							<div class="text-danger" style="font-size:12px;" id="tamm_days_msg"></div>
						</div>
					</div>
					<div class="form-row" style="margin-top:5px;">
						<hr />
					</div>
					<div class="form-row" style="margin-top:5px;">
						<div class="col-2">
							<label for="day_cost" class="control-label" style="float:left;">إيجار اليوم</label>
						</div>
						<div class="col-3">
							<input type="number" onchange="last()" class="form-control" id="day_cost" name="day_cost" />
							<div class="text-danger" style="font-size:12px;" id="day_cost_msg"></div>
						</div>
						<div class="col-3">
							<label for="km_out" class="control-label" style="float:left;">عداد الخروج</label>
						</div>
						<div class="col-4">
							<input type="number" onchange="last()" class="form-control" id="km_out" name="km_out" />
							<div class="text-danger" style="font-size:12px;" id="km_out_msg"></div>
						</div>
					</div>
					<div class="form-row" style="margin-top:5px;">
						<div class="col-2">
							<label for="kilo_cost" class="control-label" style="float:left;">سعر الكيلو</label>
						</div>
						<div class="col-3">
							<input type="number" onchange="last()" class="form-control" id="kilo_cost" name="kilo_cost" value="50" />
						</div>
						<div class="col-3">
							<label for="free_kilo" class="control-label" style="float:left;">الكيلو المسموح</label>
						</div>
						<div class="col-4">
							<input type="number" onchange="last()" class="form-control" id="free_kilo" name="free_kilo" value="250" />
						</div>
					</div>
					<div class="form-row" style="margin-top:5px;">
						<div class="col-2">
							<label for="comm" class="control-label" style="float:left;">التعليق</label>
						</div>
						<div class="col-10">
							<input type="text" class="form-control" id="comm" name="comm" placeholder="ملاحظات" />
							<div class="text-danger" style="font-size:12px;" id="comm_msg"></div>
						</div>
					</div>
					<div class="form-row" style="float:left;margin-right:10px;margin-top:5px;">
						<div class="col-2"></div>
						<div class="col-3" style="float:left;">
							<label for="cust_id2" class="control-label" style="float:left;">مستأجر إضافي</label>
						</div>
						<div class="col-5" style="float:left;margin-right:10px;">
							<input type="text" class="form-control" id="cust2_search" name="cust2_search" placeholder="رقم الهوية" style="float:left;" />
							<div class="text-danger" style="font-size:12px;" id="idSA_msg2"></div>
							<input type="hidden" class="form-control" id="cust_id2" name="cust_id2" />
						</div>
						<div class="col-1" style="float:left;margin-right:5px;">
							<button type="button" id="cust_btn2" class="btn btn-outline-primary" style="float:left;"><i class="fa fa-search" style="font-size:15px;font-weight:bold;color:#ff9966;" aria-hidden="true"></i></button>
						</div>
					</div>
					<br /><br /><br /><br />
					<script>
						function last() {
							var medd = parseInt($("#monthly").val()) || 0;
							var current = parseInt($("#day_cost").val()) || 0;

							if (current < medd) {
								$("#day_cost").val(medd).css("background-color", "#e07a6d");
							} else if (current == medd) {
								$("#day_cost").val(medd).css("background-color", "#e07a6d");
							} else {
								$("#day_cost").css("background-color", "");
							}

							var aa = parseInt($("#kilo_cost").val()) || 0;
							var bb = parseInt($("#km2").val()) || 0;
							if (aa < bb) {
								$("#kilo_cost").val(bb).css("background-color", "#e07a6d");
							} else if (aa == bb) {
								$("#kilo_cost").val(bb).css("background-color", "#e07a6d");
							} else {
								$("#kilo_cost").css("background-color", "");
							}

							var a2 = parseInt($("#free_kilo").val()) || 0;
							var b2 = parseInt($("#freekm").val()) || 0;
							if (a2 > b2) {
								$("#free_kilo").val(b2).css("background-color", "#e07a6d");
							} else if (a2 == b2) {
								$("#free_kilo").val(b2).css("background-color", "#e07a6d");
							} else {
								$("#free_kilo").css("background-color", "");
							}

							var a4 = parseInt($("#km_out").val()) || 0;
							var b4 = parseInt($("#lastkm").val()) || 0;
							if (a4 < b4) {
								$("#km_out").val(b4).css("background-color", "#e07a6d");
							} else {
								$("#km_out").css("background-color", "");
							}
						};

						function max_tamm() {
							var tamm = parseInt($("#tamm_days").val()) || 0;
							if (tamm > 175) {
								$("#tamm_days").val(175);
								$("#tamm_days").css("background-color", "#e07a6d");
							} else {
								$("#tamm_days").css("background-color", "");
							}
						}
					</script>
					<div class="form-row" style="margin-top:5px;">
						<div class="col-2">
							<label for="tash" class="control-label" style="float:left;">قيمة التشغيل</label>
						</div>
						<div class="col-2">
							<input type="text" class="form-control" id="tash" name="tash" />
						</div>
						<div class="col-3"></div>
						<div class="col-2">
							<label for="start" class="control-label" style="float:left;">تاريخ البداية</label>
						</div>
						<div class="col-3">
							<input type="text" class="form-control" id="start" name="start" value="<?php echo $today; ?>" />
						</div>
						<script>
							$(function() {
								var calendar = $.calendars.instance('');
								$('#start').calendarsPicker({
									calendar: calendar
								});
							});
						</script>
					</div>
					<br /><br />
					<div class="form-row" style="margin-top:5px;">
						<div class="col-4">
							<button type="submit" class="btn btn-success" name="save_contract" id="save_contract" style="font-weight:bold;"><i class="fa fa-cloud" aria-hidden="true"></i>حفظ العقد</button>
						</div>
					</div>
				</form>
			</div>
			<div class="col-1"></div>
			<div class="col-5" style="float:left;">
				<!-- first customer  -->
				<div id="cust_msg"></div>
				<div id="msg1"></div>
				<form method="post" class="form-horizontal " style="float:center; font-size:0.9em; " id="cust_info">
					<div class="form-row" style="margin-top:5px;">
						<input type="hidden" class="form-control" id="cust_info_id" name="cust_info_id" disabled="disabled" readonly />
						<div class="col-8">
							<label for="tot_wonted" class="control-label" style="float:left;">إجمالي مديونية</label>
						</div>
						<div class="col-4">
							<input type="text" class="form-control" id="tot_wonted" name="tot_wonted" disabled="disabled" readonly />
						</div>
					</div>
					<div class="form-row" style="margin-top:5px;">
						<div class="col-1">
							<label for="cust_name" class="control-label" style="float:left;">الإسم</label>
						</div>
						<div class="col-8">
							<input type="text" class="form-control" id="cust_name" name="cust_name" placeholder="بدون اسم" disabled="disabled" readonly />
						</div>
						<div class="col-3">
							<input type="text" class="form-control" id="dgree" name="dgree" disabled="disabled" readonly />
						</div>
					</div>
					<div class="form-row" style="margin-top:5px;">
						<div class="col-1">
							<label for="idSA" class="control-label" style="float:left;">هوية</label>
						</div>
						<div class="col-5">
							<input type="number" class="form-control" id="idSA" name="idSA" disabled="disabled" readonly />
						</div>
						<div class="col-1">
							<label for="id_Expire" class="control-label" style="float:left;">انتهاء</label>
						</div>
						<div class="col-5">
							<input type="text" class="form-control" id="id_Expire" name="id_Expire" disabled="disabled" readonly />
						</div>
					</div>
					<div class="form-row" style="margin-top:5px;">
						<div class="col-1">
							<label for="license" class="control-label" style="float:left;">رخصة</label>
						</div>
						<div class="col-5">
							<input type="number" class="form-control" id="license" name="license" disabled="disabled" readonly />
						</div>
						<div class="col-1">
							<label for="lic_expire" class="control-label" style="float:left;">إنتهاء</label>
						</div>
						<div class="col-5">
							<input type="text" class="form-control" id="lic_expire" name="lic_expire" disabled="disabled" readonly />
						</div>
					</div>
					<div class="form-row" style="margin-top:5px;">
						<div class="col-2">
							<label for="info_comm" class="control-label" style="float:left;">ملاحظات</label>
						</div>
						<div class="col-10">
							<input type="text" class="form-control" id="info_comm" name="info_comm" placeholder="ملاحظات" disabled="disabled" readonly />
						</div>
					</div>
					<div class="form-row" style="margin-top:5px;">
						<div class="col-10" style="float:left;">
							<img id="cust_photo" src="" class="img-fluid rounded" width="60%" height="auto" style="float:left;" alt="صورة الرخصة غير متاحة " disabled="disabled" readonly />
						</div>
					</div>
					<br />
				</form><!-- end of first customer -->
				<!-- start second customer -->
				<div id="cust2_msg"></div>
				<div id="msg2"></div>
				<form method="post" class="form-horizontal " style="float:center; font-size:0.9em; display:none ;" id="cust_info2">
					<div class="form-row" style="margin-top:5px;">
						<input type="hidden" class="form-control" id="cust_info_id" name="cust_info_id" />
						<div class="col-8">
							<label for="tot_wonted2" class="control-label" style="float:left;">إجمالي مديونية</label>
						</div>
						<div class="col-4">
							<input type="text" class="form-control" id="tot_wonted2" name="tot_wonted2" disabled="disabled" readonly />
						</div>
					</div>
					<div class="form-row" style="margin-top:5px;">
						<div class="col-1">
							<label for="cust_name2" class="control-label" style="float:left;">الإسم</label>
						</div>
						<div class="col-8">
							<input type="text" class="form-control" id="cust_name2" name="cust_name2" placeholder="بدون اسم" disabled="disabled" readonly />
						</div>
						<div class="col-3">
							<input type="text" class="form-control" id="dgree2" name="dgree2" disabled="disabled" readonly />
						</div>
					</div>
					<div class="form-row" style="margin-top:5px;">
						<div class="col-1">
							<label for="idSA2" class="control-label" style="float:left;">هوية</label>
						</div>
						<div class="col-5">
							<input type="number" class="form-control" id="idSA2" name="idSA2" disabled="disabled" readonly />
						</div>
						<div class="col-1">
							<label for="id_Expire2" class="control-label" style="float:left;">انتهاء</label>
						</div>
						<div class="col-5">
							<input type="text" class="form-control" id="id_Expire2" name="id_Expire2" disabled="disabled" readonly />
						</div>
					</div>
					<div class="form-row" style="margin-top:5px;">
						<div class="col-1">
							<label for="license2" class="control-label" style="float:left;">رخصة</label>
						</div>
						<div class="col-5">
							<input type="number" class="form-control" id="license2" name="license2" disabled="disabled" readonly />
						</div>
						<div class="col-1">
							<label for="lic_expire2" class="control-label" style="float:left;">إنتهاء</label>
						</div>
						<div class="col-5">
							<input type="text" class="form-control" id="lic_expire2" name="lic_expire2" disabled="disabled" readonly />
						</div>
					</div>
					<div class="form-row" style="margin-top:5px;">
						<div class="col-2">
							<label for="info_comm2" class="control-label" style="float:left;">ملاحظات</label>
						</div>
						<div class="col-10">
							<input type="text" class="form-control" id="info_comm2" name="info_comm2" placeholder="ملاحظات" disabled="disabled" readonly />
						</div>
					</div>
					<div class="form-row" style="margin-top:5px;">
						<div class="col-10">
							<img id="cust_photo2" src="" class="img-fluid rounded" width="50%" height="auto" style="float:left;" alt="صورة الرخصة غير متاحة " disabled="disabled" readonly />
						</div>
					</div>
				</form><!-- end of scound customer -->
			</div><!-- end of side form customer info -->
		</div>
	</div><!-- end of fluid2  -->
	<script>
		$(document).ready(function() {
			fetch("<?php echo $_REQUEST['carid']; ?>");
			$('#close').click(function() {
				close();
			});
			$("#cust_btn1").click(function() {
				event.preventDefault();
				var idSA = $("#cust_search").val();
				$("#idSA_msg1").html("");
				$("#msg1").html("");
				if (idSA != "") {
					$.ajax({
						url: "../actions/cust_single.php",
						method: "POST",
						data: {
							idSA: idSA
						},
						dataType: "json",
						success: function(data) {
							$("#cust_id").val(data.cust_id);
							$("#cust_info_id").val(data.cust_info_id);
							$("#tot_wonted").val(data.debt);
							if (data.debt > 0) {
								$("#tot_wonted").css('background-color', '#ff9999');
							}
							$("#dgree").val(data.dgree);
							$("#cust_name").val(data.cust_name);
							$("#idSA").val(data.idSA);
							$("#id_Expire").val(data.id_Expire);
							$("#license").val(data.license);
							$("#lic_expire").val(data.lic_expire);
							$("#info_comm").val(data.comm);
							$('#cust_photo').attr("src", "../photo/customers/" + (data.cust_photo));
							$("#msg1").html(data.notfound);
							$("#idSA_msg1").html(data.msg);
							if (data.dgree < 50) {
								$('#save_contract').css('display', 'none');
								$("#tot_wonted").css('background-color', '#ff9999');
								$("#dgree").css('background-color', '#ff9999');
								$("#cust_name").css('background-color', '#ff9999');
								$("#info_comm").css('background-color', '#ff9999');
							}
						},
						error: function(result) {
							alert(result.statusText);
						}
					});
					$.ajax({
						url: "../actions/cust_single.php",
						method: "POST",
						data: {
							cust_rent: idSA
						},
						success: function(data) {
							$("#cust_msg").html(data);
						}
					});
				}
			}); // end customer information search

			$("#cust_btn2").click(function() {
				event.preventDefault();
				var idSA = $("#cust2_search").val();
				$("#idSA_msg2").html("");
				$("#msg2").html("");
				if (idSA != "") {
					$.ajax({
						url: "../actions/cust_single.php",
						method: "POST",
						data: {
							idSA: idSA
						},
						dataType: "json",
						success: function(data) {
							$('#cust_info2').css('display', 'block');
							$("#cust_id2").val(data.cust_id);
							$("#cust_info_id2").val(data.cust_info_id);
							$("#tot_wonted2").val(data.debt);
							if (data.debt > 0) {
								$("#tot_wonted2").css('background-color', '#ff9999');
							}
							$("#dgree2").val(data.dgree);
							$("#cust_name2").val(data.cust_name);
							$("#idSA2").val(data.idSA);
							$("#id_Expire2").val(data.id_Expire);
							$("#license2").val(data.license);
							$("#lic_expire2").val(data.lic_expire);
							$("#info_comm2").val(data.comm);
							$('#cust_photo2').attr("src", "../photo/customers/" + (data.cust_photo));
							$("#msg2").html(data.notfound);
							$("#idSA_msg2").html(data.msg);
							if (data.dgree < 50) {
								$('#save_contract').css('display', 'none');
								$("#tot_wonted2").css('background-color', '#ff9999');
								$("#dgree2").css('background-color', '#ff9999');
								$("#cust_name2").css('background-color', '#ff9999');
								$("#info_comm2").css('background-color', '#ff9999');
							}
						},
						error: function(result) {
							alert(result.statusText);
						}
					});
					$.ajax({
						url: "../actions/cust_single.php",
						method: "POST",
						data: {
							cust_rent: idSA
						},
						success: function(data) {
							$("#cust2_msg").html(data);
						}
					});
				}
			}); // CUSTOMER 2 info_comm2

			$("#save_contract").click(function(event) {
				event.preventDefault();
				$("contract_msg").html("");
				$("#tamm_days_msg").html("");
				$("#day_cost_msg").html("");
				$("#km_out_msg").html("");
				$("#used_area_msg").html("");
				$("#rent_days_msg").html("");
				$("#open_comm_msg").html("");
				$("#printed_no_msg").html("");
				var formdata = $("#new_contract").serialize();
				$.ajax({
					url: "new_tash_action.php",
					method: "POST",
					data: formdata,
					dataType: "json",
					success: function(data) {
						if (data.error) {
							$("#day_cost_msg").html(data.error.day_cost);
							$("#km_out_msg").html(data.error.km_out);
							$("#used_area_msg").html(data.error.used_area);
							$("#comm_msg").html(data.error.comm);
							$("#printed_no_msg").html(data.error.printed_no);
							$("#contract_msg").html(data.error.msg22);
						} else {
							$("#contract_msg").html(data.msg);
							$("#contract_id").val(data.contract_id);
							$("#contract_msg").delay(5500).slideUp(function() {
								$("#contract_msg").html('');
							});
						}
					}
				});
			});

			function fetch(carid) {
				$.ajax({
					url: "../actions/car_single.php",
					method: "POST",
					data: {
						carid: carid
					},
					dataType: "json",
					success: function(data) {
						$("#carid").val(data.carid);
						$("#monthly").val(data.monthly);
						$("#km2").val(data.km2);
						$("#freekm").val(data.freekilo);
						$("#day_cost").val(data.costday);
						$("#km_out").val(data.maxkm);
						$("#last_oil").html(data.maxkm);
						$("#lastkm").val(data.maxkm);
						$("#kilo_cost").val(data.km1);
						$("#brand").html(data.brand);
						$("#kind").html(data.kind);
						$("#letters").html(data.letters);
						$("#numbers").html(data.numbers);
						$("#color").html(data.color);
						$("#model").html(data.model);
					},
					error: function(data) {
						alert(data.statusText);
					}
				});
			} //end of fetch contract
		}); // end of script
	</script>
</body>
</html>