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

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

$cust 		= $_REQUEST['id'];
	include "../op/wanted.php";
$myCust = $db->prepare('SELECT * from customers where cust_id = ? ');
			$myCust->bindParam(1,$cust);
			$myCust->execute();
		  if($myCust->rowCount()>0){$result = $myCust->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>تحديث بيانات المستأجر</title>
	<link rel="stylesheet" href="../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/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:#a8a89f;">
	<div class="container">
				<br /><br />
				<input style="float:right ;" type="submit" class="btn btn-danger hidden-print" name="close" id="close" value=" خروج "/>
        <br /><br />
        <h4 style="text-align:center"> تحديث بيانات السيد <?php echo $result['cust_name']; ?> الجنسية <?php echo $result['nation']; ?> جوال رقم <?php echo $result['phone']; ?></h4>
				   <br /><br /><br />
					 <!-- <div class="div-result navbar-inner " style="float:right"> -->
					 				  <!-- Nav tabs -->
		<ul class="nav nav-tabs navbar-right" role="tablist"  >
				<li role="presentation" class="active" style="float:right;"><a href="#tab_basic" aria-controls="home" role="tab" data-toggle="tab">بيانات اساسية</a></li>
				<li role="presentation" style="float:right;"><a href="#tab_photo" aria-controls="profile" role="tab" data-toggle="tab">صور الهوية و الرخصة</a></li>
			    <li role="presentation" style="float:right;"><a href="#tab_tel" aria-controls="profile" role="tab" data-toggle="tab">سبل التواصل</a></li>
				<li role="presentation" style="float:right;"><a href="#tab_work" aria-controls="profile" role="tab" data-toggle="tab">جهة العمل</a></li>
				<li role="presentation" style="float:right;"><a href="#tab_contract" 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_basic">
	<form class="form-horizontal" id="form_basic"  method="POST" class="form-horizontal" style="float:center;">
											 																									<br /><br /><br />
		<input type="hidden" id="cust_basic" name="cust_basic" value="<?php echo $result['cust_id']; ?>"/>
			<div id="form_basic_msg"></div>
		<div class="row" style="padding:2px;">
		<div class="col-md-2"style="float:right;">
			<label for="cust_name" class="control-label"style="float:left;">اسم المستأجر</label>
		</div>
		<div class="col-md-4"style="float:right;padding:2px;">
			<input type="text" class="form-control" style="float:right;" id="cust_name" name="cust_name" value="<?php echo $result['cust_name']; ?>" placeholder="اسم المستأجر"/>
		</div>
		<div class="col-md-2"style="float:right;padding:2px;">
			<input type="text" class="form-control" style="float:right;" id="nation" name="nation" value="<?php echo $result['nation']; ?>" placeholder="الجنسية"/>
		</div>
		<div class="col-md-2"style="float:right;">
			<label for="xn" class="control-label"style="float:left;">نسخة رقم</label>
		</div>
		<div class="col-md-1"style="float:right;padding:2px;">
			<input type="text" class="form-control" style="float:right;" id="xn" name="xn" value="<?php echo $result['xn']; ?>" placeholder="نسخة" />
		</div>
	</div>
	<div class="row" style="padding:4px;">
		<div class="col-md-2"style="float:right;">
			<label for="idSA" class="control-label"style="float:left;">رقم الهوية</label>
		</div>
		<div class="col-md-2"style="float:right;padding:2px">
			<input type="text" class="form-control" style="float:right;" id="idSA" name="idSA" value="<?php echo $result['idSA']; ?>" placeholder="رقم الهوية" />
		</div>
		<div class="col-md-2"style="float:right;">
			<label for="id_From" class="control-label"style="float:left;">صادر من</label>
		</div>
		<div class="col-md-2"style="float:right;padding:2px;">
			<input type="text" class="form-control" style="float:right;" id="id_From" name="id_From" value="<?php echo $result['id_From']; ?>" placeholder="مصدرها"/>
		</div>
		<div class="col-md-2"style="float:right;">
			<label for="id_Expire" class="control-label"style="float:left;">تنتهي</label>
		</div>
		<div class="col-md-2"style="float:right;padding:2px;">
			<input type="text" style="float:right;" class="form-control" id="id_Expire" name="id_Expire" value="<?php echo $result['id_Expire']; ?>" readonly placeholder="نهاية الهوية">
				<script>
					$(function() {
							var calendar = $.calendars.instance('ummalqura');
							$('#id_Expire').calendarsPicker({calendar: calendar});
							$('#h_birth').calendarsPicker({calendar: calendar});
									});
				</script>
		</div>
	</div>
	<div class="row" style="padding:2px;">
		<div class="col-md-2"style="float:right;">
			<label for="license" class="control-label"style="float:left;">رقم الرخصة</label>
		</div>
		<div class="col-md-2"style="float:right;padding:2px;">
			<input type="text" class="form-control" style="float:right;" id="license" name="license" value="<?php echo $result['license']; ?>" placeholder="رقم الرخصة"/>
		</div>
		<div class="col-md-2"style="float:right;">
			<label for="lic_from" class="control-label"style="float:left;">صادر من</label>
		</div>
		<div class="col-md-2"style="float:right;padding:2px">
			<input type="text" class="form-control" style="float:right;" id="lic_from" name="lic_from" value="<?php echo $result['lic_from']; ?>" placeholder="مصدرها"/>
		</div>
		<div class="col-md-2"style="float:right;">
			<label for="lic_expire" class="control-label"style="float:left;">تنتهي</label>
		</div>
		<div class="col-md-2"style="float:right;padding:2px;">
			<input type="text" style="float:right;" class="form-control" id="lic_expire" name="lic_expire" value="<?php echo $result['lic_expire']; ?>" readonly placeholder="نهاية الرخصة" />
				<script>
					$(function() {
					var calendar = $.calendars.instance('ummalqura');
					$('#lic_expire').calendarsPicker({calendar: calendar});
								});
				</script>
		</div>
	</div>
	<div class="row" style="padding:2px;">
			<div class="col-md-2"style="float:right;">
				<label for="h_birth" class="control-label"style="float:left;">تاريخ الميلاد بالهجري</label>
			</div>
			<div class="col-md-2"style="float:right;padding:2px">
				<input type="text" class="form-control" style="float:right;" id="h_birth" name="h_birth" />
		   </div>
			<div class="col-md-2"style="float:right;">
				<label for="birth" class="control-label"style="float:left;">تاريخ الميلاد ميلادي</label>
			</div>
				<div class="col-md-2"style="float:right;padding:2px;">
				<input type="text" style="float:right;" class="form-control" id="birth" name="birth" value="<?php echo $result['birth']; ?>" />
				<script>
					$(function() {
						var calendar = $.calendars.instance('');
						$('#birth').calendarsPicker({calendar: calendar});
						});
				</script>
			</div>
			<div id="age"></div>
			</div>
				<div class="row" style="padding:2px;">
					<div class="col-md-2"style="float:right;">
						<label for="job" class="control-label"style="float:left;">المهنة</label>
					</div>
					<div class="col-md-2"style="float:right;padding:2px">
						<input type="text" class="form-control" style="float:right;" id="job" name="job" value="<?php echo $result['job']; ?>" placeholder="المهنة"/>
					</div>
					<div class="col-md-2"style="float:right;">
						<label for="religion" class="control-label"style="float:left;">الديانة</label>
					</div>
						<div class="col-md-2"style="float:right;padding:2px;">
						<input type="text" style="float:right;" class="form-control" id="religion" name="religion" value="<?php echo $result['religion']; ?>" placeholder="الديانة"/>
					</div>
			</div>
          <br /><br />
			<div>
				<button type="submit" class="btn btn-success save" farm="form_basic" id="save_basic" data-loading-text="Loading...">  حفظ التحديث <i class="glyphicon glyphicon-ok-sign"></i></button>
			</div> <!-- /modal-footer -->
			</form> <!-- /.form -->
		</div><!-- /basic 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 /><br /><br /><br />
					 				 		<input type="hidden" id="cust_dd" 	class="hidden-print"	name="cust_dd" value="<?php echo $result['cust_id']; ?>"/>
											<input type="hidden" id="photo_idSA" name="photo_idSA" value="<?php echo $result['idSA']; ?>" placeholder="رقم الهوية" />

											<a href="cust_print.php?cust_id=<?php echo $result['cust_id']; ?>"target="_blank" class="hidden-print"><button type="button" class="btn btn-info btn-xs ">طباعة الرخصة و الهوية</button></a>

											<div class="row" style="padding:4px;">
												<img id="cust_preview" src="../photo/customers/<?php echo $result['cust_photo']; ?>" class="img-responsive img-rounded" width="100%" height="auto" style="" alt="صورة الرخصة غير متاحة" />
											</div>
								<div class="row" style="padding:4px;">
 					 							<div class="col-md-6"style="float:right;">
				                  <input style="float:right ; font-wight:bold;" type="file" class="btn btn-primary hidden-print" name="cust_photo" id="cust_photo"  />
												</div>
												 		<div class="col-md-2"style="float:right;">
				                   				<input style="float:right ; font-wight:bold;" type="submit" class="btn btn-primary hidden-print" id="photo_paly" farm="form_photo" value="حفظ تعديلات الرخصة" />
														</div>
														<div id="form_photo_msg"></div>
								</div>
					 	</form> <!-- /form -->
					 </div><!-- car licens photo -->

											<div role="tabpanel" class="tab-pane" id="tab_tel">
											 <form class="form-horizontal" id="form_tel"  method="POST">
											 <br /><br /><br />
											 <input type="hidden" id="cust_tel" name="cust_tel" value="<?php echo $result['cust_id']; ?>"/>
											 <div id="form_tel_msg"></div>

											 <div class="row" style="padding:4px;">
												 <div class="col-md-2"style="float:right;">
												 <label for="phone" class="control-label "style="float:left;">رقم الجوال</label>
												 </div>
												 <div class="col-md-2"style="float:right;padding:2px;">
													 <input type="text" style="float:right;" class="form-control" id="phone" name="phone" value="<?php echo $result['phone']; ?>" placeholder="رقم الجوال" />
												 </div>
												 <div class="col-md-2"style="float:right;">
												 <label for="phone2" class="control-label "style="float:left;">جوال2</label>
												 </div>
												 <div class="col-md-2"style="float:right;padding:2px">
													 <input type="text" style="float:right;" class="form-control" id="phone2" name="phone2" value="<?php echo $result['phone2']; ?>" placeholder="رقم جوال2">
												 </div>
										 </div>
											 <div class="row" style="padding:4px;">
													 <div class="col-md-2"style="float:right;">
														 <label for="address" class="control-label"style="float:left;">العنوان</label>
													 </div>
													 <div class="col-md-7"style="float:right;padding:2px">
														 <input type="text" class="form-control" style="float:right;" id="address" name="address" value="<?php echo $result['address']; ?>" placeholder="العنوان"/>
													 </div>
													 <div class="col-md-1"style="float:right;padding:2px">
														 <input type="text" style="float:right;" class="form-control" id="dgree" name="dgree" value="<?php echo $result['dgree']; ?>" placeholder="تقييم">
													 </div>
											 </div>

									 <div class="row" style="padding:4px;">
													 <div class="col-md-2"style="float:right;">
													 <label for="comm" class="control-label"style="float:left;">ملاحظات</label>
													 </div>
													 <div class="col-md-10"style="float:right;padding:2px">
													 <input type="text" class="form-control" style="float:right;" id="comm" name="comm" value="<?php echo $result['comm']; ?>" placeholder="ملاحظات ">
													 </div>
										 </div>
												 <br /><br />
											 <div>
												 <button type="submit" class="btn btn-success save" farm="form_tel" id="save_tel" data-loading-text="Loading..."> <i class="glyphicon glyphicon-ok-sign"></i> حفظ التعديلات </button>
											 </div> <!-- /modal-footer -->
											 </form> <!-- /.form -->
										 </div><!-- /tab telecomunication -->

											<div role="tabpanel" class="tab-pane" id="tab_work">
											 <form class="form-horizontal" id="form_work"  method="POST">
												 <input type="hidden" id="cust_work" name="cust_work" value="<?php echo $result['cust_id']; ?>"/>
											 <br /><br /><br /><br />
											 <div id="form_work_msg"></div>
											 	<div class="row" style="padding:4px;">
													 <div class="col-md-2"style="float:right;">
														 <label for="working_lic" class="control-label"style="float:left;">رخصة العمل</label>
													 </div>
													 <div class="col-md-2"style="float:right;padding:2px;">
														 <input type="text" class="form-control" style="float:right;" id="working_lic" name="working_lic" value="<?php echo $result['working_lic']; ?>" placeholder="رقم رخصة العمل"/>
													 </div>
													 <div class="col-md-2"style="float:right;">
													 <label for="working_woner" class="control-label "style="float:left;">صاحب العمل</label>
													 </div>
													 <div class="col-md-5"style="float:right; padding:2px;">
														 <input type="text" style="float:right;" class="form-control" id="working_woner" name="working_woner" value="<?php echo $result['working_woner']; ?>" placeholder="اسم جهة العمل">
													 </div>
											 </div>
											 <div class="row" style="padding:4px;">
												 <div class="col-md-2"style="float:right;">
												 <label for="w_phone" class="control-label "style="float:left;">تليفون العمل</label>
												 </div>
												 <div class="col-md-2"style="float:right;padding:2px;">
													 <input type="text" style="float:right;" class="form-control" id="w_phone" name="w_phone" value="<?php echo $result['w_phone']; ?>" placeholder="رقم الجوال" />
												 </div>
												 <div class="col-md-2"style="float:right;">
												 <label for="w_phone2" class="control-label "style="float:left;">تليفون العمل2</label>
												 </div>
												 <div class="col-md-2"style="float:right;padding:2px">
													 <input type="text" style="float:right;" class="form-control" id="w_phone2" name="w_phone2" value="<?php echo $result['w_phone2']; ?>" placeholder="رقم جوال2">
												 </div>
										 </div>
											 <div class="row" style="padding:4px;">
													 <div class="col-md-2"style="float:right;">
														 <label for="working_address" class="control-label"style="float:left;">عنوان العمل</label>
													 </div>
													 <div class="col-md-10"style="float:right;padding:2px">
														 <input type="text" class="form-control" style="float:right;" id="working_address" name="working_address" value="<?php echo $result['working_address']; ?>"  placeholder="عنوان جهة العمل"/>
													 </div>
											 </div>
												<br /><br />
											 <div>
												 <button type="submit" class="btn btn-success save" farm="form_work" id="save_work" data-loading-text="Loading..."> <i class="glyphicon glyphicon-ok-sign"></i> حفظ التعديلات </button>
											 </div> <!-- /modal-footer -->
											 </form> <!-- /.form -->
										 </div><!-- /tab work -->

	<div role="tabpanel" class="tab-pane fad" id="tab_contract">
			<br /><br /><br /><br /><br />
	<div class="table-responsive">
		<table class="table table-bordered table-hover">
		<thead><tr>
					<th class="text-center" width="7%">رقم العقد</th>
					<th class="text-center" width="7%">متأخرات</th>
					<th class="text-center" width="10%">إغلاق العقد</th>
					<th class="text-center" width="8%">طراز</th>
					<th class="text-center" width="6%">رقم</th>
					<th class="text-center" width="9%">موديل</th>
					<th class="text-center" width="5%">يومي</th>
					<th class="text-center" width="5%">أيام</th>
					<th class="text-center" width="6%">إضافي</th>
					<th class="text-center" width="30%">بيان الإضافي</th>
				    <th class="text-center" width="7%">المدفوع</th>
			</tr>
		</thead>
		<tbody>
	<?php
		$output = '';
		$sql =$db->prepare("SELECT contracts.contract_id ,contracts.date_in ,contracts.day_cost , contracts.addtion_desc, cars.kind , cars.numbers , cars.model , contracts.day_cost
							from  contracts , cars
			               WHERE contracts.carid = cars.carid
							AND contracts.cust_id = ?  order by contracts.date_in DESC LIMIT 19");
		$sql->bindParam(1,$cust);
		$sql->execute();
	if($sql->rowCount()>0)
		{
	while($row = $sql->fetch())
		{
			$ss = $row['contract_id'];
			$wanted    = new  Wanted($db,$ss) ;
			$pay       = $wanted->wanted() ;
			$ww        = $pay["wanted"] ;
			$tot_add   = $pay["tot_addtion"] ;
			$days_qty  = $pay["days_qty"] ;
		 if($ww > 0){ $output .='<tr><td>
								<a href="cont2debt.php?id=' . $ss . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-danger btn-xs btn-block ">' . $ss . '</button></a></td>
								<td style="color:#DA2D07; background-color:#F0F7E4;font-weight:bold;">' . $ww . '</td>';
					 }
		else { $output .= '<tr><td><a href="cont2debt.php?id=' . $ss . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-danger btn-xs btn-block ">' . $ss . '</button></a></td>
								<td>' . $ww . '</td>' ;}
				$output .= '<td class = "table-info">' . $row['date_in'] . '</td>
							<td class="text-center" >' . $row['kind'] . '</td>
							<td class="text-center" >' . $row['numbers'] . '</td>
							<td class="text-center" >' . $row['model'] . '</td>
							<td class="text-center" >' . $row['day_cost'] . '</td>
							<td class="text-center" >' . $days_qty . '</td>
							<td class="text-center" >' . $tot_add . '</td>
							<td class="text-center" >' . $row['addtion_desc'] . '</td>
							<td class="text-center" >' . $pay["payed"] . '</td>';
				 }
			}
		else {$output .= '<tr><td colspan="11">لا يوجد سجلات ...</td></tr>';}
		echo $output;
		?>
		</tbody><tfoot></tfoot></table>
	</div>
</div>
	<p><br/><br /><br /><br /><br /></p>
	<div class="form-group">
			<div class="col-md-12">
			<!-- <input style="float:right ;" type="button" id="update_car" name="update_car" class="btn btn-success btn-lg " value="تحديث بيانات السيارة"/> -->
			</div>
							</div>
						</form>
          </div>
  <script>
$(document).ready(function(){
  $('#close').click(function(){close();});

  $(".save").click(function(event){
			  event.preventDefault();
				var farm = $(this).attr("farm");
				if(farm =="form_basic"){
					var tt = $('#id_Expire').val();
				    if(tt.length != 0 ){convertDates('ummalqura','id_Expire','id_Expire');}
				var dd = $('#lic_expire').val();
					 if(dd.length != 0 ){convertDates('ummalqura','lic_expire','lic_expire');}
			 	var gg = $('#h_birth').val();
	 					 if(gg.length != 0 ){convertDates('ummalqura','h_birth','birth');}
					}
	 $.ajax({
			    url     : "../actions/cust_update.php",
			    method  : "POST",
					 data    :$("#"+farm).serialize(),
			    success : function(data){ $("#"+farm+"_msg").html(data); },
			    error   :function(result){
			      alert(result.statusText);
			      }
			  });
			});

$('#form_photo').on('submit', function(event){
						event.preventDefault();
		var cust_dd = $('#cust_dd').val();
		var extension = $('#cust_photo').val().split('.').pop().toLowerCase();
    if(extension != '')
         { if(jQuery.inArray(extension, ['png','jpg','jpeg']) == -1)
            {  alert("صور jpg فقط");
               $('#cust_photo').val('');
           return false;
             }
    	var oFReader = new FileReader();
    		oFReader.readAsDataURL(document.getElementById("cust_photo").files[0]);
    	var f = document.getElementById("cust_photo").files[0];
    	var fsize = f.size||f.fileSize;
    if(fsize > 1600000){alert("حجم الصورة كبير جدا ...");return false;	}
		$.ajax({
			 url		:"../actions/cust_update.php",
			 method		:"POST",
			 data		:new FormData(this),
			 contentType	:false,
			 processData:false,
		success:function(data)
			{
			 $('#form_photo')[0].reset();
			 $('#cust_preview').attr("src","../photo/customers/"+data);
			 $('#form_photo_msg').html(data);
			}
			});
                }
	});//end of button add image

		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; }
			}

			});
</script>
    </body>
</html>
