<?php

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

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

$carid = $_REQUEST['id'];
$req = 1;
if(isset($_REQUEST["req"])){
	$req   = $_REQUEST['req'];
}

function current_car($mycar)
{
	$sql = $db->prepare('SELECT kind,concat(letters,numbers)as lh , model,color from cars where carid = ? ');
	$sql->bindParam(1, $mycar);
	$sql->execute();
	if ($sql->rowCount() > 0) {
		$result = $sql->fetch(PDO::FETCH_ASSOC);
	}
	return $result;
}
// if(empty($req)){
// 	$sq = $db->prepare("SELECT max(req_id) as req from ment_request where car_id = ?");
// 	$sq->bindParam(1, $mycar);
// 	$sq->execute();
// 	if ($sq->rowCount() > 0) {
// 		$result = $sql->fetch(PDO::FETCH_ASSOC);
// 		$req = $result['req'];
// 	}
	
// }
?>
<!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="../js/jquery.autocomplete.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:#EDF4F7;">
	<div class="container-fluid">
		<div class="row" style="background-color:#262928; padding-top: 15px;">
			<div class="col-2">
				<input type="hidden" id="neww" value="<?php echo $req ;?>"/>
			</div>
			<div class="col-8">
				<h3 aligne='center' style="font-weight:bold; color:#EDDFDF;" id="title">طلب صيانة لسيارة</h3>
				<?php
				$roas = current_car($carid);
				echo "<h4 style='font-weight:bold;color:#EDDFDF;' align = 'center' > سيارة  " . $roas['kind'] . " لوحة رقم " . $roas['lh'] . " موديل " . $roas['model'] . " اللون " . $roas['color'] . " </h4>";
				?>
			</div>
			<div class="col-2" style="float:left ;">
				<a href="../ment/car_ment_br.php?id=<?php echo $carid; ?>" target="_blank"><button type="button" style="font-weight:bold;font-size:17px;padding:5px;" class="btn btn-outline-danger btn-xs form-control">كارتة صيانة<i class="fa fa-heartbeat" aria-hidden="true"></i></button></a>
			</div>
		</div>
		<br /><br />
		<div class="row">
			<div class="col-3 col-right" style="padding:1 ; margin:0 ; background-color:#E8EBE1; ">
				<ul>
					<li><a href='#' style="font-weight:bold; font-size:1.4em ; text-decoration:none">سجلات الصيانة</a></li>
					<?php
					$reqs = $db->prepare('SELECT req_id , ask_date, description from ment_request WHERE car_id = ?  order by done ');
					$reqs->bindParam(1, $carid);
					$reqs->execute();
					if ($reqs->rowCount() > 0) {
						while ($row = $reqs->fetch()) {
							$req_id  = $row["req_id"];
							$ask_date  = $row["ask_date"];
							$description  = $row["description"];
							echo "<li><a href='#' class='req_data'  idd='$req_id'>$ask_date" . " " . "$description</a></li>";
						}
					} else {
						echo "<li><a href='#'   kok=''>لا توجد سجلات</a></li>";
					}
					?>
				</ul>
			</div><!-- end of side bar -->
			<div class="col-md-8" style="font-size:0.8em; padding:2px; margin:0;">
				<div class="form-row">
					<div class="col" id="msg">
						<!--Alert from signup form-->
					</div>
				</div>
				<div id="site">
					<form method="post" class="form-horizontal" id="new_request">
						<div class="form-row">
							<div class="col-2">
								<input type="hidden" class="form-control" id="car_id" name="car_id" value="<?php echo $carid; ?>" />
								<label for="kmm" class="control-label" style="text-align:center ;">قراءة العداد</label>
							</div>
							<div class="col-2  col-md-offset-2" style="float:right;">
								<input type="number" class="form-control" style="float:right;" id="kmm" name="kmm" placeholder="عداد السيارة" />
							</div>
							<div class="col-2  " style="float:right;">
								<label for="ask_date" class="control-label" style="float:left;">تاريخ الطلب</label>
							</div>
							<script>
								$(function() {
									var calendar = $.calendars.instance();
									$('#ask_date').calendarsPicker({
										calendar: calendar
									});
								});
							</script>
							<div class="col-3 ">
								<input type="text" class="form-control" id="ask_date" name="ask_date" readonly />
							</div>
						</div>
						<div class="row" style="padding:4px;">
							<div class="col-md-2" style="float:right;">
								<label for="description" class="control-label" style="float:right;">وصف الحالة</label>
							</div>
							<div class="col-xs-12 col-md-9" style="float:right;">
								<textarea class="form-control" rows="3" style="float:right;" id="description" name="description" placeholder="ملاحظات الصيانة في السيارة" style="position: absolute; z-index: 2; background: transparent;"></textarea>
							</div>
							<div class="col-md-2" style="float:right;">
								<label for="photo_before" class="control-label" style="float:left;">ادخال صورة</label>
							</div>
							<div class="col-md-4" style="float:right;">
								<input type="file" style="float:right;" class="form-control" id="photo_before" name="photo_before" />
							</div>
						</div>
						<div id="request_image"></div>
						<p><br /></p>
						<div class="form-group">
							<div class="col">
								<input type="submit" id="add_request" name="add_request" class="btn btn-success" value="ارسال الطلب">
							</div>
						</div>
					</form>
					<form method="post" class="form-horizontal" id="car_request" style="float:center; display:none; ">
						<div class="form-row">
							<div class="col-2">
								<label for="ask_date2" class="control-label" style="float:left;">تاريخ الطلب</label>
							</div>
							<div class="col-3 col-md-offset-2 ">
								<input type="text" class="form-control" id="ask_date2" name="ask_date2" readonly />
							</div>
							<div class="col-2">
								<label for="kmm2" class="control-label" style="float:right; text-align:center ;">قراءة العداد</label>
							</div>
							<div class="col-2 ">
								<input type="number" class="form-control" id="kmm2" name="kmm2" readonly />
							</div>

						</div>
						<div class="form-row">
							<div class="col-1">
								<label for="description2" class="control-label">وصف الحالة</label>
							</div>
							<div class="col-7">
								<textarea class="form-control" rows="3" id="description2" name="description2" readonly></textarea>
							</div>
							<div class="col-3" style="float:left;">
								<img class="img-responsive" id="photo_before2" name="photo_before2" src="" alt="بدون صورة" />
							</div>
						</div>
						<p><br /></p>
						<h3 style="color:blue; font-weight:bold;">رد الصيانة على الطلب</h3>
						<p><br /></p>
						<div class="form-row">
							<div class="col-2">
								<label for="done" class="control-label">حالة الطلب</label>
							</div>
							<div class="col-3">
								<input type="text" class="form-control" id="done" name="done" readonly />
							</div>
							<div class="col-2">
								<label for="date_done" class="control-label" style="float:left;">تاريخ الحالة</label>
							</div>
							<div class="col-3 ">
								<input type="text" class="form-control" id="date_done" name="date_done" readonly />
							</div>
						</div>
						<div class="form-row" style="padding:4px;">
							<div class="col-1">
								<label for="feedback" class="control-label" style="float:right;">تقرير الصيانة</label>
							</div>
							<div class="col-7">
								<textarea class="form-control" rows="3" style="float:right;" id="feedback" name="feedback" readonly></textarea>
							</div>
							<div class="col-3">
								<label for="next_kmm" class="control-label" style="float:right; text-align:center ;">العداد التالي للصيانة الدورية</label>
								<input type="number" class="form-control" style="float:right;" id="next_kmm" name="next_kmm" readonly />
							</div>
						</div>
						<div class="form-row" style="padding:4px;">
							<div class="col-11">
								<img class="img-responsive" id="photo_after" name="photo_after" src="" alt="بدون صورة" />
							</div>
						</div>
						<input type="hidden" class="form-control" id="car_id" name="car_id" value="<?php echo $carid; ?>" />
					</form>
				</div>
			</div><!-- end of body element -->
		</div>
	</div>
	</div><!-- container fluid -->
	<script>
		$(document).ready(function() {
			var neww = $("#neww").val();
			if(neww > 1){
				myreq(<?php echo $req;  ?>);
			}
			
			$('#close').click(function() {
				close();
			});
			$('#new_request').on('submit', function(event) {
				event.preventDefault();
				var extension = $('#photo_before').val().split('.').pop().toLowerCase();
				if (extension != '') {
					if (jQuery.inArray(extension, ['gif', 'png', 'jpg', 'jpeg']) == -1) {
						alert("Invalid Image File");
						$('#photo_before').val('');
						return false;
					}
					var oFReader = new FileReader();
					oFReader.readAsDataURL(document.getElementById("photo_before").files[0]);
					var f = document.getElementById("photo_before").files[0];
					var fsize = f.size || f.fileSize;
					if (fsize > 1700000) {
						alert("حجم الصورة كبير جدا ...");
						return false;
					}
				}
				$.ajax({
					url: "newRequest.php",
					method: "POST",
					data: new FormData(this),
					contentType: false,
					processData: false,
					success: function(data) {
						$('#request_image').html(data);
					},
					error: function(result) {
						alert(result.statusText);
					}
				})
			}); //end of button add image

			$('.req_data').click(function(event) {
				event.preventDefault();
				var req_id = $(this).attr("idd");
				$('#new_request').css("display", "none");
				$('#car_request').css("display", "block");
				$.ajax({
					url: "request_single.php",
					method: "POST",
					data: {
						req_id: req_id
					},
					dataType: "json",
					success: function(data) {
						$('#ask_date2').val(data.ask_date);
						$('#description2').val(data.description);
						$('#kmm2').val(data.kmm);
						$('#photo_before2').attr("src", "../photo/ment_before/" + (data.photo_before));
						$('#done').val(data.ment_status);
						$('#photo_after').attr("src", "../photo/ment_after/" + (data.photo_after));
						$('#date_done').val(data.date_done);
						$('#feedback').val(data.feedback);
						$('#next_kmm').val(data.next_kmm);
					}
				});
			}); //viewe requst single data for a car
//
function myreq(req_id) {
				$('#new_request').css("display", "none");
				$('#car_request').css("display", "block");
				$.ajax({
					url: "request_single.php",
					method: "POST",
					data: {
						req_id: req_id
					},
					dataType: "json",
					success: function(data) {
						$('#ask_date2').val(data.ask_date);
						$('#description2').val(data.description);
						$('#kmm2').val(data.kmm);
						$('#photo_before2').attr("src", "../photo/ment_before/" + (data.photo_before));
						$('#done').val(data.ment_status);
						$('#photo_after').attr("src", "../photo/ment_after/" + (data.photo_after));
						$('#date_done').val(data.date_done);
						$('#feedback').val(data.feedback);
						$('#next_kmm').val(data.next_kmm);
					}
				});
			}
		});
	</script>
</body>
</html>