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

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

$mySupp = $db->prepare('SELECT supp_id,supp_name from supplycomp where supp_id = ? ');
$mySupp->bindParam(1, ($_GET['id']));
$mySupp->execute();
if ($mySupp->rowCount() > 0) {
	$result = $mySupp->fetch(PDO::FETCH_ASSOC);
}
?>
<!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>بدر14 لخدمات السيارات</title>
	<link rel="stylesheet" href="../css/bootstrap.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/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:#f0f0f0;">
	<div class="container-fluid">
		<br />
		<h4 style="text-align:center">عقود السيارات الواردة من شركة </h4>
		<br />
		<h3 style="text-align:center"> <?php echo $result['supp_name']; ?> </h3>
		<br /><br /><br />
		<div class="row">
			<div class="col-xs-1 col-md-1"></div>
			<div class="col-xs-10 col-md-10">
				<form method="post" id="contForm" class="form-horizontal" style="float:center;font-weight: bold; font-size: 14px; color: #000;">
					<input type="hidden" id="supp_id" name="supp_id" value="<?php echo $result['supp_id']; ?>" />
					<input type="hidden" id="cont_id" name="cont_id" />
					<div class="row" style="padding:4px;">
						<div class="col-md-2" style="float:right;">
							<label for="contract_code" class="control-label" style="float:right;">رقم العقد</label>
						</div>
						<div class="col-md-2" style="float:right;">
							<input type="text" class="form-control input" style="float:right;" id="contract_code" name="contract_code" placeholder="رقم العقد " />
						</div>
						<div class="col-md-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
								});
								$('#recpeit').calendarsPicker({
									calendar: calendar
								});
								$('#first_kest').calendarsPicker({
									calendar: calendar
								});
							});
						</script>
						<div class="col-xs-12 col-md-2" style="float:right;">
							<input type="text" style="float:right;" class="form-control" id="ask_date" name="ask_date" >
						</div>
						<div class="col-xs-6 col-md-2" style="float:right;">
							<label for="recpeit" class="control-label" style="float:left;">تاريخ الإستلام</label>
						</div>
						<div class="col-xs-12 col-md-2" style="float:right;">
							<input type="text" style="float:right;" class="form-control " id="recpeit" name="recpeit" >
						</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-10" style="float:right;">
							<input type="text" class="form-control " style="float:right;" id="description" name="description" placeholder=" وصف محتويات العقد من سيارات ">
						</div>
					</div>
					<div class="row" style="padding:4px;">
						<div class="col-md-2" style="float:right;">
							<label for="first_payment" class="control-label" style="float:right;">الدفعة الأولي</label>
						</div>
						<div class="col-md-2" style="float:right;">
							<input type="number" class="form-control " style="float:right;" id="first_payment" name="first_payment" placeholder="الدفعة الأولي" />
						</div>
						<div class="col-md-1" style="float:right;">
							<label for="kest_amount" class="control-label" style="float:left;">قسط</label>
						</div>
						<div class="col-md-2" style="float:right;">
							<input type="number" style="float:right;" class="form-control input-md" id="kest_amount" name="kest_amount" value="4500">
						</div>
						<div class="col-md-1" style="float:right;">
							<input type="number" style="float:right;" class="form-control" id="kest_qty" name="kest_qty" value="36">
						</div>
						<div class="col-md-2" style="float:right;">
							<label for="first_kest" class="control-label" style="float:left;">أول قسط</label>
						</div>
						<div class="col-md-2" style="float:right;">
							<input type="text" style="float:right;" class="form-control " id="first_kest" name="first_kest">
						</div>
					</div>
					<div class="row" style="padding:4px;">
						<div class="col-xs-6 col-md-2" style="float:right;">
							<label for="last_payment" class="control-label" style="float:right;">الدفعة الأخيرة</label>
						</div>
						<div class="col-xs-6 col-md-2" style="float:right;">
							<input type="number" class="form-control input" style="float:right;" id="last_payment" name="last_payment" placeholder="الدفعة الاخيرة" />
						</div>
						<div class="col-xs-6 col-md-2" style="float:right;">
							<label for="total_cost" class="control-label" style="float:left;">إجمالي التكلفة</label>
						</div>
						<div class="col-xs-12 col-md-2" style="float:right;">
							<input type="number" style="float:right;" class="form-control input" id="total_cost" name="total_cost" placeholder="إجمالي العقد">
						</div>
						<div class="col-xs-6 col-md-2" style="float:right;">
							<label for="cars_qty" class="control-label" style="float:left;">عدد السيارات</label>
						</div>
						<div class="col-xs-12 col-md-2" style="float:right;">
							<input type="number" style="float:right;" class="form-control input" id="cars_qty" name="cars_qty" placeholder=" عدد السيارات">
						</div>
					</div>
					<div class="row" style="padding:4px;">
						<div class="col-xs-3 col-md-2" style="float:right;">
							<label for="comm" class="control-label" style="float:right;">ملاحظات</label>
						</div>
						<div class="col-xs-9 col-md-10" style="float:right;">
							<input type="text" class="form-control " style="float:right;" id="comm" name="comm" placeholder="ملاحظات العقد">
						</div>
					</div>
					<p><br /></p>
					<div class="form-group">
						<div class="col-xs-12 col-md-12">
							<input style="float:right ;" type="submit" class="btn btn-danger" name="close" id="close" value=" خروج " />
							<input style="float:right ;" type="button" id="add_cont" name="add_cont" class="btn btn-success" value="حفظ العقد">
							<input style="float:right ;" type="hidden" id="update_cont" name="update_cont" class="btn btn-primary" value="تحديث بيانات ">
						</div>
					</div>
				</form>
				<div id="msg"></div>
			</div>
		</div>
		<div>
			<!-- avilable contracts -->
			<?php
			$output = '';
			$myCont = $db->prepare('SELECT cont_id,contract_code,recpeit,description,cars_qty FROM supp_contracts WHERE supp_id = ? order by cont_id desc');
			$myCont->bindParam(1, ($_GET['id']));
			$myCont->execute();
			$output .= '
						<div class="table-responsive">
								 <table class="table table-bordered table-hover">
											<tr>
													 <th class="text-center" width="10%">حركة</th>
													 <th class="text-center" width="20%">رقم العقد</th>
													 <th class="text-center"  width="15%">تاريخ الاستلام</th>
													 <th class="text-center" width="45%">وصف محتوي العقد</th>
													 <th class="text-center" width="10%">عدد السيارات</th>
													</tr>';
			if ($myCont->rowCount() > 0) {
				while ($row = $myCont->fetch()) {
					$output .= '
											<tr>
													 <td>
												 <button type="button"  idd="' . $row["cont_id"] . '"  name="cont_update" class="btn btn-info cont_update btn-xs">تحديث</button>
													 </td>
													 <td class="text-center">' . $row['contract_code'] . '</td>
													 <td class="text-center">' . $row['recpeit'] . '</td>
													 <td>' . $row['description'] . '</td>
													 <td class="text-center">' . $row['cars_qty'] . '</td>
												</tr>
								 ';
				}
			} else {
				$output .= '<tr><td colspan="5">لا يوجد سجلات ...</td></tr>';
			}
			$output .= '</table></div>';
			echo $output;
			?>

		</div><!-- end of avilable conratcts -->

	</div>
	<script>
		$(document).ready(function() {
			$('#close').click(function() {
				close();
			});
			$("#add_cont").click(function(event) {
				event.preventDefault();
				$.ajax({
					url: "../actions/supp20Cont.php",
					method: "POST",
					data: $("#contForm").serialize(),
					success: function(data) {
						$("#msg").html(data);
					},
					error: function(result) {
						alert(result.statusText);
					}
				})
			}); //end of signup_button ajax code ?act='update'

			$(".cont_update").click(function(event) {
				event.preventDefault();
				var cont_id = $(this).attr("idd");
				$.ajax({
					url: "../actions/contract_single.php",
					method: "POST",
					data: {
						cont_id: cont_id
					},
					dataType: "json",
					success: function(data) {
						$('#cont_id').val(data.cont_id);
						$('#contract_code').val(data.contract_code);
						$('#ask_date').val(data.ask_date);
						$('#recpeit').val(data.recpeit);
						$('#description').val(data.description);
						$('#first_payment').val(data.first_payment);
						$('#kest_amount').val(data.kest_amount);
						$('#kest_qty').val(data.kest_qty);
						$('#first_kest').val(data.first_kest);
						$('#last_payment').val(data.last_payment);
						$('#total_cost').val(data.total_cost);
						$('#cars_qty').val(data.cars_qty);
						$('#comm').val(data.comm);
						$('#add_cont').attr("type", "hidden");
						$('#update_cont').attr("type", "button");
					}
				})
			}); //update contract
			$("#update_cont").click(function(event) {
				event.preventDefault();
				$.ajax({
					url: "../actions/contract_update.php",
					method: "POST",
					data: $("form").serialize(),
					success: function(data) {
						$("#msg").html(data);
					},
					error: function(result) {
						alert(result.statusText);
					}
				})
			}); //end of signup_button ajax code ?act='update'
		});
	</script>
</body>
</html>
