<?php $__env->startSection('css'); ?>
<style>
	.modal-8 {
		width: 50%;
	}
	.select2-dropdown{
		z-index: 999999999;
	}
</style>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('content'); ?>
<?php echo $__env->make('administrator.client-maintenance-house.form-pay', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
<?php Fun::lang() ?>
<div style="display:none;" class="alert-message alert-success" role="alert" id="done_payment">
	<button type="button" class="close" data-dismiss="alert">×</button>
	<h4 class="alert-heading" style="color:green">Successfully!</h4>
	<hr>
	<p class="mb-0">Data has been saved successfully!</p>
</div>
<div class="title-page">
    <div class="page-bar">
        <ul class="page-breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Dashboard</a></li>
            <li><a href="#">Operation</a></li>
            <li><a href="#">Manage Operation</a></li>
            <li><a href="#">Client Maintenance House</a></li>
			<li><a href="#">Show</a></li>
			<li><a href="#">Client Maintenance House Detail List</a></li>
        </ul>
    </div>
</div>
<div class="page">
	<div class="row">
		<div class="col-sm-12 col-xs-12">
			<div class="title-add">
				<div class="row">
					<div class="col-sm-12 col-xs-12">
						<div class="col-sm-3 col-xs-3">
							<center>
								<?php if(!empty($data[0]->client_img) && $data[0]->client_img != 'none'): ?>
								<img style="border-radius: 50%!important;" width="160px" height="160px" src='<?php echo e(asset("assets/images/resize/" . $data[0]->client_img)); ?>'>
								<?php endif; ?>
							</center>
						</div>
						<div class="col-sm-4 col-xs-4"><br>
							<p><strong>Client Name : </strong> <span><?php echo e($data[0]->client_fullname); ?></span></p><br>
							<p><strong>Gender : </strong> <span><?php echo e(($data[0]->client_gender == 1)?'Male':'Female'); ?></span></p><br>
							<p><strong>Date Of Birth: </strong> <span><?php echo e($data[0]->client_dob); ?></span></p>

						</div>
						<div class="col-sm-4 col-xs-4"><br>
							<p><strong>Contact : </strong> <span><?php echo e($data[0]->client_phone1); ?></span></p><br>
							<p><strong>ID Card : </strong> <span><?php echo e($data[0]->client_cardid); ?></span></p>

						</div>
					</div>

				</div>
			</div>
		</div>
		<div class="page-wrapper" style="margin-top: 10px;">
			<div class="row">
				<div class="col-sm-12 col-xs-12">
					<div class="box-body table-responsive">
						<div class="pull-right">
							<h2><a href="javascript:void(0)" onclick="window.history.go(-1)"><span class="fa fa-reply"></span> Back</a></h2>
						</div>
							<table id="datatable_client_purchase" class="table table-striped table-bordered table-hover nowrap">
							<thead>
								<tr>
									<th></th>
									<th>No</th>
									<th>Project</th>
									<th>Category</th>
									<th>Item</th>
									<th>Action</th>
								</tr>
							</thead>
						</table>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<div class="overlay">
	<div id="loading-img"></div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('js'); ?>
<script type="text/javascript" src="<?php echo e(asset('assets/scripts/yajrabox-datatable/handlebars.min.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/controller/client-maintenance-schedule-administrator.js')); ?>"></script>
<script id="details-template" type="text/x-handlebars-template">
	
	    <div style="border-top:2px solid #000;"></div>
	    <table class="table details-table" id="purchases-{{id}}">
	        <thead>
	        <tr>
	        	<th>No</th>
	            <th>Payment Date</th>
	            <th>Amount</th>
	            <th>Amount Pay</th>
	            <th>Amount Discount</th>
	            <th>Amount Balance</th>
	            <th>Status</th>
	        </tr>
	        </thead>
	    </table>
	    
	</script>

<script type="text/javascript">
	$(document).ready(function() {
		$('.readonly').attr('readonly', true);
	});
	$('#main-spinner').show();
	var template = Handlebars.compile($("#details-template").html());
	var table = $('#datatable_client_purchase').DataTable({
		ordering: false,
		serverSide: false,
		searching: false,
		paging: false,
		ajax: "<?php echo e(route('get_client_maintenance_list')); ?>",
		columns: [{
				"className": 'details-control',
				"orderable": false,
				"searchable": false,
				"data": null,
				"defaultContent": ''
			},
			{
				data: 'DT_RowIndex'
			},
			{
				data: 'project_name'
			},
			{
				data: 'category_name'
			},
			{
				data: 'item_name'
			},
			{
				data: 'action'
			},
		],
		"initComplete": function(settings, json) {
			$('#main-spinner').hide();
		}
	});

	$(document).ready(function() {
		$('.number-only').keypress(function(e) {
			if (isNaN(this.value + "" + String.fromCharCode(e.charCode))) return false;
		});
	});

	function pay(obj) {
		var id = $(obj).closest("tr").attr("id");
		$('#main-spinner').show();
		$.ajax({
			url: "/realty/payment-maintenance-process/" + id,
			type: "get",
			success: function(data) {
				$('#main-spinner').hide();
				var d = $.parseJSON(data);
				$("#pid").val(d['maintenance_process'].id);
				$("#loan_id").val(d['maintenance_process'].maintenance_process_loanid);
				$("#amount").val(d['maintenance_process'].maintenance_process_owepay);
				$("#pay").val(d['maintenance_process'].maintenance_process_owepay);
				$("#payment").val(d['maintenance_process'].maintenance_process_owepay);
			}
		});
	}
    $("#discount").keyup(function() {
        var discount_amount = $(this).val();
        var amount=$('#amount').val();
        var total=amount-discount_amount;
        $('#payment').val(total);

    });
	function save_payment(obj) {

		var pid = $('#pid').val();
		var amount = $('#amount').val();
		var discount = $('#discount').val();
		var payment = $('#payment').val();
		var pay = $('#pay').val();
		var condition = $('#condition').val();
		var penalty = $('#penalty').val();
		var reference = $('#reference').val();
		var date_pay = $('#date').val();
		var remark = $('#remark').val();
		var loan_id = $('#loan_id').val();
		var bank_list = $('#bank-list').val();
		var option = $('#penalty_option').find(':selected').val();
		var amount_pay = parseFloat(pay);
		var amount_payment = parseFloat(payment);
		var condition_type = parseFloat(condition);
		var fee_charge = parseFloat($('#fee-charge').val());
		if (!amount_pay) {
			$.alert({
				title: 'Error!',
				content: 'Payment amount is empty, Please enter your payment!',
			});
		} else if (bank_list == "") {
			$.alert({
				title: 'Error!',
				content: 'Bank Name No Select, Please Select Bank Name!',
			});
		} else if (condition_type == 1 && amount_payment > amount_pay) {
			$.alert({
				title: 'Error!',
				content: 'Your money must be smaller than payment!',
			});
		} else if (condition_type == 2 && amount_payment <= amount_pay) {
			$.alert({
				title: 'Error!',
				content: 'Money must be bigger than payment!',
			});
		} else if (penalty > 0 && option == "") {
			$.alert({
				title: 'Error!',
				content: 'Please Select Penalty Option!',
			});
		} else if (penalty > 0 && condition_type != 1) {
			$.alert({
				title: 'Error!',
				content: 'Pay Next is not available When Penalty Exists!',
			});
		} else if ((amount_pay >= amount_payment && condition_type == 1) || (amount_pay < amount_payment && condition_type == 2)) {
			$('#main-spinner').show();
			$('#_div_paymentPopup').hide();
			$(obj).attr("disabled", "disabled");
			$('#form-pay').modal('hide');
			$.ajax({
				type: 'POST',
				url: "<?php echo e(route('submit_payment_maintenance')); ?>",
				data: {
					"_token": "<?php echo e(csrf_token()); ?>",
					"pid": pid,
					"amount": amount,
					"discount": discount,
					"payment": payment,
					"pay": pay,
					"penalty_condition": option,
					"condition": condition,
					"penalty": penalty,
					"reference": reference,
					"date_pay": date_pay,
					"remark": remark,
					"bank_list": bank_list,
					"fee_charge": fee_charge,
					"loan_id": loan_id
				},
				success: function(data) {

					$('#main-spinner').hide();
					$('#btn-submit').prop('disabled', false);

					if (data.success === true) {
						$.alert({
							title: 'Success!',
							content: data.msg,
						});
						$('#datatable_client_purchase').DataTable().ajax.reload();
					} else {
						$.alert({
							title: 'Error!',
							content: data.msg,
						});
					}
				},
				error: function(request, status, error) {
					$.alert({
						title: 'Error!',
						content: error,
					});
				}
			});
		} else if (parseFloat(amount_pay) < parseFloat(amount_payment) && condition_type == 2) {
			$("#_msg_paymentPopup").html('Your money is smaller than payment, Please select condition!');
			$("#_div_paymentPopup").attr("style", "display:block");
		} else {
			$("#_msg_paymentPopup").html('Your money is more than payment, Please select condition!');
			$("#_div_paymentPopup").attr("style", "display:block");
		}
	}

	$("#extra_amount").keyup(function() {
		var extra_amount = $(this).val();
		$('#payment').val(extra_amount);

	});
	$("#condition").change(function() {
		document.getElementById('btn-submit').disabled = false;
		$("#_div_paymentPopup").attr("style", "display:none");
	});

	function rejectReschedule(sale_id) {
		var route = "<?php echo e(route('reschedule-client-payment.destroy',':id')); ?>";
		route = route.replace(":id", sale_id);
		$("#form-reject-reschedule").attr("action", route);
		$("#reject-reschedule").modal("show");
	}

	function save_deposit(obj) {
		$(obj).attr("disabled", "disabled");
		var sale_id = $('#sale_id').val();
		var date = $('#date').val();
		var date_contract = $('#date_contract').val();
		var deposit_amount = $('#deposit_amount').val();
		var payamount = $('#payamount').val();
		if (!payamount) {
			$("#_msg_depositPopup").html('Deposit amount is empty, Please enter your deposit amount!');
			$("#_div_depositPopup").attr("style", "display:block");
		} else {
			$('#main-spinner').show();
			$.ajax({
				type: 'POST',
				url: "<?php echo e(route('submit_deposit_on_downpayment')); ?>",
				data: {
					"_token": "<?php echo e(csrf_token()); ?>",
					"sale_id": sale_id,
					"date": date,
					"date_contract": date_contract,
					"deposit_amount": deposit_amount,
					"payamount": payamount

				},
				success: function(data) {
					$('#main-spinner').hide();
					var d = $.parseJSON(data);
					if (d.message == "success") {
						window.location.reload();
					}
				}
			});
		}
	}
	$(document).on('keyup', '#payamount', function() {
		var amount = $(this).val();
		var down = $('#deposit_amount').val();
		if (parseFloat(amount) > parseFloat(down)) {
			$.alert({
				title: 'Over Amount',
				content: 'Payment is greater than down payment',
				type: 'blue',
				animation: 'scale',
				draggable: true,
			});
			$('.btn-save-deposite').prop('disabled', true);
		} else {
			$('.btn-save-deposite').prop('disabled', false);
		}
	});
	$(document).on('click', '.approve_deposite', function() {
		var id = $(this).data('id');
		var route = "<?php echo e(route('downpayment.approve-deposite',':id')); ?>";
		route = route.replace(':id', id);
		$('#form-approve-deposite').attr('action', route);
		$('#approve_deposite').modal('show');
	});
	$(document).on('click', '.reject_deposite', function() {
		var id = $(this).data('id');
		var route = "<?php echo e(route('downpayment.reject-deposite',':id')); ?>";
		route = route.replace(':id', id);
		$('#form-reject-deposite').attr('action', route);
		$('#reject_deposite').modal('show');
	});
	$(document).on('click', '.btn-add-commission', function() {
		var id = $(this).data('id');
		$('#commission_sale_id').val(id);
		var route = "<?php echo e(route('get_commission_amount',':id')); ?>";
		route = route.replace(':id', id);
		$('#main-spinner').show();
		$.ajax({
			type: 'GET',
			url: route,
			dastaType: 'json',
			data: {
				"_token": "<?php echo e(csrf_token()); ?>",
				"id": id,
			},
			success: function(data) {
				$('#main-spinner').hide();
				obj = JSON.parse(data);
				$('#commission_amount').val(obj["total_comm"]);
				$('#agency_id').val(obj["agency_id"]);
				$('#agency_name').val(obj["agency_name"]);
			}
		});
		$('#add_commission').modal('show');
	});
	$(document).on('change', '#agent_type', function() {
		var id = $(this).find(':selected').val();
		$('#commission_option').empty();
		$('#main-spinner').show();
		if (id != 1 && id != "") {
			$.ajax({
				type: 'GET',
				url: "<?php echo e(route('get_commission_network')); ?>",
				dastaType: 'text',
				data: {
					"_token": "<?php echo e(csrf_token()); ?>",
					"id": id,
				},
				success: function(data) {
					$('#main-spinner').hide();
					$('#commission_option').append(data);
				}
			});
		}
	});
	$(document).on('click', '.btn-approve-commission', function() {
		var id = $(this).data('id');
		var route = "<?php echo e(route('approve-request-commission.agent')); ?>";
		$('#commission_approve').val(id);
		$('#form-approve-commission').attr('action', route);
		$('#approve_commission').modal('show');
	});
	$(document).on('click', '.btn-reject-commission', function() {
		var id = $(this).data('id');
		var route = "<?php echo e(route('reject-request-commission.agent')); ?>";
		$('#commission_reject').val(id);
		$('#form-reject-commission').attr('action', route);
		$('#reject_commission').modal('show');
	});
	$(document).on('click', '.btn_cancel_payment', function() {
		var id = $(this).data('id');
		var message = "Your Request Will be processed Soon";
		var route = "<?php echo e(route('request-cancel-payment',':id')); ?>";
		route = route.replace(':id', id);
		$('#form_cancel_payment').attr('action', route);
		$('#message').text(message);
		$('#cancel_payment').modal('show');
	});
	$(document).on('click', '.btn_reject_cancel', function() {
		var id = $(this).data('id');
		var route = "<?php echo e(route('reject-cancel-payment',':id')); ?>";
		var message = "This Request will be rejected";
		route = route.replace(':id', id);
		$('#message').text(message);
		$('#form_cancel_payment').attr('action', route);
		$('#cancel_payment').modal('show');
	});
	$(document).on('click', '.btn_approve_cancel', function() {
		var id = $(this).data('id');
		var message = "This Request will be Approve";
		var route = "<?php echo e(route('approve-cancel-payment',':id')); ?>";
		route = route.replace(':id', id);
		$('#message').text(message);
		$('#form_cancel_payment').attr('action', route);
		$('#cancel_payment').modal('show');
	});
	$(document).on('click', '.btn-outline-danger', function() {
		var id = $(this).data('id');
		var route = "<?php echo e(route('delete-client-payment.loan-process',':id')); ?>";
		route = route.replace(':id', id);
		$('#form_delete_payment').attr('action', route);
		$('#delete_payment').modal('show');
	});
	$(document).on('change', '#bank-list', function() {
		var id = $(this).find(':selected').val();
		var fee = 0;
		var payment = $('#payment').val();
		if (id == 2) {
			if (payment <= 400) {
				fee = 2.5;
			} else {
				fee = (0.1 / 100) * payment;
			}
			$('#fee-charge').val(fee);
			$('.fee-charge').css('display', 'block');
		} else {
			$('.fee-charge').css('display', 'none');
		}
	});
	$(document).on('click', '.btn-approve-payoff', function() {
		$('#main-spinner').show();
		var id = $(this).data('id');
		var route = "<?php echo e(route('payoff-loan.edit',':id')); ?>";
		route = route.replace(':id', id);
		var bank_id = null;
		$.ajax({
			type: "GET",
			url: route,
			data: {
				_token: "<?php echo e(csrf_token()); ?>"
			},
			success: function(response) {
				$('#main-spinner').hide();
				if (response.success == true) {
					var data = response.data;
					bank_id = data.bank_id;
					$.confirm({
						title: 'Approve Pay-off',
						content: '' +
							'<form action="" class="formName">' +
							'<div class="form-group">' +
							'<label>Date Payment</label>' +
							'<input type="text" placeholder="date pay off" value="'+data.date+'" class="date form-control date_picker"/>' +
							'</div>' +
							'<div class="form-group">' +
							'<label>Pay-off Amount</label>' +
							'<input type="text" placeholder="date pay off" value="'+data.amount+'" class="amount form-control" readonly/>' +
							`</div>`+
							`<div class="form-group">`+
							`<label class="required">Bank Name</label>
								<?php echo Form::select('bank_id', $bank_data, null, ['id' => 'bank_id','required', 'class' => 'form-control select2']); ?>

							</div>`+
							'</form>',
						buttons: {
							cancel: function() {},
							formSubmit: {
								text: 'Submit',
								btnClass: 'btn-blue',
								action: function() {
									var date = this.$content.find('.date').val();
									var amount = this.$content.find('.amount').val();
									var bank_id = this.$content.find('#bank_id').val();
									var urlaction ="<?php echo e(route('payoff-loan.update',':id')); ?>";
									urlaction = urlaction.replace(':id',data.id);
									if (!name || !amount || !bank_id) {
										$.alert('Error Input Data');
										return false;
									}
									$.ajax({
										type:'PATCH',
										url:urlaction,
										dataType:'JSON',
										data:{
											_token:"<?php echo e(csrf_token()); ?>",
											date:date,
											amount:amount,
											bank_id:bank_id
										},
										success:function(response){
											
										}
									});
								}
							},
						},
						onContentReady: function() {
							var jc = this;
							$('#bank_id option[value="'+bank_id+'"]').prop('selected',true);
							$('.select2').select2();
							this.$content.find('form').on('submit', function(e) {
								e.preventDefault();
								jc.$$formSubmit.trigger('click');
							});
						}
					});
				}
			}
		});
	});

	function approvedReschedule(sale_id) {
		$("#approve_sale_id").val(sale_id);
	}
    $(document).on('click', '.btn-change-client', function() {
        var id = $(this).data('id');
        $('.loan_id').val(id);
        $('#change_client').modal('show');
    });
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('adminlte::page', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>