<?php include '../header.php'; ?>
<?php include '../sidemenu.php'; ?>

<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>Assign - 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;">Sale From&nbsp;<input id="chkDateFrom" type="checkbox" checked >:</label>
											<div class="col-md-3">  
												<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>
											<label class="col-md-2 col-form-label" style="text-align: right;"> Sale To:</label>
											<div class="col-md-3">  
												<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;">City:</label>
											<div class="col-md-3">
												<div class="input-group input-group">
													<select type="text" class="form-control" id="cboCity"></select>
												</div>
											</div>
											<label class="col-md-2 col-form-label" style="text-align: right;">Type:</label>
											<div class="col-md-3">
												<div class="input-group input-group">
													<select type="text" class="form-control" id="cboSR">
														<option value=''>All</option>
														<option value='Sales'>Sales</option>
														<option value='Renew'>Renew</option>
													</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;">Start From&nbsp;<input id="chkDateFrom1" type="checkbox"  >:</label>
											<div class="col-md-3">  
												<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="txtDatePicker2" value="2020-02-04" aria-label="Date Picker" placeholder="Select a date">
												</div>
											</div>
											<label class="col-md-2 col-form-label" style="text-align: right;">Start To:</label>
											<div class="col-md-3">  
												<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="txtDatePicker3" 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" style='text-align:right;'>
											</div>
											<label class="col-md-2 col-form-label" style="text-align: right;">Assign:</label>
											<div class="col-md-3">
												<div class="input-group input-group">
													<select type="text" class="form-control" id="cboAssign">
														<option value=''>All</option>
														<option value='Assigned'>Assigned</option>
														<option value='Not Assigned'>Not Assigned</option>
													</select>
												</div>
											</div>
										</div>
										<!-- <div class="form-group row">
											<label class="col-md-4 col-form-label" style="text-align: right;">Date Interval:</label>
											<div class="col-md-8">
												<input type="number" class="form-control" id="txtDateInterval" value=0 style='text-align:right;'>
											</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>
					<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;min-width:100px;">Sales Date</th>
								<th style="text-align: center;min-width:130px;">Invoice No.</th>
								<th style="text-align: center;min-width:100px;">ID No.</th>
								<th style="text-align: center;min-width:200px;">Customer Name</th>
								<th style="text-align: center;min-width:100px;">Plan_Months</th>
								<!-- <th style="text-align: center;min-width:100px;">Price</th>
								<th style="text-align: center;min-width:100px;">Fee</th>
								<th style="text-align: center;min-width:100px;">Tax Charges</th> -->
								<!-- <th style="text-align: center;min-width:100px;">Normal Dis</th>
								<th style="text-align: center;min-width:100px;">Member Dis</th>
								<th style="text-align: center;min-width:100px;">Promotion Dis</th>
								<th style="text-align: center;min-width:100px;">Refund</th>
								<th style="text-align: center;min-width:100px;">Downgrade Charges</th> -->
								<!-- <th style="text-align: center;min-width:100px;">Total Amount</th> -->
								<!-- <th style="text-align: center;min-width:100px;">Payment</th>
								<th style="text-align: center;min-width:100px;">Balance</th> -->
								<th style="text-align: center; width: 10%;">Extra Days</th>
								<th style="text-align: center; width: 10%;">Start Date</th>
                                <th style="text-align: center; width: 10%;">End Date</th>
							</tr>
						</thead>
						<tbody>
						</tbody>
					</table>
				</div>
			</div>	

			<div class="modal fade" id="myModalUMM">
					<div class="modal-dialog" style="max-width: 100% !important;">
        				<div class="modal-content installID" style="width: 30%; top: 40px; margin-left: 34%;">

							<section class="content">
								<div class="container-fluid">
									<div class="row">
										<div class="col-md-12">
											<div class="card card-outline card-primary">
												<div class="card-header">
													<h3 class="card-title">ID No. - <span id="id_no" style="font-weight:bold;"> </span></h3>
												</div>
												<form role="form">
													<div class="card-body">
														<div class="row">
														<div class="form-group row">
															<label class="col-md-4 col-form-label" style="text-align: right;">Start Date:</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="txtDatePicker4" value="2020-02-04" aria-label="Date Picker" placeholder="Select a date">
																</div>
															</div>
														</div>
															<div class="col-md-12">
																<div class="form-group row" style="margin-bottom: 0px;">
																	<div class="col-md-6"></div>
																	<div class="col-md-4">
																		<button type="button" class="btn btn-primary btn-block" onclick="updateStartDate()">Update</button>
																	</div>
																</div>
															</div>
														</div>
													</div>
												</form>
											</div>
										</div>
										
									</div>
								</div>
							</section>	
						</div>
					</div>
				</div>

			

		</div>
	</section>
</div>
<?php include '../footer.php'; ?>
<script>

	var C = '<?php echo isset($_SESSION['city'])?$_SESSION['city']:"" ?>';
	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, #txtDatePicker2, #txtDatePicker3, #txtDatePicker4').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 + "'>" + C + "</option>");	
		}

	});

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

	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 start_df =($("#chkDateFrom1").prop("checked"))?$("#txtDatePicker2").val():"";
		var start_dt =($("#chkDateFrom1").prop("checked"))?$("#txtDatePicker3").val():"";
		var id_no = $("#txtIDNo").val();
		// var date_interval = $("#txtDateInterval").val();
		var city = $("#cboCity").val();
		var S_R = $("#cboSR").val();
		var assign = $("#cboAssign").val();
		$("#myTable").find("tbody").find("tr").remove();
		$.ajax({
			type: "POST",
			url: APP_URL + "api/sales/search_interval.php",
			data: JSON.stringify({ df: df, dt: dt, start_df: start_df, start_dt: start_dt, id_no: id_no, city: city, S_R: S_R, assign: assign }),
			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.sales_date + "</td>")
                    .append("<td>" + v.invoice_no + "</td>")
					.append("<td>" + v.id_no + "</td>")
					.append("<td>" + v.name + "</td>")
					.append("<td>" + v.package + "</td>")
					// .append("<td style='text-align:right'>" + v.price + "</td>")
					// .append("<td style='text-align:right'>" + v.fee + "</td>")
					// .append("<td style='text-align:right'>" + v.tax_charges + "</td>")
					// .append("<td style='text-align:right'>" + v.total_amount + "</td>")
					// .append("<td style='text-align:right'>" + v.refund + "</td>")
					// .append("<td style='text-align:right'>" + v.payment + "</td>")
					// .append("<td style='text-align:right'>" + v.balance + "</td>")
					.append("<td style='text-align:right'>" + v.extra_days + "</td>")
                    .append("<td style='cursor:pointer;background-color:#beeae0;' onclick='umm(\"" + v.id + "\",\"" + v.id_no + "\",\"" + v.start_date + "\",this)'>" + v.start_date + "</td>")
                    .append("<td >" + v.end_date + "</td>")
				);
			});	

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

       });
	}	
	
// function edit(city, date, id_no, cid, inv, obj) {
//     var url = APP_URL + "entry/extra_days.php?city=" + city + "&date=" + date + "&id_no=" + id_no + "&cid=" + cid + "&inv=" + inv;
//     window.open(url, "_blank");
// }

	function umm(id,id_no,sd,obj){
		$("#myModalUMM").modal('show');
		$("#id_no").text(id_no);
		$("#txtDatePicker4").val(sd);	
		ID_ = id;

		$('#datePicker4').attr("data-date",sd);
		$("#txtDatePicker4").val(sd);
		$('#datePicker4').datepicker();
		// port_ = $(obj).parent().find("td").eq(5);
		// ont_id_ = $(obj).parent().find("td").eq(6);
		
	}

	function updateStartDate(){	
		$("#loading").css("display","block");
		var start_date = $("#txtDatePicker4").val();	
		var id = ID_;	

		if(id=="" || start_date==""){
			bootbox.alert("Please fill the box.");
			$("#loading").css("display","none");
			return false;
		}else{
			$.ajax({
				url: APP_URL + "api/sales/update_start_date.php",
				type: "POST",
				data: JSON.stringify({ id: id, start_date: start_date }),
			}).done(function(data){
				$("#loading").css("display","none");
				if(data.message=="updated"){
					search();
					bootbox.alert("Successfully Updated.");
					// $("#myCategory")[0].reset();
					$("#myModalUMM").modal('hide');
				}else{
					bootbox.alert("Error on server side.");
				}
			});
		}		
	}


	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>	
