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

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

$contract_id = $_REQUEST['id'];
?>
<!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">
	<link rel="icon" href="../images/perfect.ico">
		<title>رفع معاملة محكمة</title>
		<link rel="stylesheet" href="../dist/css/bootstrap.min.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>
<style>
			.control-label , .table , .h6, h6 {	color:#A9A9A7;	}
			a{font-size:1.1em; font-weight:bold; color:#EDDFDF;}
			a:hover {		color:green;		}
</style>
<body style="background-color:#3C3838;">
		<div class="container-fluid" style="background-color:black;">
			<br />
			<div class="form-row align-items-center">
					<div class="col-2">
						<input style="margin-right:20px;" type="button" class="btn btn-danger" id="close2" value=" خروج "/>
					</div>
					<div class="col-8 text-center">
						<h3 style="font-weight:bold; color:#EDDFDF;" id="title"> فتح معاملة لعقد رقم <?php echo $contract_id; ?></h3>
				</div>
			</div>
		<?php
		$sql = $db->prepare('SELECT contracts.contract_id,contracts.wanted,contracts.cust_id2,cars.carid,customers.cust_id ,customers.cust_name,customers.phone , customers.phone2, cars.kind , cars.numbers , cars.model,contracts.cont_status FROM cars,contracts,customers
			WHERE contracts.carid = cars.carid AND contracts.cust_id = customers.cust_id AND contracts.contract_id = ? ');
		$sql->bindParam(1,$contract_id);
		$sql->execute();
		if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
			$cust_id = $row['cust_id'] ;
			$phone = $row['phone'] ; 			$phone2 = $row['phone2'] ; $wanted = $row['wanted'] ;$cust_name = $row['cust_name'] ;
			$whats = substr($phone, 1);
			$whats = "9660".$whats; $whats2="";
		if(!empty($phone2)){	$whats2 = substr($phone2, 1); $whats2 = "9660".$whats2;	}
		 $today = date("Y-m-d");
?>
<div class="form-row align-items-center" style="margin-top:10px;">
			<div class="col-1">
				<a href="cust_update.php?id=<?php echo $cust_id ; ?>"><button type="button" class="btn btn-outline-success btn-sm">بيانات المستأجر</button></a>
			</div>
	<div class="col-10 text-center">
		<center class="control-label" style="font-size:1.3em; font-weight:bold; color:#EDDFDF;">
			<?php   echo $cust_name." ~<a href='https://api.whatsapp.com/send?phone=$whats' target='_blank'>". $phone."</a>~ <a href='https://api.whatsapp.com/send?phone=$whats2' target='_blank'>". $phone2."</a>~ سيارة ".$row['kind']." ".$row['numbers']." موديل ". $row['model'] ;
			$cust22 = $row['cust_id2'] ;?>
		</center>
	</div>
	<div class="col-1 text-left">
		<a href="../a/photo_print.php?carid=<?php echo $row['carid'] ; ?>"target="_blank"><button type="button"  class="btn btn-outline-success btn-sm ">الرخصة</button></a>
	</div>
</div>
<?php		if($cust22 > 0){
$cust2 = $db->prepare("SELECT cust_id , cust_name , phone , phone2 from customers where cust_id = ?") ;
$cust2->bindParam(1, $cust22);
$cust2->execute();
if($cust2->rowCount()>0){$wow = $cust2->fetch(PDO::FETCH_ASSOC);
	$ph1 = $wow['phone'] ; $ph2  = $wow['phone2'];
	$wh1 = substr($ph1, 1);
	$wh1 = "9660".$wh1; $wh2 = "" ;
if(!empty($ph2)){	$wh2 = substr($ph2, 1); $wh2 = "9660".$wh2; }
echo '<br /><div class="form-row align-items-center">
			<div class="col-1 col-md-1">
				<a href="../a/cust_update.php?id='.$cust22.'" target="_blank"><button type="button" class="btn btn-outline-success btn-sm ">مستأجر إضافي</button></a>
			</div >	<div class="col-11">
				<center class="control-label" style="font-size:1.3em;font-weight:bold; color:#EDDFDF;">';
			echo $wow['cust_name']." ~ <a href='https://api.whatsapp.com/send?phone=$wh1' target='_blank'>". $ph1."</a>~<a href='https://api.whatsapp.com/send?phone=$wh2' target='_blank'>". $ph2."</a>" ;
			echo "</center></div></div></div>";
}}}

	$debt = $db->prepare("SELECT sum(wanted) as debt from contracts where cust_id = ?  LIMIT 1");
	$debt->bindParam(1,$cust_id);
	$debt->execute();
if($debt->rowCount()>0){$monn = $debt->fetch(PDO::FETCH_ASSOC);
			$debt     = $monn["debt"];}
?>
</div>
<p><br/></p>
<style>
.control-label , .table , .h6, h6 {
	color:#EDDFDF;
}
</style>
<div class="container-fluid">
	<div class="row" >
		<div class="col-2">
			<div class="row" style="padding:2px;">
				<div class="col-11"style="float:right;">
					<button type="button" class="btn btn-primary btn-block" id="sanad_in_btn" data-toggle="modal" style="float:left;font-weight:bold;display:none;" data-backdrop="static" data-keyboard="false" data-target="#sanad_in">سند قبض</button>
				</div>
			</div>
	<div class="row" style="padding:2px;">
		<div class="col-11">
			<a id="print3" href="" target="_blank" ><button type="button" class="btn btn-outline-success btn-block">طباعة العقد <span style="margin-right:20px;" class="glyphicon glyphicon-print" aria-hidden="true"></span></button></a>
	</div>
</div>
<div class="row" style="padding:2px;">
	<div class="col-11">
		<a id="acc" href="" target="_blank" ><button type="button" class="btn btn-outline-success btn-block">كشف حساب <span style="margin-right:20px;" class="glyphicon glyphicon-print" aria-hidden="true"></span></button></a>
  </div>
</div>
		<div class="row" style="padding:2px;">
				<div class="col-11" style="float:right;">
					<input style="float:right ;font-weight:bold;" type="button" class="btn btn-outline-danger btn-block" id="close" value=" خروج "/>
				</div>
			</div>
</div>
		<div class="col-10"style="float:right;">
<form id="case_new" method="post" class="form-horizontal" style="float:center;font-weight: bold; font-size: 16px; color: #000;">
		<div id="msg"></div>
		<input type="hidden" id="case_id"  name="case_id"/>
		<input type="hidden" id="contract_id"  name="contract_id" value="<?php echo $contract_id ;?>"/>
		<div class="row"style="padding:5px;">
				<div class="col-1 text-left">
					<label for="case_no" class="control-label">رقم المعاملة</label>
					</div>
					<div class="col-2">
						<input type="number" class="form-control" id="case_no" name="case_no"/>
					</div>
					<div class="col-1 text-left">
						<label for="money_wanted" class="control-label">المبلغ</label>
						</div>
						<div class="col-2">
							<input type="number" class="form-control" id="money_wanted" name="money_wanted" value="<?php echo $wanted ;?>"/>
						</div>
						<div class="col-2"></div>
				<div class="col-2 text-left">
					<label for="start" class="control-label text-center">تاريخ الرفع</label>
					</div>
				<div class="col-2">
					<input type="text" class="form-control" id="start" name="start" value="<?php echo $today; ?>"/>
				</div>
				<script>
					$(function() {
						var calendar = $.calendars.instance('');
						$('#start').calendarsPicker({calendar: calendar});
						});
				</script>
		</div>
		<div class="row"style="padding:5px;">
				<div class="col-1">
					<label for="court_id" class="control-label"style="float:left;">المحكمة</label>
				</div>
				<div class="col-5">
					<select class="form-control" id="court_id" name="court_id" style="padding:3px;">
	          <?php
	          $sql =$db->prepare("SELECT court_id , concat(name,' # ',address) as tname from courts") ;
	          $sql->execute();
	           if($sql->rowCount()>0)
	          {
	             while($row = $sql->fetch()){
	                    $court_id   = $row["court_id"];
	                    $tname  		= $row["tname"];
	             echo "<option value= $court_id >$tname</option>";
	                  }} ?>
	          </select>
				</div>
				<div class="col-1">
					<label for="floor" class="control-label"style="float:left;">الدور</label>
				</div>
				<div class="col-1">
					<input type="number" class="form-control" id="floor" name="floor"value="2"/>
				</div>
				<div class="col-1">
					<label for="zone" class="control-label"style="float:left;">الدائرة</label>
				</div>
				<div class="col-3">
					<input type="text" class="form-control" id="zone" name="zone" value="جدة"/>
				</div>
		</div>
		<div class="row"style="padding:5px;">
				<div class="col-1">
					<label for="start_comm" class="control-label"style="float:left;">ملاحظات</label>
				</div>
				<div class="col-11">
					<textarea class="form-control" rows="8" id="start_comm" name="start_comm" placeholder="عند بدأ المعاملة"></textarea>
				</div>
		</div>
		<br />
	<div class="form-row">
					<div class="col-2">
						<button id="save_case" class="btn btn-outline-success btn-block"> حفظ معاملة المحكمة</button>
					</div>
		</div>

	</form>

	</div>
</div><!-- end of main row 2 and 10  -->
<div class="form-row">
	<div class="col">
		<h6 >جلسات المعاملة</h6>
				<table class="table table-bordered table-hover">
					<thead>
						 <tr>
							<th class="text-center" width="6%">م</th>
							<th class="text-center" width="12%">تاريخ</th>
							<th class="text-center" width="60%">ملاحظات</th>
							<th class="text-center" width="15%">المعقب</th>
							<th class="text-center" width="8%">صورة</th>
						 </tr>
					 </thead>
			<tbody id="tstandby">
<?php
$output = '';
$sql =$db->prepare("SELECT * FROM case_sessions	WHERE case_id = ? order by session_id DESC ");
					$sql->bindParam(1,$session_id);
$sql->execute();
		if($sql->rowCount()>0)
			{ $ss = 1 ;
				while($row = $sql->fetch())
						{
					$output .= '<tr>
									<td>' . $ss . '</td>
									<td>' . $row['start'] . '</td>
									<td>' . $row['comm'] . '</td>
									<td>' . $row['photo'] . '</td>
									<td>' . $row['father'] . '</td>
								</tr>'; $ss = $ss + 1 ;
					} }
			else	 {$output .= '<tr><td colspan="5">لا يوجد سجلات ...</td></tr>'; }
		echo $output;
			 ?>
						 </tbody>
					 <tfoot></tfoot>
				 </table>
</div>
</div> <!-- end of sessions table row -->
</div><!-- end of fluid2  -->
	<script>
	$(document).ready(function(){
		  $('#close').click(function(){close();});
			 $('#close2').click(function(){close();});

			 $("#save_case").click(function(event){
			   event.preventDefault();
			   $.ajax({
			     url     : "case_new.php",
			     method  : "POST",
			     data    :$("#case_new").serialize(),
			     success : function(data){
			       if($.isNumeric(data))
			         {  $("#case_id").val(data);
			            // $("#invoice_id").val(data);
			            // $('#item_line_add').css('display','block');
			            // $('#update_material_in').attr("type","button");
			            // $('#add_material_in').attr("type","hidden");
			             }
			       else{ $("#msg").html(data);
			     }

			   },
			     error   :function(result){
			       alert(result.statusText);
			       }
			   })
			 }); //end of add matrial invoice ajax code
	 });// end of script
	</script>
  </body>
</html>
