<?php
require_once "../magic/auth.php";

$Car = $_REQUEST['id'];
$auth = bdr_require_privilege(12);
$user = $auth['user_id'];
$conn = $auth['conn'];

$master = $conn->prepare('SELECT role from users where user_id = ? ');
$master->bindParam(1, $user);
$master->execute();
$ro = $master->fetch(PDO::FETCH_ASSOC);
$role		  = $ro["role"];

$cars_contracts_link = "../ment/car_contracts.php?dd=$Car";
if ($role < 3) {
	$cars_contracts_link = "../acc/car_contracts.php?dd=$Car";
}

$today = date("Y-m-d");
$maxReq = $conn->prepare("SELECT max(req_id) as maxReq from ment_request where car_id = ?");
$maxReq->bindParam(1, $Car);
$maxReq->execute();
if ($maxReq->rowCount() > 0) {
	$max = $maxReq->fetch(PDO::FETCH_ASSOC);
	$req_idd = $max['maxReq'];
}
?>
<!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>

<body style="background-color:#efefef;">
	<div class="container">
		<br /><br />
		<button type="submit" class="btn btn-danger" name="close" id="close"><i style="font-size:14pt ;" class="fa fa-times" aria-hidden="true"></i></button>
		<br /><br />
		<h3 style="text-align:center">تحديث بيانات السيارة بالكامل <span id="hd_kind" style="font-size: 1.75rem;"></span> لوحة رقم <span id="hd_numbers" style="font-size: 1.75rem;color:teal;"></span> موديل <span id="hd_model" style="font-size: 1.75rem;"></span></h3>
		<br /><br /><br />
		<div class="form-row">
			<div class="col-7">
				<input type="hidden" id="carid" />
			</div>
			<div class="col-2">
				<a href="../a/ment_b_r.php?id=<?php echo $Car; ?>" target="_blank"><button type="button" style="font-weight:bold;font-size:17px;padding:5px;" class="btn btn-outline-danger form-control"> طلب صيانة <i class="fa fa-shopping-cart" aria-hidden="true"></i></button></a>
			</div>
			<div class="col-1">
				<a href="../naql/lic_print.php?J=<?php echo $Car; ?>" target="_blank"><button type="button" style="font-weight:bold;font-size:17px;padding:5px;" class="btn btn-outline-success form-control"> رخصة <i class="fa fa-id-card" aria-hidden="true"></i> </button></a>
			</div>
			<div class="col-1">
				<a href="<?php echo $cars_contracts_link; ?>" target="_blank"><button type="button" style="font-weight:bold;font-size:17px;padding:5px;" class="btn btn-outline-primary form-control"> عقود <i class="fa fa fa-film" aria-hidden="true"></i></button></a>
			</div>
			<div class="col-1">
				<a href="ment_respond.php?id=<?php echo $Car . '&req_id=' . $req_idd; ?>" target="_blank"><button type="button" style="font-weight:bold;font-size:17px;padding:5px;" class="btn btn-outline-danger form-control"> صيانة <i class="fa fa-heartbeat" aria-hidden="true"></i></button></a>
			</div>
		</div>
	</div>
	<div class="container p-1 my-3">
		<br /> <!-- Nav tabs -->
		<div class="form">
			<ul class="nav nav-pills navbar-right">
				<li class="nav-item"><a class="nav-link active" style="float:right;" href="#tab_work" aria-controls="profile" role="tab" data-toggle="tab">تشغيل </a></li>
				<li class="nav-item"><a class="nav-link" style="float:right;" href="#tab_photo" aria-controls="profile" role="tab" data-toggle="tab">الرخصة </a></li>
				<li class="nav-item"><a class="nav-link" style="float:right;" href="#tab_trafic" aria-controls="profile" role="tab" data-toggle="tab">المرور </a></li>
				<li class="nav-item"><a class="nav-link" style="float:right;" href="#tab_ins" aria-controls="profile" role="tab" data-toggle="tab">التأمين</a></li>
				<li class="nav-item"><a class="nav-link" style="float:right;" href="#tab_woner" aria-controls="profile" role="tab" data-toggle="tab">البائع</a></li>
			</ul>
			<!-- Tab panes -->
			<div class="tab-content" style="width:100% ;">
				<div role="tabpanel" class="tab-pane fad active" id="tab_work">
					<form class="form-horizontal" id="form_work" method="POST" class="form-horizontal" style="float:center;">
						<br />
						<input type="hidden" id="car_work" name="car_work" value="" />
						<div id="form_work_msg"></div>
						<div class="form-row align-items-center">
							<input type="hidden" id="role" name="role" value="<?php echo $role; ?>" />
							<div class="col-2">
								<label for="costday" class="control-label" style="float:left;">إيجار اليوم</label>
							</div>
							<div class="col-1">
								<input type="number" class="form-control" style="float:right;" id="costday" name="costday" value="" placeholder="يومي" />
							</div>
							<div class="col-1">
								<input type="number" class="form-control" id="monthly" name="monthly" value="" placeholder="شهري" />
							</div>
							<div class="col-1">
								<input type="number" class="form-control" id="minn" name="minn" value="" placeholder="الحد الأدني" />
							</div>
							<div class="col-1">
								<label for="minn" class="control-label ">ريال</label>
							</div>
							<div class="col-2">
								<label for="month_cost" class="control-label" style="float:left;">الإيجار الشهري</label>
							</div>
							<div class="col-2">
								<input type="number" value="" class="form-control" id="month_cost" name="month_cost" />
							</div>
							<div class="col-1">
								<label for="target" class="control-label" style="float:left;">تارجت</label>
							</div>
							<div class="col-1">
								<input type="number" class="form-control" id="target" name="target" value="" />
							</div>
						</div>
						<div class="form-row align-items-center">
							<div class="col-2">
								<label for="km1" class="control-label" style="float:left;">الكيلو</label>
							</div>
							<div class="col-1">
								<input type="number" class="form-control" id="km1" name="km1" value="" placeholder="يومي" />
							</div>
							<div class="col-1">
								<input type="number" class="form-control" id="km2" name="km2" value="" placeholder="شهري" />
							</div>
							<div class="col-1">
								<input type="number" class="form-control" id="km3" name="km3" value="" placeholder="الحد الأدني" />
							</div>
							<div class="col-1">
								<label for="km3" class="control-label ">هللة</label>
							</div>
							<div class="col-2">
								<label for="freekilo" class="control-label" style="float:left;">الكيلو المسموح</label>
							</div>
							<div class="col-2">
								<input type="number" class="form-control" id="freekilo" name="freekilo" value="" />
							</div>
							<div class="col-1">
								<label for="used_percent" class="control-label" style="float:left;">إستهلاك</label>
							</div>
							<div class="col-1">
								<input type="number" class="form-control" id="used_percent" name="used_percent" value="" />
							</div>
						</div>
						<div class="form-row align-items-center">
							<div class="col-2">
								<label for="branch" class="control-label " style="float:left;">الفرع</label>
							</div>
							<div class="col-2">
								<select class="form-control" id="branch" name="branch" style="padding:3px;">
									<?php
									$branchs = "";
									$sql = $conn->prepare("SELECT branchID , b_name from branches ");
									$sql->execute();
									if ($sql->rowCount() > 0) {
										while ($row = $sql->fetch()) {
											$branchid   = $row["branchID"];
											$b_name     = $row["b_name"];
											$branchs .= "<option value= $branchid >$b_name</option>";
										}
										echo $branchs;
									} ?>
								</select>
							</div>
							<div class="col-2">
								<label for="carstatus" class="control-label " style="float:left;">حالة السيارة</label>
							</div>
							<div class="col-2">
								<select class="form-control" id="carstatus" name="carstatus" style="padding:3px;">
									<?php
									$sql = $conn->prepare("SELECT status_id , status from carstatus");
									$sql->execute();
									if ($sql->rowCount() > 0) {
										while ($row = $sql->fetch()) {
											$status_id   = $row["status_id"];
											$status      = $row["status"];
											echo "<option value= $status_id >$status</option>";
										}
									} ?>
								</select>
							</div>
							<div class="col-2">
								<label for="carstatus" class="control-label " style="float:left;">نوع التشغيل</label>
							</div>
							<div class="col-2">
								<select class="form-control" id="car_job_id" name="car_job_id" style="padding:3px;">
									<?php
									$job = "";
									$sql = $conn->prepare("SELECT car_job_id , car_job_desc from car_job");
									$sql->execute();
									if ($sql->rowCount() > 0) {
										while ($row = $sql->fetch()) {
											$car_job_id   = $row["car_job_id"];
											$car_job_desc  = $row["car_job_desc"];
											$job .=  "<option value= $car_job_id >$car_job_desc</option>";
										}
										echo $job;
									} ?>
								</select>
							</div>
						</div>
						<div class="row">
							<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" value="" />
							</div>
						</div>
						<div class="form-row align-items-center">
							<div class="col-2">
								<label for="start" class="control-label" style="float:left;">تاريخ البداية</label>
							</div>
							<div class="col-2 input-append">
								<input type="text" value="" class="form-control" id="start" readonly name="start" />
							</div>

							<div class="col-2">
								<label for="date_sale" class="control-label" style="float:left;">نقل ملكية</label>
							</div>
							<div class="col-2 input-append">
								<input type="text" value="" class="form-control" id="date_sale" readonly name="date_sale" />
							</div>
							<div class="col-2">
								<label for="area" class="control-label " style="float:left;">موقع السيارة</label>
							</div>
							<div class="col-2">
								<select class="form-control" id="area" name="area" style="padding:3px;">
									<?php
									$area = "";
									$sql = $conn->prepare("SELECT branchID , b_name from branches 
								where branchID != 8");
									$sql->execute();
									if ($sql->rowCount() > 0) {
										while ($row = $sql->fetch()) {
											$branchid   = $row["branchID"];
											$b_name     = $row["b_name"];
											$area .= "<option value= $branchid >$b_name</option>";
										}
										echo $area;
									}
									?>
								</select>
							</div>

						</div>
						
						<br /><br />
						<div>
							<button type="submit" class="btn btn-success save" style="font-weight:bold;font-size:14pt ;" farm="form_work" id="save_work" data-loading-text="Loading..."><i style="font-size:14pt ;" class="fa fa-check-square-o" aria-hidden="true"></i></button>
						</div> <!-- /modal-footer -->
					</form> <!-- /.form -->
				</div><!-- /work update -->

				<div role="tabpanel" class="tab-pane fad" id="tab_photo">
					<form method="POST" id="form_photo" class="form-horizontal" enctype="multipart/form-data">
						<br /><br />
						<input type="hidden" id="car_photo" name="car_photo" value="" />
						<input type="hidden" id="car_letters" name="car_letters" value="" />
						<input type="hidden" id="car_numbers" name="car_numbers" value="" />

						<div class="form-row align-items-center">
							<img id="lic_preview" src="" class="img-fluid rounded" width="100%" height="auto" alt="صورة الرخصة غير متاحة" />
						</div>
						<div class="form-row align-items-center">
							<div class="col-6">
								<input style="font-weight:bold;" type="file" class="btn btn-primary" name="lic_photo" id="lic_photo" />
							</div>
							<div class="col-2">
								<input style="float:right ; font-weight:bold;" type="submit" class="btn btn-primary" id="photo_paly" value="حفظ تعديلات الرخصة" />
							</div>
							<div id="form_photo_msg"></div>
						</div>
					</form> <!-- /form -->
				</div><!-- car licens photo -->

				<div role="tabpanel" class="tab-pane" id="tab_trafic">
					<form class="form-horizontal" id="form_trafic" method="POST">
						<br />
						<input type="hidden" id="car_trafic" name="car_trafic" value="" />
						<div id="form_trafic_msg"></div>
						<div class="form-row align-items-center">
							<div class="col-3">
								<label for="skilton" class="control-label">رقم الهيكل</label>
								<input type="text" class="form-control" id="skilton" name="skilton" placeholder="رقم الهيكل" value="" />
								<!-- <div id="ins_val"></div> -->
							</div>
							<div class="col-1 ">
								<label for="letters" class="control-label" style="float:left;"> لوحة </label>
								<input type="text" class="form-control" id="letters" name="letters" value="" placeholder="حروف" />
							</div>
							<div class="col-1">
								<label for="numbers" class="control-label">رقم</label>
								<input type="number" class="form-control" id="numbers" name="numbers" value="" placeholder="أرقام" />
							</div>
						</div>
						<div class="form-row align-items-center">
							<div class="col-2">
								<label for="brand" class="control-label" style="float:left;">ماركة السيارة</label>
							</div>
							<div class="col-2">
								<input type="text" class="form-control" id="brand" name="brand" value="" placeholder="ماركة السيارة" />
							</div>
							<div class="col-2">
								<label for="kind_use" class="control-label" style="float:left;">نوع التسجيل</label>
							</div>
							<div class="col-2">
								<input type="text" class="form-control" id="kind_use" name="kind_use" value="" placeholder="نوع التسجيل" />
							</div>
							<div class="col-2">
								<label for="kind" class="control-label" style="float:left;">طراز المركبة</label>
							</div>
							<div class="col-2">
								<input type="text" class="form-control" id="kind" name="kind" value="" placeholder="النوع" />
								<!-- <div id="ins_val"></div> -->
							</div>

						</div>
						<div class="form-row align-items-center">
							<div class="col-2">
								<label for="color" class="control-label" style="float:left;">اللون</label>
							</div>
							<div class="col-2">
								<input type="text" class="form-control" id="color" name="color" value="" placeholder="اللون" />
							</div>
							<div class="col-2">
								<label for="year" class="control-label" style="float:left;">الموديل</label>
							</div>
							<div class="col-2">
								<input type="number" class="form-control" id="model" name="model" value="" placeholder="سنة الصنع" />
							</div>
							<div class="col-2">
								<label for="lic_expire" class="control-label " style="float:left;">نهاية الترخيص</label>
							</div>
							<div class="col-2">
								<input type="text" class="form-control" id="lic_expire" name="lic_expire" value="" readonly placeholder="نهاية الترخيص" />
								<script>
									$(function() {
										var calendar = $.calendars.instance('ummalqura');
										$('#lic_expire').calendarsPicker({
											calendar: calendar
										});
									});
								</script>
							</div>
						</div>
						<br /><br />
						<div>
							<button type="submit" class="btn btn-outline-success save" farm="form_trafic" id="save_trafic" data-loading-text="Loading..."><i style="font-size:14pt ;" class="fa fa-check-square-o" aria-hidden="true"></i></button>
						</div> <!-- /modal-footer -->
					</form> <!-- /.form -->
				</div><!-- /tab trafic -->

				<div role="tabpanel" class="tab-pane" id="tab_ins">
					<form class="form-horizontal" id="form_ins" method="POST">
						<input type="hidden" id="car_ins" name="car_ins" value="" />
						<input type="hidden" id="active_kilov" name="active_kilov" value="" />
						<input type="hidden" id="active_insv" name="active_insv" value="" />
						<br /><br /><br />
						<div id="form_ins_msg"></div>
						<div class="form-row align-items-center">
							<br />
							<div class="col-2">
								<label for="insurance_id" class="control-label " style="float:left;">شركة التأمين</label>
							</div>
							<div class="col-3">
								<select class="form-control" id="insurance_id" name="insurance_id" style="padding:3px;">
									<?php
									$sql = $conn->prepare("SELECT insurance_id , ins_name from insurance");
									$sql->execute();
									if ($sql->rowCount() > 0) {
										while ($row = $sql->fetch()) {
											$insurance_id   = $row["insurance_id"];
											$ins_name  = $row["ins_name"];
											echo "<option value= $insurance_id >$ins_name</option>";
										}
									} ?>
								</select>
							</div>
							<div class="col-3"> <input type="text" class="form-control" id="insurance_no" name="insurance_no" value="" />
							</div>
							<div class="col-2">
								<label for="insurance_expire" class="control-label" style="float:left;">نهاية التأمين</label>
							</div>
							<div class="col-2">
								<input type="text" class="form-control" id="insurance_expire" name="insurance_expire" readonly value="" />
								<script>
									$(function() {
										var calendar = $.calendars.instance('');
										$('#insurance_expire').calendarsPicker({
											calendar: calendar
										});
										$('#woner_permeation').calendarsPicker({
											calendar: calendar
										});
										$('#start').calendarsPicker({
											calendar: calendar
										});
										$('#myday').calendarsPicker({
											calendar: calendar
										});
										$('#date_sale').calendarsPicker({
											calendar: calendar
										});
									});
								</script>
							</div>
						</div>
						<br />
						<div class="form-row">
							<div class="col-2">
								<label for="insurance_kind" class="control-label " style="float:left;">نوع التغطية التأمينية</label>
							</div>
							<div class="col-3">
								<select class="form-control" id="insurance_kind" name="insurance_kind" style="padding:3px;">
									<option value="1">المسئولية المدنية تجاه الغير</option>
									<option value="2">تغطية ضد الغير - وافي</option>
									<option value="3">تأمين شامل</option>
								</select>
							</div>
							<div class="col-2">
								<label for="insurance_money" class="control-label" style="float:left;">قسط التحمل</label>
							</div>
							<div class="col-1">
								<input type="number" class="form-control" id="insurance_money" name="insurance_money" value="" />
							</div>
						</div>
						<br />
						<div class="form-row">
							<div class="col-1">
							</div>
							<div class="col-1">
								<input class="form-check-input form-control" type="checkbox" id="active_ins" name="active_ins" checked="true" />
							</div>
							<div class="col-1">
								<label for="wesam_ins" class="control-label">التأمين الذهبي</label>
							</div>
							<div class="col-2">
								<input type="number" class="form-control" id="wesam_ins" name="wesam_ins" value="29.99" />
							</div>
							<div class="col-1">
								<input class="form-check-input form-control" type="checkbox" id="active_kilo" name="active_kilo" checked="true" />
							</div>
							<div class="col-1">
								<label for="open_kilo" class="control-label">الكيلو المفتوح</label>
							</div>
							<div class="col-2">
								<input type="number" class="form-control" id="open_kilo" name="open_kilo" value="129.99" />
							</div>
						</div>
						<br /><br />
						<div>
							<button type="submit" class="btn btn-outline-success save" farm="form_ins" id="save_ins" data-loading-text="Loading..."><i style="font-size:14pt ;" class="fa fa-check-square-o" aria-hidden="true"></i></button>
						</div> <!-- /modal-footer -->
					</form> <!-- /.form -->
				</div><!-- /tab ins -->
				<div role="tabpanel" class="tab-pane" id="tab_woner">
					<form class="form-horizontal" id="form_woner" method="POST">
						<input type="hidden" id="car_woner" name="car_woner" value="" />
						<br /><br />
						<div id="form_woner_msg"></div>
						<div class="form-row align-items-center">
							<div class="col-2">
								<label for="woner" class="control-label " style="float:left;">الشركة المالكة</label>
							</div>
							<div class="col-5">
								<select class="form-control" id="woner" name="woner" style="padding:3px;">
									<?php
									$sql = $conn->prepare("SELECT supp_id , supp_name from supplycomp");
									$sql->execute();
									if ($sql->rowCount() > 0) {
										while ($row = $sql->fetch()) {
											$supp_id   = $row["supp_id"];
											$supp_name  = $row["supp_name"];
											echo "<option value= $supp_id >$supp_name</option>";
										}
									} ?>
								</select>
							</div>
						</div>
						<div class="form-row align-items-center">
							<div class="col-2">
								<label for="cont_id" class="control-label " style="float:left;">رقم عقد الشراء</label>
							</div>
							<div class="col-2">
								<select class="form-control" id="cont_id" name="cont_id" style="padding:3px;"></select>
							</div>
							<div class="col-2">
								<label for="woner_permeation" class="control-label" style="float:left;">نهاية العقد</label>
							</div>
							<div class="col-2">
								<input type="text" class="form-control" id="woner_permeation" name="woner_permeation" value="" readonly placeholder="نهاية التفويض" />
							</div>
							<div class="col-2">
								<label for="total_cost" class="control-label" style="float:left;">إجمالي السعر</label>
							</div>
							<div class="col-2">
								<input type="number" class="form-control" id="total_cost" name="total_cost" value="" placeholder="إجمالي سعر السيارة" />
							</div>
						</div>
						<div class="form-row align-items-center">
							<div class="col-2">
								<label for="first_pay" class="control-label" style="float:left;">الدفعة المقدمة</label>
							</div>
							<div class="col-2">
								<input type="number" class="form-control" id="first_pay" name="first_pay" value="" placeholder="الدفعة الأولي" />
							</div>
							<div class="col-2">
								<label for="kest" class="control-label" style="float:left;">قيمة القسط</label>
							</div>
							<div class="col-1">
								<input type="number" class="form-control" id="kest" name="kest" value="" placeholder="القسط الشهري" />
							</div>
							<div class="col-1">
								<input type="number" class="form-control" id="kest_qty" name="kest_qty" value="" placeholder="عدد" />
							</div>
							<div class="col-2">
								<label for="last_pay" class="control-label" style="float:left;">اخر دفعة</label>
							</div>
							<div class="col-2">
								<input type="number" class="form-control" id="last_pay" name="last_pay" value="" placeholder="الدفعة الأخيرة" />
							</div>
						</div>
						<br /><br />
						<div>
							<button type="submit" class="btn btn-outline-success save" farm="form_woner" id="save_woner" data-loading-text="Loading..."><i style="font-size:14pt ;" class="fa fa-check-square-o" aria-hidden="true"></i></button>
						</div> <!-- /modal-footer -->
					</form> <!-- /.form -->
				</div><!-- /tab woner -->
			</div>
		</div>
		<br /><br />
		<div class="row">
			<div class="col-2">
				<input type="text" class="form-control" id="myday" name="myday" value="<?php echo $today; ?>" readonly />
			</div>
			<div class="col-2">
				<button type="button" id="car_move" style="font-weight:bold;font-size:16px;" class="btn btn-outline-success form-control">حركة الشهر <i class="fa fa-trophy" aria-hidden="true"></i> </button>
			</div>
		</div>
	</div>

	<script>
		$(document).ready(function() {
			fetch('<?php echo $Car ?>');

			function fetch(carid) {
				$.ajax({
					url: "../actions/car_single.php",
					method: "POST",
					data: {
						carid: carid
					},
					dataType: "json",
					success: function(data) {
						$("#carid").val(data.carid);
						$("#car_work").val(data.carid);
						$("#car_photo").val(data.carid);
						$("#car_trafic").val(data.carid);
						$("#car_ins").val(data.carid);
						$("#car_woner").val(data.carid);

						$('#lic_preview').attr('src', '../photo/car_lic/' + data.lic_photo);
						$("#car_letters").val(data.letters);
						$("#car_numbers").val(data.numbers);
						$("#branch").val(data.branch);
						$("#area").val(data.area);

						$("#brand").val(data.brand);
						$("#kind").val(data.kind);
						$("#hd_kind").html(data.kind);
						$("#model").val(data.model);
						$("#hd_model").html(data.model);
						$("#kind_use").val(data.kind_use);
						$("#color").val(data.color);
						$("#letters").val(data.letters);
						$("#numbers").val(data.numbers);
						$("#hd_numbers").html(data.letters + " " + data.numbers);
						$("#skilton").val(data.skilton);

						$("#lic_expire").val(data.lic_expire);
						$("#costday").val(data.costday);
						$("#monthly").val(data.monthly);
						$("#minn").val(data.minn);
						$("#month_cost").val(data.month_cost);
						$("#km1").val(data.km1);
						$("#km2").val(data.km2);
						$("#km3").val(data.km3);
						$("#freekilo").val(data.freekilo);
						$("#start").val(data.start);
						$("#target").val(data.target);

						$("#carstatus").val(data.carstatus);

						$("#car_job_id").val(data.car_job_id);
						$("#insurance_id").val(data.insurance_id);
						$("#insurance_no").val(data.insurance_no);
						$("#insurance_kind").val(data.insurance_kind);
						$("#insurance_money").val(data.insurance_money);
						$("#insurance_expire").val(data.insurance_expire);
						$("#active_insv").val(data.active_ins);
						$("#active_kilov").val(data.active_kilo);
						if (data.active_ins == 1) {
							$("#active_ins").prop('checked', true);
						} else {
							$("#active_ins").prop('checked', false);
						}
						if (data.active_kilo == 1) {
							$("#active_kilo").prop('checked', true);
						} else {
							$("#active_kilo").prop('checked', false);
						}

						//
						$("#wesam_ins").val(data.wesam_ins);
						$("#open_kilo").val(data.open_kilo);
						//
						$("#woner").val(data.woner);
						supp_cont_list(data.woner, data.cont_id);
						$("#woner_permeation").val(data.woner_permeation);
						$("#first_pay").val(data.first_pay);
						$("#kest").val(data.kest);
						$("#kest_qty").val(data.kest_qty);
						$("#last_pay").val(data.last_pay);
						$("#total_cost").val(data.total_cost);
						$("#date_sale").val(data.date_sale);
						$("#used_percent").val(data.used_percent);
						$("#comm").val(data.comm);

						if (data.carstatus == 2) {
							$('#branch').each(function() {
								if (!this.selected) {
									$(this).attr('disabled', true);
								}
							});
							$('#carstatus').each(function() {
								if (!this.selected) {
									$(this).attr('disabled', true);
								}
							});
							$('#car_job_id').each(function() {
								if (!this.selected) {
									$(this).attr('disabled', true);
								}
							});
						}
					},
					error: function(data) {
						alert(data.statusText);
					}
				});
			} //end of fetch car

			$('#close').click(function() {
				close();
			});

			$(".save").click(function(event) {
				event.preventDefault();
				var farm = $(this).attr("farm");
				if (farm == "form_trafic") {
					var tt = $('#lic_expire').val();
					if (tt.length != 0) {
						convertDates('ummalqura', 'lic_expire', 'lic_expire');
					}
				} else if (farm == "form_ins") {
					var ai = $('#active_insv');
					var ak = $('#active_kilov');
					$('#active_ins').is(":checked") ? ai.val(1) : ai.val(2);
					$('#active_kilo').is(":checked") ? ak.val(1) : ak.val(2);
				}
				$.ajax({
					url: "../actions/car_update.php",
					method: "POST",
					data: $("#" + farm).serialize(),
					success: function(data) {
						$("#" + farm + "_msg").html(data);
						$("#" + farm + "_msg").delay(3000).slideUp(function() {
							$("#" + farm + "_msg").html('');
						});
					},
					error: function(result) {
						alert(result.statusText);
					}
				});
			});
			$('#form_photo').on('submit', function(event) {
				event.preventDefault();
				var carid = $('#car_photo').val();
				var extension = $('#lic_photo').val().split('.').pop().toLowerCase();
				if (extension != '') {
					if (jQuery.inArray(extension, ['png', 'jpg', 'jpeg']) == -1) {
						alert("صور jpg فقط");
						$('#lic_photo').val('');
						return false;
					}
					var oFReader = new FileReader();
					oFReader.readAsDataURL(document.getElementById("lic_photo").files[0]);
					var f = document.getElementById("lic_photo").files[0];
					var fsize = f.size || f.fileSize;
					if (fsize > 1900000) {
						alert("حجم الصورة كبير جدا ...");
						return false;
					}
					$.ajax({
						url: "../actions/car_update.php",
						method: "POST",
						data: new FormData(this),
						contentType: false,
						processData: false,
						success: function(data) {
							$('#form_photo')[0].reset();
							$('#lic_preview').attr("src", "../photo/car_lic/" + data);
							$('#form_photo_msg').html(data);
							$("#form_photo_msg").delay(3000).slideUp(function() {
								$("#form_photo_msg").html('');
							});
						}
					});
				}
			}); //end of button add image
			$("#car_move").click(function(event) {
				event.preventDefault();
				var myday = $("#myday").val();
				var car = $("#carid").val();
				window.open("../acc/car_move.php?dd=" + myday + "&cr=" + car, '_blank');
			});

			function convertDates(name, ss, dest) {
				var date = $('#' + ss).val().split('/');
				var year = parseInt(date[0], 10);
				if (name == 'ummalqura') {
					if (year > 1800) {
						return false;
					}
				} else if (name == 'gregorian') {
					if (year < 1800) {
						return false;
					}
				}
				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) {
					alert(e);
					return;
				}
				name == 'ummalqura' ? showDate('gregorian', jd, dest) : showDate('ummalqura', jd, dest);
			}

			function showDate(name, jd, dest) {
				try {
					$('#' + dest).val('');
					var date = $.calendars.instance(name).fromJD(jd);
					$('#' + dest).val(date.formatYear() + '/' + date.month() + '/' + date.day());
				} catch (e) {
					alert(e);
					return null;
				}
			}

			$('#woner').change(function() {
				if ($(this).val() != '') {
					var supp_id = $(this).val();
					supp_cont_list(supp_id, '0');
				}
			}); //end on select option change

			function supp_cont_list(supp_id, cont_id) {
				$.ajax({
					url: "../actions/cont_list.php",
					method: "POST",
					data: {
						supp_id: supp_id,
						cont_id: cont_id
					},
					success: function(data) {
						$('#cont_id').html(data);
					}
				});
			}
		});
	</script>
	<script>
		var role = document.getElementById("role").value;
		if (role > 2) {
			document.getElementById("costday").readOnly = true;
			document.getElementById("monthly").readOnly = true;
			document.getElementById("minn").readOnly = true;
			document.getElementById("target").readOnly = true;
			document.getElementById("month_cost").readOnly = true;
			document.getElementById("freekilo").readOnly = true;
		} else {
			document.getElementById("costday").readOnly = false;
			document.getElementById("monthly").readOnly = false;
			document.getElementById("minn").readOnly = false;
			document.getElementById("target").readOnly = false;
			document.getElementById("month_cost").readOnly = false;
			document.getElementById("freekilo").readOnly = false;

		}
	</script>
</body>

</html>
