<?php include "headerLink.php"?>
<!DOCTYPE html>
<html lang="en">


<body class="animsition">
    <div class="page-wrapper">
        <!-- HEADER MOBILE-->
        <?php include "menuSideBar_mobile.php" ;?>
        <!-- END HEADER MOBILE-->
		
        <!-- MENU SIDEBAR-->
        <?php include "menuSideBar.php" ;?>
        <!-- END MENU SIDEBAR-->

        <!-- PAGE CONTAINER-->
        <div class="page-container">
            <!-- HEADER DESKTOP-->
			<?php include "headerPage.php"?>
            <!-- END HEADER DESKTOP-->
			
			
			
			<!-- MAIN CONTENT-->
			<?php
			include("tenant_payment_dashboard.php");
			$widthTableRental = "50%";
			$widthTableWater = "50%";
			$widthTableOthers = "0%";
			if($countRowBillOutstandingOthers > 0){
				$widthTableRental = "00%";
				$widthTableWater = "0%";
				$widthTableOthers = "0%";
			}
			?>
			<!--main content upper start-->
			<?php include "main-content-upper.php"?>
            <!--main content upper end-->
			<!--main content middle start-->
						 
						
			<div class="row" style="font:small-caption">
					<div class="card-body">
						<div class="row">
							<div class="col col-md-6">
								<div class="card">
									<div class="card-header">
										<h5 class="m-0 font-weight-bold text-primary">
											Personal Information  
											<button type="button" class="btn btn-sm btn-primary" style="float:right">Edit</button>
										</h5>
									</div>
									<div class="card-body card-block">
										<div class="row">
											<div class="col col-md-3">
												<label class=" form-control-label">Name</label>
											</div>
											<div class="col col-md-9">
												<label class=" form-control-label"><?php echo strtoupper($tenant_name)?></label>
											</div>
										</div>
										<div class="row">
											<div class="col col-md-3">
												<label class=" form-control-label">Unit No</label>
											</div>
											<div class="col col-md-9">
												<label class=" form-control-label"><?php echo $unit_no?> , <?php echo $location?></label>
											</div>
										</div>
										<div class="row">
											<div class="col col-md-3">
												<label class=" form-control-label">Project</label>
											</div>
											<div class="col col-md-9">
												<label class=" form-control-label"><?php echo $project_name?></label>
											</div>
										</div>
										<div class="row">
											<div class="col col-md-3">
												<label class=" form-control-label">Rental Status</label>
											</div>
											<div class="col col-md-9">
												<label class=" form-control-label"><?php echo ucfirst($tenant_status_show)?></label>
											</div>
										</div>
										<div class="row">
											<div class="col col-md-3">
												<label class=" form-control-label">IC NO.</label>
											</div>
											<div class="col col-md-9">
												<label class=" form-control-label"><?php echo $tenant_ic?></label>
											</div>
										</div>									
									</div>	
								</div>												
							</div>
							<div class="col col-md-6" >
								<div class="card">
									<div class="card-header ">
										<h5 class="m-0 font-weight-bold text-primary">
											Billing Summary
											<button type="button" class="btn btn-outline-success btn-sm" style="float:right" onclick="viewTenantDashboard('<?php echo $tenant_id;?>')" >Show Profile & Payment History</button>
										</h5>
									</div>
									<div class="card-body card-block">
													<div class="row" valign="middle">
														<div class="col col-md-6">
															<label class=" form-control-label">Deposit</label>
															<label class=" badge badge-secondary"><?php echo $depositStatus?></label>
														</div>
														<div class="col col-md-6" >
														<?php if($deposit_button == "Re-print" || $deposit_button == ""){?>
															<a href="" onclick="reprint('<?php echo $tenant_id;?>')" style="float:right" class="href"><?php echo $totalDepositShow ?></a>
														<?php } 
															else if($deposit_button == "Pay Now"){?>
															 <button type="button" style="float:right" class="btn btn-outline-danger btn-sm" onclick="Javascript:newPopup('deposit_Paynow.php?tenant_id=<?php echo $tenant_id;?>','&deposit_bill_id=<?php echo $deposit_bill_id; ?>')">
																<?php echo $deposit_button ?></a>
															</button> 
														<?php } else if($deposit_button == "Create Bill"){?>
															<button type="button" style="float:right" class="btn btn-outline-danger btn-sm" onclick="Javascript:newPopup('deposit_CreateBill.php?tenant_id=<?php echo $tenant_id;?>')">
																<?php echo $deposit_button ?>
															</button> 
															
														<?php } else if($deposit_button == "Update Bill"){?>
															<button type="button" style="float:right" class="btn btn-outline-danger btn-sm" onclick="Javascript:newPopup('deposit_UpdateBill.php?tenant_id=<?php echo $tenant_id;?>')">
																<?php echo $deposit_button ?>
															</button> 
															<button type="button" style="float:right" class="btn btn-outline-danger btn-sm" onclick="Javascript:newPopup('deposit_Paynow.php?tenant_id=<?php echo $tenant_id;?>','&deposit_bill_id=<?php echo $deposit_bill_id; ?>')">
																<?php echo $deposit_button_payNow ?></a>
															</button> 
															
														<?php } ?>
															<br>
														</div>
													</div>
													<div class="row">
														<div class="col col-md-6">
															<label class=" form-control-label">Current Rental Bill <?php echo date("M Y");?></label>
															<label class=" badge badge-secondary"><?php echo ucfirst($currentRentalPaymentStatus)?></label>
														</div>
														<div class="col col-md-6" >
															<label class=" form-control-label" style="float:right"><?php echo $bill_show?></label>
														</div>
													</div>
													<div class="row">
														<div class="col col-md-6">
															<label class=" form-control-label">Total Previous Outstanding Rental</label>
														</div>
														<div class="col col-md-6" >
															<label class=" form-control-label" style="float:right"><?php echo $Outstanding_amount?></label>
														</div>
													</div>
													<div class="row">
														<div class="col col-md-6">
															<label class=" form-control-label">Current Water Bill <?php echo date("M Y");?></label>
														</div>
														<div class="col col-md-6" >
															<label class=" form-control-label" style="float:right">-</label>
														</div>
													</div>
													<?php //if($countRowBillOutstandingOthers > 0){ ?>
													<div class="row">
														<div class="col col-md-6">
															<label class=" form-control-label"> <?php echo $others_outstanding_sentences?></label>
														</div>
														<div class="col col-md-6" >
															<label class=" form-control-label" style="float:right"><?php echo $others_outstanding?></label>
														</div>
													</div>
													<?php //} ?>
												</div>
								</div>	
							</div>
						</div>
						<div class="row">
							<div class="col col-md-12">
								<div class="card">
									<form name="bill_payment_form">
										<div class="card-header ">
											<h5 class="m-0 font-weight-bold text-primary">
												Bill Payment
												<span style="float:right">
													<?php if($countRowBillOutstandingOthers > 0){?>
													<!--<button type="button" class="btn btn-outline-primary btn-sm"   onclick="loadInv_others('<?php echo $tenant_id;?>','<?php echo $bill_type_id_others?>')" >Invoice Listing</button>-->
													<button type="button" style="float:right" class="btn btn-outline-danger btn-sm" onclick="Javascript:newPopup('loadInv_others.php?tenant_id=<?php echo $tenant_id;?>')">Invoice Listing</button>
													<button type="button" class="btn btn-outline-primary btn-sm"   onclick="loadDoc_others('<?php echo $tenant_id;?>','<?php echo $bill_type_id_others?>')" >Preview Payment</button>
													<button type="button" class="btn btn-outline-success btn-sm" id="buttonSaveAndPrint"  onclick="billPayment_saveandprint_others('<?php echo $tenant_id;?>','<?php echo $bill_type_id_others?>')" disabled >Save & Print</button>
													<?php } 
													else if($countRowBillOutstandingOthers == 0){
													?>
													<!--<button type="button" class="btn btn-outline-primary btn-sm"   onclick="loadInv('<?php echo $tenant_id;?>')" >Invoice Listing</button>-->
													<button type="button" style="float:right" class="btn btn-outline-danger btn-sm" onclick="Javascript:newPopup('loadInv_others.php?tenant_id=<?php echo $tenant_id;?>')">Invoice Listing</button>
													<button type="button" class="btn btn-outline-primary btn-sm"   onclick="loadDoc('<?php echo $tenant_id;?>')" >Preview Payment</button>
													<button type="button" class="btn btn-outline-success btn-sm"   id="buttonSaveAndPrint"  onclick="billPayment_saveandprint('<?php echo $tenant_id;?>')" disabled >Save & Print</button>
													<?php } ?>
													<button type="reset" class="btn btn-outline-danger btn-sm" onclick="billPayment_reset('<?php echo $tenant_id;?>')" >Reset</button>
													<!--<input type="reset" class="btn btn-outline-danger btn-sm" value="Reset">-->
												</span>
											</h5>
											<br/>
										</div>
										<div class="card-body" style="background-color:#f0fff0   ">
											
											<table class="table  table-hover table-sm" id="dataTable" width="100%" cellspacing="0" style="font-size: 12px">
												<tr>
													<td colspan="2" width="<?php echo $widthTableRental?>" class="border-right"><i class="fa fa-bars"></i>&nbsp; <b>Rental</b></td>
													<td colspan="2" width="<?php echo $widthTableWater?>" class="border-right"><i class="fa fa-bars"></i>&nbsp; <b>Water</b></td>
													<?php 
														if($countRowBillOutstandingOthers > 0) {
													?>
													<td colspan="2" width="<?php echo $widthTableOthers?>"><i class="fa fa-bars"></i>&nbsp; <b><?php echo $bill_type_name?></b></td>
													<?php } ?>
												</tr>
												<tr>
													<td><b>Monthly Rental Payment (RM)</b></td>
													<td class="border-right"><?php echo $rental_rate?></td>
													<td><b>Month</b></td>
													<td class="border-right"><input class="form-control" type="month" name="monthYear_water" id="monthYear_water"></td>
													<?php 
														if($countRowBillOutstandingOthers > 0) {
													?>
													<td><b>Amount Outstanding</b></td>
													<td class="border-right"><?php echo $others_outstanding ?></td>
													<?php } ?>
												</tr>
												<tr>
													<td><b>Rental Amount Paid (RM)</b></td>
													<td class="border-right"><input class="form-control" type="number" step="0.01" name="rental_amount_paid" id="rental_amount_paid"></td>
													<td><b>Water Amount Paid (RM)</b></td>
													<td class="border-right"><input class="form-control" type="number" step="0.01" name="water_amount_paid" id="water_amount_paid"></td>
													<?php 
														if($countRowBillOutstandingOthers > 0) {
													?>
													<td><b>Amount Paid (RM)</b></td>
													<td >
													<input type="number" step="0.01" name="others_amount_paid" id="others_amount_paid"  class="form-control edit-items" min="0.00" max='<?php echo $maxOutstanding ?>'/></td>
													<?php } ?>
												</tr>
												<tr>
													<td><b>Payment Type</b></td>
													<?php 
														
														
													?>
													<td class="border-right">
														<select name="rental_payment_type" id="rental_payment_type" onchange="paymentTypeForRental()">
															<?php 
															$sqlPaymentType_rental = "SELECT * FROM `payment_type` ";
															$runPaymentType_rental = mysqli_query($link, $sqlPaymentType_rental);
															if($runPaymentType_rental === FALSE){
																die(mysqli_error($link));
															}
																while($resultPaymentType_rental = mysqli_fetch_array($runPaymentType_rental)){
																	$paymentType_id = $resultPaymentType_rental["id"];
																	$payment_type_code = $resultPaymentType_rental["payment_type_code"];
																	$payment_type_desc = $resultPaymentType_rental["payment_type_desc"];
																	
																	?>
																	<option value="<?php echo $paymentType_id ?>"><?php echo $payment_type_desc?></option>
																	<?php
																}
															?>
														</select>
														<div style="display:none" id="refNo_paymentType_rental_view">
															Reference No.: <input class="form-control" type="text"  name="refNo_rental" id="refNo_rental">
														<div>
													</td>
													<td><b>Water Amount Paid (RM)</b></td>
													<td class="border-right">
														<select name="water_payment_type" id="water_payment_type" onchange="paymentTypeForWater()">
															<?php 
															$sqlPaymentType_water = "SELECT * FROM `payment_type` ";
															$runPaymentType_water = mysqli_query($link, $sqlPaymentType_water);
															if($runPaymentType_water === FALSE){
																die(mysqli_error($link));
															}
																while($resultPaymentType_water = mysqli_fetch_array($runPaymentType_water)){
																	$paymentType_id = $resultPaymentType_water["id"];
																	$payment_type_code = $resultPaymentType_water["payment_type_code"];
																	$payment_type_desc = $resultPaymentType_water["payment_type_desc"];
																	
																	?>
																	<option value="<?php echo $paymentType_id ?>"><?php echo $payment_type_desc?></option>
																	<?php
																}
															?>
														</select>
														<div style="display:none" id="refNo_paymentType_water_view">
															Reference No.: <input class="form-control" type="text"  name="refNo_water" id="refNo_water">
														<div>
													
													</td>
													<?php 
														if($countRowBillOutstandingOthers > 0) {
													?>
													<td><b>Amount Paid (RM)</b></td>
													<td >
														<select name="others_payment_type" id="others_payment_type" onchange="paymentTypeForOther()">
															<?php 
															$sqlPaymentType_other = "SELECT * FROM `payment_type` ";
															$runPaymentType_other = mysqli_query($link, $sqlPaymentType_other);
															if($runPaymentType_other === FALSE){
																die(mysqli_error($link));
															}
																while($resultPaymentType_other = mysqli_fetch_array($runPaymentType_other)){
																	$paymentType_id = $resultPaymentType_other["id"];
																	$payment_type_code = $resultPaymentType_other["payment_type_code"];
																	$payment_type_desc = $resultPaymentType_other["payment_type_desc"];
																	
																	?>
																	<option value="<?php echo $paymentType_id ?>"><?php echo $payment_type_desc?></option>
																	<?php
																}
															?>
														</select>
														<div style="display:none" id="refNo_paymentType_other_view">
															Reference No.: <input class="form-control" type="text"  name="refNo_other" id="refNo_other">
														<div>
													
													</td>
													<?php } ?>
												</tr>
												<tr id="demo">
													
												</tr>
											</table>
										</div>
									</form>
								</div>
							</div>	
						</div>	
						</div>
					</div>
				
                        
            <!--main content middle end-->
			<!--main content bottom start-->
				<?PHP include "main-content-bottom.php";?>
			<!--main content bottom end-->
			
            <!-- END MAIN CONTENT-->
            <!-- END PAGE CONTAINER-->
        </div>
    </div>
    <?PHP include "footerLink.php";?>
</body>
</html>
<!-- end document-->

<script>
function billPayment_reset(tenant_id){
	//alert("RESET" + tenant_id);
	 document.getElementById("demo").innerHTML = "";
	document.getElementById('rental_amount_paid').value = "";
	document.getElementById('monthYear_water').value = "";
	document.getElementById('water_amount_paid').value = "";
	
	document.getElementById('others_amount_paid').value = "";
	document.getElementById('rental_payment_type').value = "";
	document.getElementById('water_payment_type').value = "";
	document.getElementById('others_payment_type').value = "";
	document.getElementById('refNo_rental').value = "";
	document.getElementById('refNo_water').value = "";
	document.getElementById('refNo_other').value = "";
	
	var refNo_paymentType_rental_view = document.getElementById('refNo_paymentType_rental_view');
	var refNo_paymentType_water_view = document.getElementById('refNo_paymentType_water_view');
	var refNo_paymentType_other_view = document.getElementById('refNo_paymentType_other_view');
	refNo_paymentType_rental_view.style.display = "none";
	refNo_paymentType_water_view.style.display = "none";
	refNo_paymentType_other_view.style.display = "none";
	
	document.getElementById("buttonSaveAndPrint").setAttribute("disabled");
}



function paymentTypeForRental(){
	var rental_payment_type = document.getElementById('rental_payment_type').value;
	//alert(rental_payment_type);
	var refNo_paymentType_rental_view = document.getElementById('refNo_paymentType_rental_view');
	if (rental_payment_type > 1) { 
		refNo_paymentType_rental_view.style.display = "block";
	  } else {
		refNo_paymentType_rental_view.style.display = "none";
		document.getElementById('refNo_rental').value = "";
	  }
}
function paymentTypeForWater(){
	var water_payment_type = document.getElementById('water_payment_type').value;
	//alert(water_payment_type);
	var refNo_paymentType_water_view = document.getElementById('refNo_paymentType_water_view');
	if (water_payment_type > 1) { 
		refNo_paymentType_water_view.style.display = "block";
	  } else {
		refNo_paymentType_water_view.style.display = "none";
		document.getElementById('refNo_water').value = "";
	  }
}
function paymentTypeForOther(){
	var others_payment_type = document.getElementById('others_payment_type').value;
	//alert(others_payment_type);
	var refNo_paymentType_other_view = document.getElementById('refNo_paymentType_other_view');
	if (others_payment_type > 1) { 
		refNo_paymentType_other_view.style.display = "block";
	  } else {
		refNo_paymentType_other_view.style.display = "none";
		document.getElementById('refNo_other').value = "";
	  }
}
document.getElementsByClassName('edit-items')[0].oninput = function () {
        var max = parseInt(this.max);

        if (parseInt(this.value) > max) {
            this.value = max; 
        }
    }


function loadDoc(tenant_id) {
	//alert();
	var rental_amount_paid = document.getElementById('rental_amount_paid').value;
	var monthYear_water = document.getElementById('monthYear_water').value;
	var water_amount_paid = document.getElementById('water_amount_paid').value;
	var rental_payment_type = document.getElementById('rental_payment_type').value;
	var water_payment_type = document.getElementById('water_payment_type').value;
	var refNo_rental = document.getElementById('refNo_rental').value;
	var refNo_water = document.getElementById('refNo_water').value;
	var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("demo").innerHTML = this.responseText;
			document.getElementById("buttonSaveAndPrint").removeAttribute("disabled");
       }
    };
   xhttp.open("GET", "ajax_info.php?tenant_id="+tenant_id+"&rental_amount_paid="+rental_amount_paid+"&monthYear_water="+monthYear_water+"&water_amount_paid="+water_amount_paid+"&rental_payment_type="+rental_payment_type+"&water_payment_type="+water_payment_type+"&refNo_rental="+refNo_rental+"&refNo_water="+refNo_water, true);
    xhttp.send();
}
function loadDoc_others(tenant_id,bill_type_id_others) {
	//alert(tenant_id);
	//alert(bill_type_id_others);
	var rental_amount_paid = document.getElementById('rental_amount_paid').value;
	var monthYear_water = document.getElementById('monthYear_water').value;
	var water_amount_paid = document.getElementById('water_amount_paid').value;
	var others_amount_paid = document.getElementById('others_amount_paid').value;
	var rental_payment_type = document.getElementById('rental_payment_type').value;
	var water_payment_type = document.getElementById('water_payment_type').value;
	var others_payment_type = document.getElementById('others_payment_type').value;
	var refNo_rental = document.getElementById('refNo_rental').value;
	var refNo_water = document.getElementById('refNo_water').value;
	var refNo_other = document.getElementById('refNo_other').value;
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
		//alert("READY STATE");
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("demo").innerHTML =
            this.responseText;
			document.getElementById("buttonSaveAndPrint").removeAttribute("disabled");
       }
    };
    xhttp.open("GET", "ajax_info.php?tenant_id="+tenant_id+"&rental_amount_paid="+rental_amount_paid+"&monthYear_water="+monthYear_water+"&water_amount_paid="+water_amount_paid+"&others_amount_paid="+others_amount_paid+"&bill_type_id_others="+bill_type_id_others+"&rental_payment_type="+rental_payment_type+"&water_payment_type="+water_payment_type+"&others_payment_type="+others_payment_type+"&refNo_rental="+refNo_rental+"&refNo_water="+refNo_water+"&refNo_other="+refNo_other, true);
    xhttp.send();
}
function billPayment_saveandprint(str) {
  //alert(str);
	var rental_amount_paid = document.getElementById('rental_amount_paid').value;
	var monthYear_water = document.getElementById('monthYear_water').value;
	var water_amount_paid = document.getElementById('water_amount_paid').value;
	var rental_payment_type = document.getElementById('rental_payment_type').value;
	var water_payment_type = document.getElementById('water_payment_type').value;
	var refNo_rental = document.getElementById('refNo_rental').value;
	var refNo_water = document.getElementById('refNo_water').value;
	
	window.open("billPayment_saveandprint.php?tenant_id="+str+"&rental_amount_paid="+rental_amount_paid+"&monthYear_water="+monthYear_water+"&water_amount_paid="+water_amount_paid+"&rental_payment_type="+rental_payment_type+"&water_payment_type="+water_payment_type+"&refNo_rental="+refNo_rental+"&refNo_water="+refNo_water, "_blank");
	
	 window.location("viewTenantDashboard.php?tenant_id="+str+"&menu=Tenant&submenu=Dashboard");
 
}
function billPayment_saveandprint_others(str,bill_type_id_others) {
  //alert();
	var rental_amount_paid = document.getElementById('rental_amount_paid').value;
	var monthYear_water = document.getElementById('monthYear_water').value;
	var water_amount_paid = document.getElementById('water_amount_paid').value;
	var monthYear_others = "";
	var others_amount_paid = document.getElementById('others_amount_paid').value;
	var rental_payment_type = document.getElementById('rental_payment_type').value;
	var water_payment_type = document.getElementById('water_payment_type').value;
	var others_payment_type = document.getElementById('others_payment_type').value;
	var refNo_rental = document.getElementById('refNo_rental').value;
	var refNo_water = document.getElementById('refNo_water').value;
	var refNo_other = document.getElementById('refNo_other').value;
  
	window.open("billPayment_saveandprint.php?tenant_id="+str+"&rental_amount_paid="+rental_amount_paid+"&monthYear_water="+monthYear_water+"&water_amount_paid="+water_amount_paid+"&monthYear_others="+monthYear_others+"&others_amount_paid="+others_amount_paid+"&bill_type_id_others="+bill_type_id_others+"&rental_payment_type="+rental_payment_type+"&water_payment_type="+water_payment_type+"&others_payment_type="+others_payment_type+"&refNo_rental="+refNo_rental+"&refNo_water="+refNo_water+"&refNo_other="+refNo_other, "_blank");
 
}
function newPopup(url) {
	popupWindow = window.open(
	//url,'popUpWindow','height=600,width=700,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
	url,'_blank','left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
}
	//window.close();
    window.onunload = refreshParent;
    function refreshParent() {
        window.opener.location.reload();
    }
</script >
</script>