<?php include '../header.php'; ?>
<?php include '../sidemenu.php'; ?>
<?php
	$D_ = "";
	if(isset($_GET['d'])){
		if(!empty($_GET['d'])){
			$D_ = $_GET['d'];
		}
	}
?>
<div class="content-wrapper" style="min-height: 1203.6px;">
	<!-- Content Header (Page header) -->
	<section class="content-header">
		<div class="container-fluid">
			<div class="row mb-2">
				<div class="col-md-6">
					<h3>Credit_Sale - Report</h3>
				</div>
				<div class="col-md-6">
				</div>
			</div>
		</div>
	</section>
	<section class="content">
		<div class="container-fluid">
			<div class="row" id="SS">
				<div class="col-md-12">
					<div class="card card-outline card-primary">
						<div class="card-header">
							<h3 class="card-title">Search</h3>
						</div>
						<div class="overlay white" id="loading" style="display:none;position: absolute;width: 100%;height: 100%;z-index: 1000;">
							<i class="fas fa-3x fa-sync-alt rotate360"  style="margin: 70px 45%;"></i>
						</div>
						<form role="form">
							<div class="card-body">
								<div class="row">
									<div class="col-md-6">
										<div class="form-group row">
											<label class="col-md-4 col-form-label" style="text-align: right;">Date From&nbsp;<input id="chkDateFrom" type="checkbox" checked >:</label>
											<div class="col-md-8">  
												<div class="input-group">
													<div class="input-group-prepend">
														<span class="input-group-text" style="width: 40px;">
															<i class="far fa-calendar-alt"></i>
														</span>
													</div>
													<input type="text" class="form-control" id="txtDatePicker" value="2020-02-04" aria-label="Date Picker" placeholder="Select a date">
												</div>
											</div>
										</div>
										<div class="form-group row">
											<label class="col-md-4 col-form-label" style="text-align: right;">City:</label>
											<div class="col-md-8">
												<div class="input-group input-group">
													<select type="text" class="form-control" id="cboCity"></select>
												</div>
											</div>
										</div>

									</div>
									<div class="col-md-6">
										<div class="form-group row">
											<label class="col-md-4 col-form-label" style="text-align: right;">Date To: </label>
											<div class="col-md-8">  
												<div class="input-group">
													<div class="input-group-prepend">
														<span class="input-group-text" style="width: 40px;">
															<i class="far fa-calendar-alt"></i>
														</span>
													</div>
													<input type="text" class="form-control" id="txtDatePicker1" value="2020-02-04" aria-label="Date Picker" placeholder="Select a date">
												</div>
											</div>
										</div>

										<div class="form-group row">
											<label class="col-md-4 col-form-label" style="text-align: right;">ID No.:</label>
											<div class="col-md-3">
												<input type="text" class="form-control" id="txtIDNo">
											</div>
											<label class="col-md-2 col-form-label" style="text-align: right;">Payment:</label>
											<div class="col-md-3">
												<select type="text" class="form-control" id="cboBalance">
													<option value="y">Credit</option>
													<option value="n">Cash</option>
												</select>
											</div>	
										</div>

										<div class="form-group row">
											<div class="col-md-6"></div>
											<div class="col-md-3" style="padding-bottom: 2px;">
												<button type="button" class="btn btn-primary btn-block" onclick="search()">Search</button>
											</div>
											<div class="col-md-3">
												<button type="button" id="btnExport" value=" Export Table data into Excel " datafrom="myTable" class="btn btn-success btn-block exportToExcel">Export Data</button> 
											</div>
										</div>
									</div>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="card card-outline card-primary">
				<div class="card-header">
					<h3 class="card-title">List<span id="total_records" style="font-weight:bold;"> </span></h3>
					<!-- <h3 class="card-title" style='padding-left:1000px;'> <span id="tb" style="font-weight:bold;"> </span></h3> -->
					<!-- <div class="card-tools">
						<button type="button" class="btn btn-tool" data-card-widget="collapse"><i class="fas fa-minus"></i></button>
						<button type="button" class="btn btn-tool" data-card-widget="remove"><i class="fas fa-remove"></i></button>
					</div> -->
				</div>
				<div class="card-body p-0 table-responsive ">
					<table class="table table-striped table-head-fixed table-bordered" id="myTable" style="max-height: 500px;">
						<thead>                  
							<tr>
								<th style="width: 3%;">#</th>
								<th style="text-align: center; width: 12%;">Payment Date</th>
								<th style="text-align: center; width: 9%;">Invoice No.</th>
								<th style="text-align: center; width: 10%;">ID No.</th>
								<th style="text-align: center;">Customer Name</th>
                                <th style="text-align: center; width: 10%;">Phone</th>
								<th style="text-align: center;">address</th>
								<th style="text-align: center; width: 7%;">Package</th>
								<th style="text-align: center; width: 9%;">Total Amount</th>
								<th style="text-align: center; width: 7%;">Payment</th>
								<th style="text-align: center; width: 7%;">balance</th>
								<!-- <th style="text-align: center; width: 5%;">Action</th> -->
							</tr>
						</thead>
						<tbody>
						</tbody>
					</table>
				</div>
			</div>	

		</div>
	</section>
</div>
<?php include '../footer.php'; ?>
<script>
	var C = '<?php echo isset($_SESSION['city'])?$_SESSION['city']:"" ?>';
	var code = '<?php echo isset($_SESSION['code'])?$_SESSION['code']:"" ?>';
	var d = new Date();
	var mm = (d.getMonth("MM")+1);
	var dd = d.getDate();
	var customDate = d.getFullYear() + "-" + ((mm<10)?"0" + mm:mm) + "-" +  ((dd<10)?"0" + dd:dd);

	$(function(){

		$("body").addClass("sidebar-collapse");	
		$('#txtDatePicker, #txtDatePicker1').datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: 'yy-mm-dd',
			showButtonPanel: true,
            showAnim: 'clip',
            autoclose: true
        }).val(customDate);

		if(C=='Main Office'){
			getAllCity();
		}else{
			$("#cboCity").append("<option value= '" + C + "'  data-code= '" + code + "' >" + C + "</option>");	
			// getAll();
			getPackage();
		}

	});

	$(".exportToExcel").click(function(){
		exportExcel("#myTable");
	});

	$("#chkDateFrom").change(function(){
		if($("#chkDateFrom").prop("checked")){
			$("#txtDatePicker").attr("disabled",false);
			$("#txtDatePicker1").attr("disabled",false);
		}else{
			$("#txtDatePicker").attr("disabled",true);
			$("#txtDatePicker1").attr("disabled",true);
		}
	});

	function getAllCity(){
		$("#cboCity").find("option").remove();
		$("#cboCity").append("<option value= '' data-id = ''></option>");
		$.ajax({
			url: APP_URL + "api/city/get_all_rows.php"
		}).done(function(data) {
			$.each(data.records, function(i, v) {
				if(v.name!='Main Office'){
					$("#cboCity").append("<option value= '" + v.name + "' data-code= '" + v.code + "' >" + v.name + "</option>");	
				}
			});		
		});
	}

	

	function search(){
		$("#loading").css("display","block");
		var df =($("#chkDateFrom").prop("checked"))?$("#txtDatePicker").val():"";
		var dt =($("#chkDateFrom").prop("checked"))?$("#txtDatePicker1").val():"";
		var id_no = $("#txtIDNo").val();
		var city = $("#cboCity").val();
		var bal = $("#cboBalance").val();

		$("#myTable").find("tbody").find("tr").remove();
		$.ajax({
			type: "POST",
			url: APP_URL + "api/sales/search_payment.php",
			data: JSON.stringify({ df: df,dt: dt,id_no: id_no, city:city,bal:bal }),
			error: function(data) {
				$("#loading").css("display","none");
			}
		}).done(function(data) {	
			if(data.records.length>1){
				$("#total_records").text(" - " + data.records.length + " records found.");
			}else{
				$("#total_records").text(" - " + data.records.length + " record found.");
			}
			$("#loading").css("display","none");
			$.each(data.records, function(i, v) {
				$("#myTable").find("tbody")
				.append($('<tr>')
					.append("<td>" + (i+1) + "</td>")
                    .append("<td>" + v.payment_date + "</td>")
                    .append("<td>" + v.invoice_no + "</td>")
					.append("<td>" + v.id_no + "</td>")
					.append("<td>" + v.name + "</td>")
                    .append("<td>" + v.phone + "</td>")
					.append("<td>" + v.address + "</td>")
					.append("<td>" + v.package + "</td>")
					.append("<td style='text-align:right'>" + v.total + "</td>")
					.append("<td style='text-align:right'>" + v.payment + "</td>")
					.append("<td style='text-align:right'>" + v.balance + "</td>")
					// .append("<td style='text-align:center;'><span class='btn btn-info btn-sm' style='cursor:pointer; font-size: 12px; width: 50px;' onclick='payment(\"" + v.invoice_no + "\")'><i class='fas fa-dollar-sign' title='Payment'></i></span></td>")
				);
			});	

			$("#myTable").find("tbody")
			.append($('<tr style="font-weight:bold;">')
			.append("<td colspan='8' style='text-align:right;'>Total:</td>")
			.append("<td style='text-align:right;'>" + data.total_amountF + "</td>")
			.append("<td style='text-align:right;'>" + data.paymentF + "</td>")
			.append("<td style='text-align:right;'>" + data.balanceF + "</td>")
  			);  
			  // $("#tb").text("Total - " + data.total_amountF );
       });
	}	

	

	

	function goToDetail(id){
		document.location = APP_URL + "entry/sales.php?id=" + id;
	}	

	function isNumber(evt) {
		evt = (evt) ? evt : window.event;
		var charCode = (evt.which) ? evt.which : evt.keyCode;
		if ( (charCode > 31 && charCode < 48) || charCode > 57) {
			return false;
		}
		return true;
	}
	
</script>	
