<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css" rel="stylesheet"></link>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCDgFNMkZRmDv6tkxbKut5eIqaQbcM8zS8&sensor=true&libraries=places"></script>
  <!--<script src="alertify/alertify.min.js"></script>-->
<style>
/* 
Max width before this PARTICULAR table gets nasty
This query will take effect for any screen smaller than 760px
and also iPads specifically.
*/
@media only screen and (max-width: 560px), (min-device-width: 568px) and (max-device-width: 1024px) {
/* Force table to not be like tables anymore */
table, thead, tbody, th, td, tr {
	display: block;
}
/* Hide table headers (but not display: none;, for accessibility) */
thead tr {
	position: absolute;
	top: -9999px;
	left: -9999px;
}
tr {
	border: 0px solid #ccc;
}
td {
	/* Behave  like a "row" */
	border: none;
	/*border-bottom: 1px solid #eee; */
	position: relative;
	padding-left: 50%;
}
td:before {
	/* Now like a table header */
	position: unset; /*absolute causing issues*/
	/* Top/left values mimic padding */
	top: 6px;
	left: 6px;
	width: 45%;
	padding-right: 10px;
	white-space: nowrap;
}
/*
Label the data
*/
td:nth-of-type(1):before {content: "S.No";}
td:nth-of-type(2):before {content: "Name";}
td:nth-of-type(3):before {content: "Email & Phone";}
td:nth-of-type(4):before {content: "Type";}
td:nth-of-type(5):before {content: "Status";}
td:nth-of-type(6):before {content: "Actions";}
.table-checkable tr > td:first-child, .table-checkable tr > th:first-child {
  max-width: 20%;  
}
}
@media only screen and (max-width: 480px), (min-device-width: 300px) and (max-device-width: 480px) {
	.table-checkable tr > td:first-child, .table-checkable tr > th:first-child {
	  max-width: 100%;  
	}
	td:nth-of-type(1)::before {
	  content: "";
	}
}
</style>
<script src="assets/plugins/jquery/jquery.min.js" ></script>
<script type="text/javascript">
$(document).ready(function() {
	$('.submit-pop').click(function () {		
		var btn_id = this.id;	
		var strarray = btn_id.split('-');	
		var tech_id = strarray[1];	
		var amount = $('#amount-'+tech_id).val();			
		if(amount == '' || amount == 'undefined'){
			alert("Enter payment amount");
			return false;
		} else if(amount<=0){
			alert("Enter a valid amount");
			return false;
		}
		if(tech_id != '' && tech_id != 'undefined'){		
			$.ajax({url:"ajax/ajax.php?action=tech_mark_paid&tech_id="+tech_id+"&amount="+amount,success:function(result){								
				if(result=='success'){
					$("#ajax_result-"+tech_id).html('<span style="color:green;">The payment has been submitted</span>');					
				} else {					
					$("#ajax_result-"+tech_id).html('<span style="color:red;">'+result+'</span>');					
				}													
			}});
		} else {
			alert("something went wrong");
			return false;
		}
			
	});		
});	
</script>
<div class="page-content-wrapper">
                <div class="page-content">
                    <div class="page-bar">
                        <div class="page-title-breadcrumb">
                            <div class=" pull-left">
                                <!--<div class="page-title">Manage Technician</div>-->
                            </div>
                            <ol class="breadcrumb page-breadcrumb pull-right">
                                <li><i class="fa fa-home"></i>&nbsp;<a class="parent-item" href="index.php?page=home">Home</a>&nbsp;<i class="fa fa-angle-right"></i>
                                </li>
                                <li class="active">Manage Technician</li>
                            </ol>
                        </div>
                    </div>
                    
	 <div class="row">
                        <div class="col-md-12">
                            <div class="card card-topline-red">
                                <div class="card-head">
                                    <header>Manage Technician</header>
                                    <div class="tools">                                        
	                                    <a class="t-collapse btn-color fa fa-chevron-down" href="javascript:;"></a>
	                                    <a class="t-close btn-color fa fa-times" href="javascript:;"></a>
                                    </div>
                                </div>
                                <div style="margin: 10px;">
	                                <form name="form" id="form" action="" method="post">	                                
										<input type="text" style="margin-right: 8px;width: 240px;" name="searchvalue" id="searchvalue" placeholder="Keywords.."  value="<?php if(isset($_POST['searchvalue'])) { echo $_POST['searchvalue']; }  ?>" />
										<select name="dispatch_type" id="dispatch_type" >
											<option value="">Admin Dispatch</option>
											<option value="admin" <?php if(isset($_REQUEST['dispatch_type']) && $_REQUEST['dispatch_type']=='admin'){ echo 'selected'; } ?> >Yes</option>
											<option value="direct" <?php if(isset($_REQUEST['dispatch_type']) && $_REQUEST['dispatch_type']=='direct'){ echo 'selected'; } ?> >No</option>
										</select>
										<?php /*?><select name="staff_type" id="staff_type" >
											<option value="">Own Staff</option>
											<option value="own" <?php if(isset($_REQUEST['staff_type']) && $_REQUEST['staff_type']=='own'){ echo 'selected'; } ?> >Yes</option>
											<option value="loop" <?php if(isset($_REQUEST['staff_type']) && $_REQUEST['staff_type']=='loop'){ echo 'selected'; } ?> >No</option>
										</select>

										<select name="optype" id="optype" >
											<option value="">Operator</option>
											<option value="yes" <?php if(isset($_REQUEST['optype']) && $_REQUEST['optype']=='yes'){ echo 'selected'; } ?> >Yes</option>
											<option value="no" <?php if(isset($_REQUEST['optype']) && $_REQUEST['optype']=='no'){ echo 'selected'; } ?> >No</option>
										</select><?php */?>

										<select name="cattype" id="cattype" >
											<option value="">Category Added</option>
											<option value="yes" <?php if(isset($_REQUEST['cattype']) && $_REQUEST['cattype']=='yes'){ echo 'selected'; } ?> >Yes</option>
											<option value="no" <?php if(isset($_REQUEST['cattype']) && $_REQUEST['cattype']=='no'){ echo 'selected'; } ?> >No</option>
										</select>
										<select name="status" id="status" >
											<option value="">-- Status --</option>
											<option value="Active" <?php if(isset($_REQUEST['status']) && $_REQUEST['status']=='Active'){ echo 'selected'; } ?> >Active</option>
											<option value="Pending" <?php if(isset($_REQUEST['status']) && $_REQUEST['status']=='Pending'){ echo 'selected'; } ?> >Pending</option>
											<option value="Disabled" <?php if(isset($_REQUEST['status']) && $_REQUEST['status']=='Disabled'){ echo 'selected'; } ?> >Disabled</option>
										</select>
										<select name="availability" id="availability" >
												<option value="">-- Availability --</option>
												<option value="on" <?php if(isset($_REQUEST['availability']) && $_REQUEST['availability']=='on'){ echo 'selected'; } ?> >On</option>
												<option value="off" <?php if(isset($_REQUEST['availability']) && $_REQUEST['availability']=='off'){ echo 'selected'; } ?> >Off</option>
										</select>
										<select name="category" id="category" >
                                                    	<option value="">--Select Category--</option>
										              <?php if(mysqli_num_rows($categoryRs)>0){
										              	while($categoryRec = mysqli_fetch_array($categoryRs)){
										              	//check if sub cat exist										              	
														$subcatRs = mysqli_query($con, "SELECT id, name FROM tbl_service_categories WHERE status = 'Active' AND parent_id = '".$categoryRec['id']."' ORDER BY name"); ?>
										              	<option <?php if(mysqli_num_rows($subcatRs)>0){ /*echo 'disabled="disabled"';*/ } ?> value="<?php echo $categoryRec['id']; ?>" <?php if(isset($_REQUEST['category']) && $_REQUEST['category']==$categoryRec['id']) { echo 'selected="selected"'; } ?> ><?php echo $categoryRec['name']; ?></option>
                                                        <?php if(mysqli_num_rows($subcatRs)>0){
										              	while($subcatRec = mysqli_fetch_array($subcatRs)){ ?>
                                                        	<option value="<?php echo $subcatRec['id']; ?>" <?php if(isset($_REQUEST['category']) && $_REQUEST['category']==$subcatRec['id']) { echo 'selected="selected"'; } ?> ><?php echo '--'.$subcatRec['name']; ?></option>
                                                        <?php } } ?>
										              <?php } } ?>
									  </select>
										                                    
										<select name="paid" id="paid">
				                                	<option value="">--- Is Paid --</option>	                                	
				                                	<option value="yes" <?php if(isset($_REQUEST['paid']) && $_REQUEST['paid']=='yes'){ echo 'selected'; }?> >Yes</option>
				                                	<option value="no" <?php if(isset($_REQUEST['paid']) && $_REQUEST['paid']=='no'){ echo 'selected'; }?> >No</option>
				                        </select>
				                        <select name="city" id="city" >
                                                    	<option value="">--Select city--</option>
										              <?php if(mysqli_num_rows($cityRs)>0){
										              	while($cityRec = mysqli_fetch_array($cityRs)){ ?>
										              	<option value="<?php echo $cityRec['id']; ?>" <?php if(isset($_REQUEST['city']) && $_REQUEST['city']==$cityRec['id']) { echo 'selected="selected"'; } ?> ><?php echo $cityRec['name']; ?></option>
                                                      <?php } } ?>
									    </select>
									    <select name="license" id="license" >
												<option value="">License</option>
												<option value="yes" <?php if(isset($_REQUEST['license']) && $_REQUEST['license']=='yes'){ echo 'selected'; } ?> >Yes</option>
												<option value="no" <?php if(isset($_REQUEST['license']) && $_REQUEST['license']=='no'){ echo 'selected'; } ?> >No</option>
										</select>
										<br>
											<button class="btn mt-2" name="submit1">Search</button>&nbsp;
											<button class="btn mt-2" name="clear">Clear</button>
															
									</form>
								</div>
                                <div class="card-body ">
                                    <!--<div class="row p-b-20">
                                        <div class="col-md-12 col-sm-12 col-12">
                                            <div class="btn-group">
                                                <button onclick="location.href = 'index.php?page=add_technician';" id="addRow1" class="btn btn-info">
                                                    Add New <i class="fa fa-plus"></i>
                                                </button>
                                            </div>
                                        </div>                                        
                                    </div>-->
                                    <?php if(isset($_REQUEST['success']) && !isset($_POST['submit1'])){ ?><div class="alert alert-success"><?php echo urldecode($_REQUEST['success']); ?></div></div><?php } ?>
                                    <?php if(isset($execDetails) && mysqli_num_rows($execDetails)>0) { ?>
                                    <table class="table table-striped table-bordered table-hover table-checkable order-column" style="width: 100%" id="example4">
                                        <thead>
                                            <tr>                                                
                                                <th> S.No. </th>
                                                <th> ID </th>
                                                <th> Name </th>
                                                <th> Email & Phone </th>
                                                <th> Own Staff </th>
                                                <th> Operator </th>
                                                <th> Admin Dispatch </th>                                                
                                                <th> Status </th>
                                                <th> Actions </th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        	<?php
                                        	if(isset($_GET['p']) && is_numeric($_GET['p']) && ($_GET['p'] > 1)) {
												$slNo = ($_GET['p']*30-29);
											} else {
												$slNo = 1;
											}
											while($resDetails = mysqli_fetch_array($execDetails)) {	
												//check if operator	
												$isOperator = FALSE;
												$hasPayment = FALSE;
												$lastPaidRs = mysqli_query($con, "SELECT amount, DATE(date) AS date FROM tbl_technician_paid WHERE technician_id = '".$resDetails['id']."' ORDER BY id DESC LIMIT 1");
												if(mysqli_num_rows($lastPaidRs)>0){
														$lastPaidRec = mysqli_fetch_array($lastPaidRs);
														$lastPaidDate = $lastPaidRec['date'];
														$lastPaidAmount = $lastPaidRec['amount'];														
														$hasPayment = TRUE;
												}
												$chkOperatorRs = mysqli_query($con, "SELECT id FROM tbl_operator_locations WHERE operator_id = '".$resDetails['id']."'");
												if(mysqli_num_rows($chkOperatorRs)>0){
													$isOperator = TRUE;													
													//monthly payment for operator
													//get last paid date and amount. Find next due date
													if($hasPayment){
														$dueDate = date('d-m-Y', strtotime('+1 month', strtotime($lastPaidDate)));
													}													 												
												} else {
													//yearly payment for paid tech
													if($hasPayment){
														$dueDate = date('d-m-Y', strtotime('+1 year', strtotime($lastPaidDate)));
													}													
												} ?>
                                            <tr class="odd gradeX" <?php if($resDetails['operator']=='yes'){ echo 'style="background-color:#e2b2fc;"'; }?> >                                                
                                                <td><?php echo $slNo; ?></td>
                                                <td>
                                                	<?php echo TECH_ID.$resDetails['id']; ?>
                                                	<?php if($hasPayment){ ?>
                                                		<div class="paid">Paid</div>
                                                	<?php } ?>
                                                </td>
                                                <td><?php echo $resDetails['name']; ?></td>
                                                <td>
                                                	<?php echo $resDetails['email']; ?><br/>
                                                	<?php echo $resDetails['phone']; ?>
                                                </td>
                                                <td><?php if($resDetails['type']=='own'){ echo 'Yes'; } else { echo 'No'; } ?></td>
                                                <td><?php if($resDetails['operator']=='yes'){ echo 'Yes'; } else { echo 'No'; } ?></td>
                                                <td><?php if(in_array($resDetails['city'], $dispatchArray)){ echo 'Yes'; } else { echo 'No'; } ?></td>
                                                
                                                <td>
                                                	<div class="col-12">
                                                		<a target="_blank" class="btn btn-xs deepPink-bgcolor dropdown-toggle no-margin" href="index.php?page=view_technician&id=<?php echo $resDetails['id']; ?>">View</a><br>
                                                	</div>
                                                	<div class="col-12 pt-2">
	                                                	<?php if($resDetails['status']=='Active'){?>
	                                                    <span class="label label-sm label-success"> <?php echo $resDetails['status'];?> </span>
	                                                    <?php } else if($resDetails['status']!='Acrtive'){?>
	                                                    <span class="label label-sm label-warning"> <?php echo $resDetails['status'];?> </span>
	                                                    <?php } else { echo $resDetails['status']; }?>
                                                    </div>
                                                </td>                                                
                                                <td class="valigntop">
                                                    <div class="btn-group">
                                                        <button class="btn btn-xs deepPink-bgcolor dropdown-toggle no-margin" type="button" data-toggle="dropdown" aria-expanded="false"> Actions
                                                            <i class="fa fa-angle-down"></i>
                                                        </button>
                                                        <ul class="dropdown-menu pull-left" role="menu">  
                                                        	  
                                                        	<li>
                                                                <a target="_blank" href="index.php?page=admin_login&action=tech_login&tech_id=<?php echo $resDetails['id']; ?>">
                                                                    <i class="icon-user"></i> Login </a>
                                                            </li>
                                                        	<!--<li>
                                                                <a id="confirm" href="index.php?page=view_technician&id=<?php echo $resDetails['id']; ?>">
                                                                    <i class="icon-user"></i> View Details </a>
                                                            </li>-->                                                                                                                   
                                                            <li>
                                                                <a href="index.php?page=add_technician&id=<?php echo $resDetails['id']; ?>">
                                                                    <i class="icon-user"></i> Edit Technician </a>
                                                            </li>
                                                            <!--<li>
                                                                <a href="index.php?page=add_salary&staff_id=<?php echo $resDetails['id']; ?>">
                                                                    <i class="icon-docs"></i> Add Salary </a>
                                                            </li>
                                                            <li>
                                                                <a href="index.php?page=add_leave&staff_id=<?php echo $resDetails['id']; ?>">
                                                                    <i class="icon-docs"></i> Add Leave </a>
                                                            </li> -->
                                                            <?php if($resDetails['status']!='Active'){ ?>
                                                            	<li>
	                                                                <a href="index.php?page=manage_technician&id=<?php echo $resDetails['id']; ?>&action=activate">
	                                                                    <i class="icon-user"></i> Activate </a>
	                                                            </li>
                                                            <?php } else { ?>
                                                            	<li>
	                                                                <a href="index.php?page=tech_add_credit&tech_id=<?php echo $resDetails['id']; ?>">
	                                                                    <i class="icon-docs"></i> Add Credit </a>
	                                                            </li>
                                                            	<li>
	                                                                <a onclick="return confirm('Are you sure to disable this technician?');" href="index.php?page=manage_technician&id=<?php echo $resDetails['id']; ?>&action=disable&p=<?php echo $p;?>">
	                                                                    <i class="icon-user"></i> Disable </a>
	                                                            </li>
	                                                        <?php } ?>
                                                            <?php if(!$isOperator && $resDetails['type']!='own'){ ?>
                                                            	<!--<li>
	                                                                <a href="index.php?page=manage_technician&id=<?php echo $resDetails['id']; ?>&action=make_operator">
	                                                                    <i class="icon-user"></i> Make Operator </a>
	                                                            </li>-->
<!--
	                                                            <li>
	                                                                <a target="_blank" href="index.php?page=operator_location&tech_id=<?php echo $resDetails['id']; ?>&action=make_operator">
	                                                                    <i class="icon-user"></i> Make Operator </a>
	                                                            </li>
-->
                                                            <?php } else if($isOperator){ ?>                                                            	
                                                            	<li>
	                                                                <a href="index.php?page=operator_location&tech_id=<?php echo $resDetails['id']; ?>">
	                                                                    <i class="icon-user"></i> Operator Location </a>
	                                                            </li>
                                                            	<li>
	                                                                <a href="index.php?page=manage_technician&id=<?php echo $resDetails['id']; ?>&action=make_normal">
	                                                                    <i class="icon-user"></i> Make Normal </a>
	                                                            </li>
                                                            <?php } ?>
                                                            <!--<li>
                                                                <a target="_blank" href="index.php?page=tech_accounts&id=<?php echo $resDetails['id']; ?>">
                                                                    <i class="icon-docs"></i> Accounts </a>
                                                            </li>-->
                                                            <?php if($resDetails['status']=='Active'){ ?>
                                                            	<?php if($resDetails['type']!='own'){ ?>	
		                                                            <li>
		                                                                <a target="_blank" href="index.php?page=tech_payments&tech_id=<?php echo $resDetails['id']; ?>">
		                                                                    <i class="icon-docs"></i> Payments from <?php echo(SITE_NAME); ?> </a>
		                                                            </li>
	                                                            <?php } ?>
                                                            <li>
                                                                <a target="_blank" href="index.php?page=tech_incomes&tech_id=<?php echo $resDetails['id']; ?>">
                                                                    <i class="icon-docs"></i> Payments to <?php echo(SITE_NAME); ?> </a>
                                                            </li>
                                                            <?php if($isOperator){ ?>
                                                            <li>
                                                                <a target="_blank" href="index.php?page=sub_tech_incomes&tech_id=<?php echo $resDetails['id']; ?>">
                                                                    <i class="icon-docs"></i> Subtech. Payments </a>
                                                            </li>
                                                            <?php } ?>
                                                            <!--<li>
                                                                <a href="index.php?page=tech_reviews&tech_id=<?php echo $resDetails['id']; ?>">
                                                                    <i class="icon-docs"></i> Reviews </a>
                                                            </li>-->
                                                            <?php } ?>  
                                                            <li>
                                                                <a target="_blank" href="index.php?page=change_password&action=tech_pass&id=<?php echo $resDetails['id']; ?>">
                                                                    <i class="icon-user"></i> Change Password </a>
                                                            </li>                                                          
                                                            <li>
                                                                <a  onclick="return confirm('Are you sure to delete this item?');" href="index.php?page=manage_technician&action=delete&del_id=<?php echo $resDetails['id']; ?>&p=<?php echo $p;?>">
                                                                    <i class="icon-docs"></i> Delete </a>
                                                            </li>                                                           
                                                        </ul>
                                                        <!-- The Modal -->
															<div class="modal fade" id="myModal<?php echo $resDetails['id'];?>">
															  <div class="modal-dialog">
															    <div class="modal-content">						
															      <!-- Modal Header -->
															      <div class="modal-header">
															        <h4 class="modal-title">Mark Paid</h4>
															        <button type="button" class="close" data-dismiss="modal">&times;</button>
															      </div>						
															      <!-- Modal body -->
															      <div class="modal-body">
															      	<div id="ajax_result-<?php echo $resDetails['id'];?>"></div>						      	
															      	 <form name="regForm" id="regForm<?php echo $resDetails['id'];?>" action="" method="post">
															      	 	<?php if($hasPayment){ ?>							
															      	 	<div class="form-group row">
																		  <label for="email" class="col-4 col-form-label">Last Paid On</label>
																		  <div class="col-6 pt-2">
																		    <?php echo getFormattedDate($lastPaidDate); ?>
																		  </div>
																		</div>
																		<div class="form-group row">
																		  <label for="email" class="col-4 col-form-label">Last Paid Amount</label>
																		  <div class="col-6 pt-2">
																		    <?php echo 'Rs.'.$lastPaidAmount; ?>
																		  </div>
																		</div>
																		<div class="form-group row">
																		  <label for="email" class="col-4 col-form-label">Due Date</label>
																		  <div class="col-6 pt-2">
																		    <?php echo $dueDate; ?>
																		  </div>
																		</div>	
																		<?php } ?>																										
																		<div class="form-group">
																		  <label for="email" class="col-12 col-form-label">Enter Amount</label>
																		  <div class="col-12">
																		    <input class="form-control col-12" name="amount" id="amount-<?php echo $resDetails['id'];?>" type="text" value="<?php if(isset($_POST['amount']) && $_POST['amount']!=''){ echo $_POST['amount']; } ?>">
																		  </div>
																		</div>																						
																		<div class="form-group row justify-content-md-center mt-4 mb-2">			
																			 <div class="col-12">				 	
																				  <button id="btn-<?php echo $resDetails['id'];?>" type="button" name="submit" data-backdrop="static" class="btn btn-primary submit-pop">Submit</button>					    
																			  </div>
																	    </div>
																	</form>
															      </div>						
															      <!-- Modal footer -->
															      <div class="modal-footer">
															        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
															      </div>						
															    </div>
															  </div>
															</div>
															<script>
															/*$( "form" ).submit(function( event ) {
															  event.preventDefault();
															});*/
															</script>
														<!-- The Modal -->
                                                    </div>
                                                    <button type="button" class="btn btn-primary btn-sm mt-2" style="margin: 0;padding: 6px;" data-toggle="modal" data-target="#myModal<?php echo $resDetails['id'];?>">Mark Paid</button>
                                                </td>
                                            </tr>
                                            <?php ++$slNo; } ?>                                            
                                        </tbody>
                                    </table>
                                    <div style="float: left; "> <?php echo $pageNums; ?> </div>
                                    <?php } else { ?>
                                    	<div class="alert alert-danger">Sorry! No Results Found.</div>					        		
					        		<?php }	?> 
                                </div>
                            </div>
                        </div>
                    </div>
                   


	<!--<script>
		function reset () {
			$("#toggleCSS").attr("href", "alertify/css/alertify.default.css");
			alertify.set({
				labels : {
					ok     : "OK",
					cancel : "Cancel"
				},
				delay : 5000,
				buttonReverse : false,
				buttonFocus   : "ok"
			});
		}

		// ==============================
		// Standard Dialogs
		$("#alert").on( 'click', function () {
			reset();
			alertify.alert("This is an alert dialog");
			return false;
		});

		$("#confirm").on( 'click', function () {
			reset();
			var res = 'no';
			alertify.confirm("This is a confirm dialog", function (e) {
				if (e) {
					res = 'yes';
					//alertify.success("You've clicked OK");
					alert(this.getAttribute("href"));
					return true;
					exit;
				} else {
					alertify.error("You've clicked Cancel");
				}
			});
			alert(res);
			if(res==='yes'){
				return true;
			} else {
				return false; 
			}			
		});	

	</script>-->
</div>
</div>