<?php
include 'header.php';
$bdd=new DB();
$search_query="";
if(isset($_POST['search_customer_submit'])){
	if($_POST['search_customer_name']!=="" && $_POST['search_contact_no']!=""){
		$search_query=" AND (name='".$_POST['search_customer_name']."' OR contact_no='".$_POST['search_contact_no']."')";
	}else if($_POST['search_contact_no']!=="" && $_POST['search_customer_name']==""){
		$search_query=" AND contact_no='".$_POST['search_contact_no']."'";
	}else if($_POST['search_customer_name']!=="" && $_POST['search_contact_no']==""){
		$search_query=" AND name='".$_POST['search_customer_name']."'";
	}
}
?>
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<div class="row">
	<div class="col-sm-12"><button class="btn btn-blue btn-block">KYC Customer</button></div>
	<div class="col-sm-12">&nbsp</div>
	<div class="col-sm-12">
    <div class="outer-container">
		<form action="" method="post" class="form-horizontal">
			<div class="form-group">
				<div class="col-sm-4">
					<input class="form-control" type="text" name="search_customer_name" id="search_customer_name" Placeholder="Customer Name" value="<?php if(isset($_POST['search_customer_name'])){ echo $_POST['search_customer_name'];}?>">
				</div>
				<div class="col-sm-4">
					<input class="form-control" type="text" name="search_contact_no" id="search_contact_no" Placeholder="Contact No." value="<?php if(isset($_POST['search_contact_no'])){ echo $_POST['search_contact_no'];}?>">
				</div>
				<div class="col-sm-2">
					<button type="submit" name="search_customer_submit" class="btn btn-danger">Search</button>
				</div>
				<div class="col-sm-2">
					<a href="kyc-customer" class="btn btn-primary">Reset</a>
				</div>
			</div>
		</form>
		<table class="table table-bordered">
			<thead>
			  <tr>
				<th>Sr.No</th>
				<th>Name</th>
				<th>Address</th>
				<th>Contact No.</th>
				<th>Date</th>
				<th>Added By</th>
				<th>Verification Proof</th>
				<th>Update</th>
			  </tr>
			</thead>
			<tbody>
			<?php
			$cnt=1;
			$rows=$bdd->getAll("SELECT C.customer_id,c.date,U.name AS user_name,C.name,C.address,C.contact_no,CVD.id_proof_type,CVD.id_proof_value,CVD.id_proof_data,CVD.address_proof_type,CVD.address_proof_value,CVD.address_proof_data FROM customer AS C LEFT JOIN customer_verification_data AS CVD ON C.customer_id=CVD.customer_id LEFT JOIN user AS U ON c.added_by=U.user_id WHERE status='KYC' $search_query ORDER BY date DESC");
			foreach($rows as $row){
			?>
				<tr id="table_row_<?php echo $row['customer_id'];?>">
					<td><?php echo $cnt;?></td>
					<td><?php echo $row['name'];?></td>
					<td><?php echo $row['address'];?></td>
					<td><?php echo $row['contact_no'];?></td>
					<td><?php echo date('d-m-Y H:i:s',strtotime($row['date']));?></td>
					<td><?php echo $row['user_name'];?></td>
					<td>
						<table class="table table-bordered">
							<thead>
								<th>ID Proof</th>
								<th>ID No</th>
								<th>Result</th>
							</thead>
							<tbody>
								<tr>
									<td><?php echo $row['id_proof_type'];?></td>
									<td><?php echo $row['id_proof_value'];?></td>
									<td>
										<?php
											$data=json_decode($row['id_proof_data']);
											$response_data="";
											if($row['id_proof_type']=="ElectionCard"){
				$response_data.="<br><b>Epic no : </b> <i>".$data->essentials->epicNumber."</i>";
				$response_data.="<br><b>Name : </b> <i>".$data->result->name."</i>";
				$response_data.="<br><b>Birth Date : </b> <i>".$data->result->dob."</i>";
				$response_data.="<br><b>Gender : </b> <i>".$data->result->gender."</i>";
				$response_data.="<br><b>Area : </b> <i>".$data->result->district."</i>";
				echo $response_data;
											}else if($row['id_proof_type']=="AadharCard"){
				$response_data.="<br><b>Aadhar No : </b> <i>".$data->data->aadhaar_number."</i>";
				$response_data.="<br><b>Name : </b> <i>".$data->data->full_name."</i>";
				$response_data.="<br><b>Birth Date : </b> <i>".date('d-m-Y',strtotime($data->data->dob))."</i>";
				$response_data.="<br><b>Gender : </b> <i>".$data->data->gender."</i>";
				$response_data.="<br><b>Address : </b> <i>".$data->data->address->house.",".$data->data->address->street.", ".$data->data->address->loc.", ".$data->data->address->po.", ".$data->data->address->dist.", ".$data->data->address->state.",".$data->data->address->country."</i>";
				$response_data.="<br><b>Photo : </b> <img src='data:image/png;base64, ".$data->data->profile_image."'>";
				echo $response_data;
											}else if($row['id_proof_type']=="PanCard"){
				$response_data.="<br><b>PAN No : </b> <i>".$data->essentials->number."</i>";
				$response_data.="<br><b>Name : </b> <i>".$data->result->name."</i>";
				$response_data.="<br><b>Status : </b> <i>".$data->result->panStatus."</i>";
				echo $response_data;
											}
											
										?>
									</td>
									
								</tr>
							</tbody>
						</table>
						<table class="table table-bordered">
							<thead>
								<th>Address Proof</th>
								<th>ID No</th>
								<th>Result</th>
							</thead>
							<tbody>
								<tr>
									<td><?php echo $row['address_proof_type'];?></td>
									<td><?php echo $row['address_proof_value'];?></td>
									<td>
										<?php
											$data=json_decode($row['address_proof_data']);
											$response_data="";
											if($row['address_proof_type']=="ElectionCard"){
				$response_data.="<br><b>Epic no : </b> <i>".$data->essentials->epicNumber."</i>";
				$response_data.="<br><b>Name : </b> <i>".$data->result->name."</i>";
				$response_data.="<br><b>Birth Date : </b> <i>".$data->result->dob."</i>";
				$response_data.="<br><b>Gender : </b> <i>".$data->result->gender."</i>";
				$response_data.="<br><b>Area : </b> <i>".$data->result->district."</i>";
				echo $response_data;
											}else if($row['address_proof_type']=="AadharCard"){
				$response_data.="<br><b>Aadhar No : </b> <i>".$data->response->aadhar."</i>";
				$response_data.="<br><b>Name : </b> <i>".$data->response->name."</i>";
				$response_data.="<br><b>Birth Date : </b> <i>".$data->response->dob."</i>";
				$response_data.="<br><b>Gender : </b> <i>".$data->response->gender."</i>";
				$response_data.="<br><b>Address : </b> <i>".$data->response->address->house.",".$data->response->address->street.", ".$data->response->address->po.", ".$data->response->address->dist.", ".$data->response->address->state.",".$data->response->address->country."</i>";
				echo $response_data;
											}
											
										?>
									</td>
									
								</tr>
							</tbody>
						</table>
					</td>
					<td>
						<button type="button" class="btn btn-primary update_customer_status_button_class" data-toggle="modal" data-target="#update_customer_status_modal" data-value-type="<?php echo $row['customer_id'];?>">Update</button>
					</td>
				</tr>
			<?php
			$cnt++;
			}
			?>
			</tbody>
		  </table>
    </div>
    
	</div>
</div>
</div>
</div>
</div>

<!-- Tag Modal -->
<div id="update_customer_status_modal" class="modal fade" role="dialog" data-keyboard="false" data-backdrop="static">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal">&times;</button>
				<h4 class="modal-title">Update Status</h4>
			</div>
			<div class="modal-body">
				<div class="row" style="margin-bottom:50px">
					<div class="col-sm-6">
						<input type="radio" class="update_status_radio" name="update_status_radio" value="Accept" checked> Accept
					</div>
					<div class="col-sm-6">
						<input type="radio" class="update_status_radio" name="update_status_radio" value="Reject"> Reject
					</div>
				</div>
				<form action="" method="post" class="form-horizontal" id="update_status_form">
					<input type="hidden" id="hidden_customer_id" name="hidden_customer_id">
					<div class="form-group">
						<label class="control-label col-sm-2" for="tactics">Salesman:</label>
						<div class="col-sm-10">
							<select class="form-control" name="salesman_id" id="salesman_id" required>
								<option value="">select salesman</option>
								<?php
								$salesman_rows=$bdd->getAll("SELECT * FROM salesman");
								foreach($salesman_rows as $row){
								?>
									<option value="<?php echo $row['id'];?>"><?php echo $row['name'];?></option>
								<?php
								}
								?>
							</select>
						</div>
					</div>
					<div class="form-group">
						<label class="control-label col-sm-2" for="tactics">Chassis No:</label>
						<div class="col-sm-10">
							<input type="text" class="form-control" name="chassis_no" id="chassis_no" required>
						</div>
					</div>
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-primary" id="update_customer_status">Update</button>
			</div>
		</div>
	</div>
</div>
<?php
include "footer.php";
$bdd->disconnect();
?>
<script>
$(".update_customer_status_button_class").click(function(){
	var data_value=$(this).data('value-type');
	$("#hidden_customer_id").val(data_value);
});

$(".update_status_radio").click(function(){
	var update_status_radio=$('input[name="update_status_radio"]:checked').val();
	if(update_status_radio=="Reject"){
		document.getElementById("update_status_form").style.display = 'none'
	}else if(update_status_radio=="Accept"){
		document.getElementById("update_status_form").style.display = 'block'
	}
});

$("#update_customer_status").click(function(){
	var status=$('input[name="update_status_radio"]:checked').val()
	var customer_id=$("#hidden_customer_id").val()
	var salesman_id=$("#salesman_id").val()
	var chassis_no=$("#chassis_no").val()
	if(status=="Reject"){
		dataString='action=updateCustomerStatus&customer_id='+customer_id+'&salesman_id='+salesman_id+"&chassis_no="+chassis_no+"&status="+status;
		$.ajax({
			type:'POST',
			async: false,
			url:'ajax_data.php',
			data:dataString,
			success:function(data){
				alert('customer status updated'+data)
			}
		});
		$("#update_customer_status_modal").modal("hide")
		$('#table_row_'+customer_id).remove()
	}else if(status=="Accept"){
		if(customer_id=="" || salesman_id=="" || chassis_no==""){
		
			alert('Select salesman or enter chassis no')
			return false
		}else{
			dataString='action=updateCustomerStatus&customer_id='+customer_id+'&salesman_id='+salesman_id+"&chassis_no="+chassis_no+"&status="+status;
			$.ajax({
				type:'POST',
				async: false,
				url:'ajax_data.php',
				data:dataString,
				success:function(data){
					alert('customer status updated'+data)
				}
			});
			$("#update_customer_status_modal").modal("hide")
			$('#table_row_'+customer_id).remove()
		}
	}
});
</script>