<?php

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

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

$carid = $_REQUEST['id'];
$req_idd = $_REQUEST['req_id'];

function current_car($mycar)
{
	$db = bdr_database();
	$sql = $db->prepare('SELECT brand , 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;
}
?>
<!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>
<style>
	@media (min-width: 992px) {
		.modal-lg {
			min-width: 1000px;
			max-width: 1300px;
		}
	}
</style>

<body style="background-color:#f0f0f0;">
	<div class="container-fluid fixed-top" style="background-color:black;padding:10px;">
		<button type="button" class="btn btn-primary a " data-toggle="modal" req="" data-backdrop="static" data-keyboard="false" data-target="#invoice_for_request">صرف من فاتورة</button>
		<button type="button" class="btn btn-primary a " data-toggle="modal" req="" data-backdrop="static" data-keyboard="false" data-target="#items_for_request">صرف بالقطعة</button>
		<a id="req_report" href="" target="_blank"><button type="button" class="btn btn-info">تقرير مصروفات طلب الصيانة</button></a>
		<a id="req_xls" href="" target="_blank"><button type="button" class="btn btn-danger">مصروفات الصيانة اكسيل</button></a>
		<a id="car_report" href="../ment/car_ment_rep.php?id=<?php echo $carid ?>" target="_blank"><button type="button" class="btn btn-danger">مصروفات السيارة</button></a>
		<input type="text" id="mydate" style="float:left; width:90px; padding:5px; background-color:transparent; color:#00b0ac " readonly />
		<script>
			$(function() {
				var calendar = $.calendars.instance('');
				$('#mydate').calendarsPicker({
					calendar: calendar
				});
			});
		</script>
	</div>
	<div class="container-fluid" style="margin-top:70px;">
		<div class="form-row">
			<div class="col text-center">
				<?php
				$roas = current_car($carid);
				echo "<h4 style='font-weight:bold'> طلب صيانة سيارة " . $roas['kind'] . " لوحة رقم " . $roas['lh'] . " موديل " . $roas['model'] . " اللون " . $roas['color'] . " </h4>";
				?>
				<input type="hidden" id="brand" name="brand" value="<?php echo $roas["brand"] ?>" />
			</div>
		</div>
		<br /><br />
		<div class="form-row">
			<div class="col-3">
				<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-8" style="font-size:0.8em;">
				<div class="form-row">
					<div class="col" id="msg">
						<!--Alert from signup form-->
					</div>
				</div>
				<form method="post" class="form-horizontal" id="send_respond" style="float:center;">
					<div class="form-row">
						<div class="col-1">
							<label for="req_id" class="control-label" style="float:left;">رقم الطلب</label>
						</div>
						<div class="col-1">
							<input type="text" style="background-color:#0F0F0F;color:#22D222" class="form-control" id="req_id" name="req_id" readonly />
						</div>
						<div class="col-2">
							<label for="ask_date" class="control-label" style="float:left;">تاريخ الطلب</label>
						</div>
						<div class="col-2">
							<input type="text" class="form-control" id="ask_date" name="ask_date" readonly />
						</div>
						<div class="col-2">
							<label for="kmm" class="control-label" style="float:left;">قراءة العداد</label>
						</div>
						<div class="col-2">
							<input type="number" class="form-control" id="kmm" name="kmm" readonly />
						</div>
					</div>
					<div class="form-row">
						<div class="col-1">
							<label for="description" class="control-label">وصف الحالة</label>
						</div>
						<div class="col-7">
							<textarea class="form-control" rows="3" id="description" name="description" readonly></textarea>
						</div>
						<div class="col-3">
							<img class="img-fluid rounded" width="60%" height="auto" style="float:left;" id="photo_before" name="photo_before" 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">
							<select id="done" name="done">
								<option value=0>مطلوبة للصيانة</option>
								<option value=1>في انتظار الرد</option>
								<option value=2>في انتظار قطع الغيار</option>
								<option value=3>في ورشة خارجية</option>
								<option value=4>تمت الصيانة</option>
							</select>
						</div>
						<div class="col-2">
							<label for="date_done" class="control-label" style="float:left;">تاريخ الحالة</label>
						</div>
						<script>
							$(function() {
								var calendar = $.calendars.instance('');
								$('#date_done').calendarsPicker({
									calendar: calendar
								});
							});
						</script>
						<div class="col-3 ">
							<input type="text" class="form-control" id="date_done" name="date_done" onchange="Gdate('date_done')" />
						</div>
					</div>
					<div class="form-row">
						<div class="col-1">
							<label for="feedback" class="control-label">تقرير الصيانة</label>
						</div>
						<div class="col-10">
							<textarea class="form-control" rows="3" id="feedback" name="feedback"></textarea>
						</div>
					</div>
					<div class="form-row">
						<div class="col-3 text-center">
							<label for="next_kmm" class="control-label">العداد التالي للصيانة الدورية</label>
							<input type="number" class="form-control" id="next_kmm" name="next_kmm" />
						</div>
						<div class="col-4">
							<label for="photo_after" class="control-label">ادخال صورة</label>
							<input type="file" class="form-control" id="photo_after" name="photo_after" />
						</div>
						<div class="col-2">
							<input type="submit" id="send_act" name="send_act" class="btn btn-success" value="ارسال الرد">
						</div>
					</div>
					<div class="form-row">
						<div class="col-12">
							<img class="img-fluid rounded" width="60%" height="auto" style="float:left;" id="photo_after2" name="photo_after2" src="" alt="بدون صورة" />
						</div>
					</div>
					<div id="request_image"></div>
					<p><br /></p>
				</form>
			</div><!-- end of body element -->
		</div>
	</div>
	<div class="modal fade" id="invoice_for_request" tabindex="-1" role="dialog">
		<div class="modal-dialog modal-lg" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<div class="col-2  text-right">
						<h5 class="modal-title">اضافة قطع غيار من فاتورة</h5>
					</div>
					<div class="col-4 text-right ">
						<input type="search" id="modal_inovice" class="form-control" />
					</div>
					<div class="col-2  text-right">
						<button id="modal_search_inovice" vv="" class="btn btn-outline-primary"><i class="fa fa-search" aria-hidden="true"></i> بحث</button>
					</div>
					<div class="col-2  text-right"></div>
					<div class="col-1 text-left">
						<button type="button" class="close  text-left" data-dismiss="modal" aria-label="Close">
							<span aria-hidden="true" style="font-size:20px;color:red;">&times;</span>
						</button>
					</div>
				</div>
				<div class="modal-body">
					<form method="post" class="form-horizontal" id="modal_invoice_items" style="float:center;">
						<input type="hidden" id="modal_req_id" name="modal_req_id" readonly />
						<input type="hidden" id="modal-mat_in_id" name="modal-mat_in_id" readonly />
						<div class="form-row">
							<div class="col-12">
								<table style="color:#00b0ac ;" id="invoice_table" class="table table-bordered table-hover">
									<thead>
										<tr>
											<th class="text-center" width="5%">صرف</th>
											<th class="text-center" width="35%">اسم القطعة</th>
											<th class="text-center" width="5%">رصيد</th>
											<th class="text-center" width="10%">ماركة السيارة</th>
											<th class="text-center" width="15%">اسم الفني</th>
											<th class="text-center" width="8%">سعر القطعة</th>
											<th class="text-center" width="10%">تاريخ الفاتورة</th>
											<th class="d-none" width="1%">s</th>
											<th class="d-none" width="1%">s</th>
										</tr>
									</thead>
									<tbody id="invoice_balance">
									</tbody>
								</table>
							</div><!-- end of avilable invoice items -->
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<div class="col-6 text-right">
						<button type="button" id="saveOutvoice" class="btn btn-primary">صرف من المخزن</button>
					</div>
					<div class="col-6 text-right">
						<button type="button" class="btn btn-default" data-dismiss="modal">خروج</button>
					</div>
				</div>
			</div>
		</div>
		<?php
		$output3 = array();
		$invoice_sr = $db->prepare("SELECT DISTINCT mat_in_id , concat(store_balance.invoice_no,' ', material_supp.mat_name) as invoice , mat_invoice_date FROM store_balance , material_supp
									 WHERE store_balance.mat_supp_id = material_supp.mat_supp_id ORDER by mat_invoice_date ");
		$invoice_sr->execute();
		$i = 0;
		if ($invoice_sr->rowCount() > 0) {
			while ($row = $invoice_sr->fetch()) {
				$output3[$i] = array(
					"value" => $row["invoice"],
					"data" => $row["mat_in_id"]
				);
				$i++;
			}
		} else {
			$output3[0] = 'لا يوجد ';
		}
		?>
		<script>
			var arr = JSON.parse('<?php echo json_encode($output3); ?>');
			$('#modal_inovice').autocomplete({
				lookup: arr,
				maxHeight: 200,
				onSelect: function(suggestion) {
					$('#modal_search_inovice').attr("vv", suggestion.data);
					//  one_invoice(suggestion.data);
				},
			})
		</script>
	</div> <!-- modal invoice items -->
	<!-- single item invoices -->
	<div class="modal fade" id="items_for_request" tabindex="-1" role="dialog">
		<div class="modal-dialog modal-lg" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<h5 class="modal-title text-right">صرف قطع غيار من فواتير</h5>
					<button id="modal_search_items" class="btn btn-outline-success"> تصفية <i class="fa fa-filter" aria-hidden="true"></i></button>
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true" style="font-size:20px;color:red;">&times;</span>
					</button>
				</div>
				<div class="modal-body">
					<form method="post" class="form-horizontal" id="modal_invoice_items">
						<div class="form-row">
							<div class="col-12">
								<div class="table-responsive-md">
									<table id="invoice_table" class="table table-bordered table-hover">
										<thead class="thead-inverse">
											<tr>
												<th class="text-center" width="5%">صرف</th>
												<th class="text-center" width="25%">اسم القطعة</th>
												<th class="text-center" width="5%">رصيد</th>
												<th class="text-center" width="10%">ماركة السيارة</th>
												<th class="text-center" width="15%">اسم الفني</th>
												<th class="text-center" width="8%">سعر القطعة</th>
												<th class="text-center" width="17%">المورد</th>
												<th class="text-center" width="10%">تاريخ الفاتورة</th>
												<th class="d-none" width="1%">s</th>
												<th class="d-none" width="1%">s</th>
											</tr>
										</thead>
										<tbody id="item_balance">
										</tbody>
									</table>
								</div>
							</div><!-- end of avilable invoice items -->
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<div class="col-6 text-right">
						<button type="button" id="saveOutitem" class="btn btn-primary">صرف من المخزن</button>
					</div>
					<div class="col-6 text-right">
						<button type="button" class="btn btn-default" data-dismiss="modal">خروج</button>
					</div>
				</div>
			</div>
		</div>
	</div> <!-- modal item -->
	</div><!-- container fluid -->
	<script>
		$(document).ready(function() {
			myreq(<?php echo $req_idd;  ?>);
			$('#close').click(function() {
				close();
			});
			$('#send_respond').on('submit', function(event) {
				event.preventDefault();
				var extension = $('#photo_after').val().split('.').pop().toLowerCase();
				if (extension != '') {
					if (jQuery.inArray(extension, ['gif', 'png', 'jpg', 'jpeg']) == -1) {
						alert("Invalid Image File");
						$('#photo_after').val('');
						return false;
					}
					var oFReader = new FileReader();
					oFReader.readAsDataURL(document.getElementById("photo_after").files[0]);
					var f = document.getElementById("photo_after").files[0];
					var fsize = f.size || f.fileSize;
					if (fsize > 1700000) {
						alert("حجم الصورة كبير جدا ...");
						return false;
					}
				}
				$.ajax({
					url: "req_respond.php",
					method: "POST",
					data: new FormData(this),
					contentType: false,
					processData: false,
					success: function(data) {
						$('#request_image').html(data);
						//  $('#user_form')[0].reset();
					},
					error: function(result) {
						alert(result.statusText);
					}
				});
			}); //end of button add image

			$('.req_data').click(function(event) {
				event.preventDefault();
				var req_id = $(this).attr("idd");
				myreq(req_id);
			}); //viewe requst single data for a car
			function myreq(req_id) {
				$.ajax({
					url: "request_single.php",
					method: "POST",
					data: {
						req_id: req_id
					},
					dataType: "json",
					success: function(data) {
						$('#req_id').val(data.req_id);
						$('.a').attr("req", data.req_id);
						$('#req_report').attr("href", "../ment/request_ment_rep.php?id=" + (data.req_id));
						$('#req_xls').attr("href", "../ment/request_ment_xls.php?id=" + (data.req_id));
						$('#ask_date').val(data.ask_date);
						$('#description').val(data.description);
						$('#kmm').val(data.kmm);
						$('#photo_before').attr("src", "../photo/ment_before/" + (data.photo_before));
						$('#done').val(data.done);
						$('#photo_after2').attr("src", "../photo/ment_after/" + (data.photo_after));
						$('#date_done').val(data.date_done);
						$('#feedback').val(data.feedback);
						$('#next_kmm').val(data.next_kmm);
					}
				});
			}

			$('#invoice_for_request').on('show.bs.modal', function(event) {
				var recipient = $(".a").attr("req");
				var modal = $(this);
				modal.find('.modal-title').text('صرف من فاتورة ');
				modal.find('#modal_req_id').val(recipient);
			})

			$('#modal_search_inovice').click(function(event) {
				event.preventDefault();
				var mat_in_id = $(this).attr("vv");
				one_invoice(mat_in_id);
			});

			function one_invoice(mat_in_id) {
				$.ajax({
					url: "../ment/invoice_balance_single.php",
					method: "POST",
					data: {
						mat_in_id: mat_in_id
					},
					success: function(data) {
						$('#invoice_balance').html(data);
					}
				});
			}

			$('#items_for_request').on('show.bs.modal', function(event) {
				var brand = "all";
				avialbel_items(brand);
			})
			$('#saveOutvoice').click(function() {
				var req_id = $('#req_id').val();
				var mydate = $('#mydate').val();
				var kmm = $('#kmm').val();
				var out_qty = [];
				var out_line_id = [];
				var out_item_id = [];
				var out_unit_price = [];
				var technician = [];
				$('.out_qty').each(function() {
					out_qty.push($(this).text());
				});
				$('.out_line_id').each(function() {
					out_line_id.push($(this).text());
				});
				$('.out_item_id').each(function() {
					out_item_id.push($(this).text());
				});
				$('.out_unit_price').each(function() {
					out_unit_price.push($(this).text());
				});
				$('.technician').each(function() {
					technician.push($(this).text());
				});
				$.ajax({
					url: "../ment/line_out_v.php",
					method: "POST",
					data: {
						req_id: req_id,
						mydate: mydate,
						kmm: kmm,
						out_qty: out_qty,
						out_line_id: out_line_id,
						out_item_id: out_item_id,
						out_unit_price: out_unit_price,
						technician: technician
					},
					success: function(data) {
						alert(data);
					}
				});
			});
			$('#modal_search_items').click(function(event) {
				event.preventDefault();
				var brand = $("#brand").val();
				avialbel_items(brand);
			});

			function avialbel_items(brand) {
				$.ajax({
					url: "../ment/item_balance_single.php",
					method: "POST",
					data: {
						brand: brand
					},
					success: function(data) {
						$('#item_balance').html(data);
					}
				});
			}

			$('#saveOutitem').click(function() {
				var req_id = $('#req_id').val();
				var mydate = $('#mydate').val();
				var kmm = $('#kmm').val();
				var out_qty = [];
				var out_line_id = [];
				var out_item_id = [];
				var out_unit_price = [];
				var technician = [];
				$('.s_qty').each(function() {
					out_qty.push($(this).text());
				});
				$('.s_line_id').each(function() {
					out_line_id.push($(this).text());
				});
				$('.s_item_id').each(function() {
					out_item_id.push($(this).text());
				});
				$('.s_unit_price').each(function() {
					out_unit_price.push($(this).text());
				});
				$('.s_technician').each(function() {
					technician.push($(this).text());
				});
				$.ajax({
					url: "../ment/line_out_v.php",
					method: "POST",
					data: {
						req_id: req_id,
						mydate: mydate,
						kmm: kmm,
						out_qty: out_qty,
						out_line_id: out_line_id,
						out_item_id: out_item_id,
						out_unit_price: out_unit_price,
						technician: technician
					},
					success: function(data) {
						alert(data);
					}
				});
			});

		}); // end of script
		function Gdate(ss) {
			var date = $('#' + ss).val().split('/');
			var year = parseInt(date[2], 10);
			if (year < 100) {
				year = year + 2000;
			}
			var month = parseInt(date[1], 10);
			if (month > 12) {
				month = 12;
			} else if (month < 1) {
				month = 1;
			}
			var day = parseInt(date[0], 10);
			if (day > 31) {
				day = 31;
			} else if (day < 1) {
				day = 1;
			}
			$('#' + ss).val(year + "/" + month + "/" + day);
		}
	</script>
</body>

</html>
