<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="js/jquery.placepicker.js"></script>
<style>
      .placepicker-map {
        width: 100%;
        height: 300px;
      }
      .another-map-class {
        width: 100%;
        height: 300px;
      }
      .pac-container {
        border-radius: 5px;
      }
</style>
<script>
$(document).ready(function() {
   $('#licensed_yes').click(function() {
       $('#license_div').show(); 
   });
   $('#licensed_no').click(function() {
       $('#license_div').hide(); 
   });
});
</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"><?php if($isEdit){ echo 'Edit Technician - <span style="color:black;">'.getTechnicianName($editId).'</span>';} else { echo 'Add 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><a class="parent-item" href="index.php?page=manage_technician">Technician</a>&nbsp;<i class="fa fa-angle-right"></i></li><li class="active">Add Technician</li>
				</ol>
			</div>
		</div>
		<div class="row">
			<div class="col-md-12 col-sm-12">
				<div class="card card-box">
					<div class="card-head">
						<header><?php if($isEdit){ echo 'Edit Technician';} else { echo 'Add Technician'; }?></header>                                    
					</div>
					<div class="card-body" id="bar-parent1">
						<?php if(isset($error) && $error!='') { ?> 
						<div class="alert alert-danger"><?php echo $error; ?></div>
						<?php } else if(isset($success) && $success!=''){ ?>                                		
							<div class="alert alert-success"><?php echo $success; ?></div>
						<?php } else if(isset($_REQUEST['success'])){ ?>                                		
							<div class="alert alert-success"><?php echo $_REQUEST['success']; ?></div>
						<?php } ?>
						<form action="" id="form_user_add" class="form-horizontal" method="post" enctype="multipart/form-data">
							<div class="form-body">
								<!--<div class="form-group row">
									<label class="control-label col-md-3">Type<span class="required"> * </span></label>
									<div class="col-md-4">      
										<select <?php if(isset($resEdit['type'])){ ?>readonly="readonly"<?php } ?> name="type" id="type" class="form-control" >
											<option value="">--Select Type--</option>
											<option value="own" <?php if(isset($_POST['type']) && $_POST['type']=='own'){ echo 'selected'; } else if(isset($resEdit['type']) && $resEdit['type']=='own'){ echo 'selected'; }?> >Own Staff</option>
											<option value="loop" <?php if(isset($_POST['type']) && $_POST['type']=='loop'){ echo 'selected'; } else if(isset($resEdit['type']) && $resEdit['type']=='loop'){ echo 'selected'; }?> >Loop</option>
										</select>  
									</div>
								</div>-->                                        	 
								<div class="form-group row">
									<label class="control-label col-md-3">Name<span class="required"> * </span></label>
									<div class="col-md-4">                                                    
										<input name="name" type="text" id="name" data-required="1" class="form-control" value="<?php if(isset($_POST['name'])) { echo htmlentities(($_POST['name']), ENT_QUOTES); } else if(isset($resEdit['name']) && $resEdit['name']!=''){ echo $resEdit['name'];} ?>"/>
									</div>
								</div>
								<div class="form-group row">
									<label class="control-label col-md-3">Email Address</label>
									<div class="col-md-4">
										<input name="email" type="text" id="email" class="form-control" value="<?php if(isset($_POST['email'])) { echo htmlentities(($_POST['email']), ENT_QUOTES); } else if(isset($resEdit['email']) && $resEdit['email']!=''){ echo $resEdit['email'];} ?>" placeholder="Email Address" />
									</div>
								</div>
								<div class="form-group row">
									<label class="control-label col-md-3">Phone Number<span class="required"> * </span></label>
									<div class="col-md-4">                                                    
										<input name="phone" type="text" id="phone" data-required="1" class="form-control" value="<?php if(isset($_POST['phone'])) { echo htmlentities(($_POST['phone']), ENT_QUOTES); } else if(isset($resEdit['phone']) && $resEdit['phone']!=''){ echo $resEdit['phone'];} ?>"/>
									</div>
								</div>
								<div class="form-group row">
									<label class="control-label col-md-3">House Name<span class="required"> * </span></label>
									<div class="col-md-4">                                                    
										<input name="house_name" type="text" id="house_name" data-required="1" class="form-control" value="<?php if(isset($_POST['house_name'])) { echo htmlentities(($_POST['house_name']), ENT_QUOTES); } else if(isset($resEdit['house_name']) && $resEdit['house_name']!=''){ echo $resEdit['house_name'];} ?>"/>
									</div>
								</div> 
								<div class="form-group row">
									<label class="control-label col-md-3">Street</label>
									<div class="col-md-4">                                                    
										<input name="street" type="text" id="street" class="form-control" value="<?php if(isset($_POST['street'])) { echo htmlentities(($_POST['street']), ENT_QUOTES); } else if(isset($resEdit['street']) && $resEdit['street']!=''){ echo $resEdit['street'];} ?>"/>
									</div>
								</div> 
								<div class="form-group row">
									<label class="control-label col-md-3">Place<span class="required"> * </span></label>
									<div class="col-md-4">         
										<input name="place" id="place" onblur="codeAddress()" class="placepicker form-control" placeholder="Enter a location" data-required="1" value="<?php if(isset($_POST['place'])) { echo htmlentities(($_POST['place']), ENT_QUOTES); } else if(isset($resEdit['place']) && $resEdit['place']!=''){ echo $resEdit['place'];} ?>"/>                                           
										<input type="hidden" name="latitude" id="latitude" value="<?php if(isset($_POST['latitude'])) { echo $_POST['latitude']; } else if(isset($resEdit['latitude']) && $resEdit['latitude']!=''){ echo $resEdit['latitude'];} ?>">
										<input type="hidden" name="longitude" id="longitude" value="<?php if(isset($_POST['longitude'])) { echo $_POST['longitude']; } else if(isset($resEdit['longitude']) && $resEdit['longitude']!=''){ echo $resEdit['longitude'];} ?>">
									</div>
								</div>
								<div class="form-group row">
									<div id="map-canvas"></div>  
								</div>
								<div class="form-group row">
									<label class="control-label col-md-3">PIN<span class="required"> * </span></label>
									<div class="col-md-4">                                                    
										<input name="pin" type="text" id="pin" data-required="1" class="form-control" value="<?php if(isset($_POST['pin'])) { echo htmlentities(($_POST['pin']), ENT_QUOTES); } else if(isset($resEdit['pin']) && $resEdit['pin']!=''){ echo $resEdit['pin'];} ?>"/>
									</div>
								</div> 
								<div class="form-group row">
									<label class="control-label col-md-3">City</label>
									<div class="col-md-4">      
										<select name="city" id="city">
											<option value="">--Select City--</option>
											<?php if(mysqli_num_rows($citiesRs)>0){
												while($cityRec = mysqli_fetch_array($citiesRs)){?>
												<option value="<?php echo $cityRec['id']; ?>" <?php if(isset($_POST['city']) && $_POST['city']==$cityRec['id']){ echo 'selected'; } else if(isset($resEdit['city']) && $resEdit['city']==$cityRec['id']){ echo 'selected'; }?> ><?php echo $cityRec['name']; ?></option>
											<?php } } ?>
										</select>  
									</div>
								</div>
								<?php if(!$isEdit){ ?>
								<div class="form-group row">
									<label class="control-label col-md-3">Password<span class="required"> * </span></label>
									<div class="col-md-4">                                                    
										<input name="password" type="password" id="password" data-required="1" class="form-control" value="<?php if(isset($_POST['password'])) { echo $_POST['password']; } else if(isset($resEdit['password']) && $resEdit['password']!=''){ echo $resEdit['password'];} ?>"/>
									</div>
								</div> 
								<div class="form-group row">
									<label class="control-label col-md-3">Re-enter Password<span class="required"> * </span></label>
									<div class="col-md-4">                                                    
										<input name="re_password" type="password" id="re_password" data-required="1" class="form-control" value="<?php if(isset($_POST['re_password'])) { echo $_POST['re_password']; } else if(isset($resEdit['re_password']) && $resEdit['re_password']!=''){ echo $resEdit['re_password'];} ?>"/>
									</div>
								</div>    
								<?php } ?>
								<div class="form-group row">
									<label class="control-label col-md-3">Mobile2</label>
									<div class="col-md-4">                                                    
										<input name="mobile2" type="text" id="mobile2" data-required="1" class="form-control" value="<?php if(isset($_POST['mobile2'])) { echo htmlentities(($_POST['mobile2']), ENT_QUOTES); } else if(isset($resEdit['mobile2']) && $resEdit['mobile2']!=''){ echo $resEdit['mobile2'];} ?>"/>
									</div>
								</div>
								<div class="form-group row">
									<label class="control-label col-md-3">Bike & Licence</label>
									<div class="col-md-8 row pt-2">
										<div class="col-md-3">        
											<input name="bike_license" id="bike_license_yes" <?php if(isset($_POST['bike_license']) && $_POST['bike_license']=='yes'){ echo 'checked'; } else if(isset($resEdit['bike_license']) && $resEdit['bike_license']=='yes'){ echo 'checked';}?> value="yes" type="radio"> Yes   
										</div>   
										<div class="col-md-3">
											<input name="bike_license" id="bike_license_no" <?php if(isset($_POST['bike_license']) && $_POST['bike_license']=='no'){ echo 'checked'; } else if(isset($resEdit['bike_license']) && $resEdit['bike_license']=='no'){ echo 'checked';}?> value="no" type="radio"> No
										</div>
									</div>
								</div>
								<div class="form-group row">
									<label class="control-label col-md-3">Smart phone</label>
									<div class="col-md-8 row pt-2">
										<div class="col-md-3">        
											<input name="smartphone" id="smartphone_yes" <?php if(isset($_POST['smartphone']) && $_POST['smartphone']=='yes'){ echo 'checked'; } else if(isset($resEdit['smartphone']) && $resEdit['smartphone']=='yes'){ echo 'checked';}?> value="yes" type="radio"> Yes   
										</div>   
										<div class="col-md-3">
											<input name="smartphone" id="smartphone_no" <?php if(isset($_POST['smartphone']) && $_POST['smartphone']=='no'){ echo 'checked'; } else if(isset($resEdit['smartphone']) && $resEdit['smartphone']=='no'){ echo 'checked';}?> value="no" type="radio"> No
										</div>
									</div>
								</div>
								<div class="form-group row">
									<label class="control-label col-md-3">Birth Year</label>
									<div class="col-md-4">                                                    
										<input style="width:100px" name="birth_year" type="text" id="birth_year" data-required="1" class="form-control" value="<?php if(isset($_POST['birth_year'])) { echo htmlentities(($_POST['birth_year']), ENT_QUOTES); } else if(isset($resEdit['birth_year']) && $resEdit['birth_year']!=''){ echo $resEdit['birth_year'];} ?>"/>
									</div>
								</div>
								<div class="form-group row">
									<label class="control-label col-md-3">Licensed</label>
									<div class="col-md-8 row pt-2">
										<div class="col-md-3">        
											<input name="licensed" id="licensed_yes" <?php if(isset($_POST['licensed']) && $_POST['licensed']=='yes'){ echo 'checked'; } else if(isset($resEdit['licensed']) && $resEdit['licensed']=='yes'){ echo 'checked';}?> value="yes" type="radio"> Yes   
										</div>   
										<div class="col-md-3">
											<input name="licensed" id="licensed_no" <?php if(isset($_POST['licensed']) && $_POST['licensed']=='no'){ echo 'checked'; } else if(isset($resEdit['licensed']) && $resEdit['licensed']=='no'){ echo 'checked';}?> value="no" type="radio"> No
										</div>
									</div>
								</div>
								<?php //if($_SERVER['REMOTE_ADDR']=='202.88.237.77'){ ?>
								<div id="license_div" style="<?php if(isset($_POST['license_cat']) && $_POST['license_cat']!=''){ echo 'display:block;'; } else if(isset($licenseCatArray) && count($licenseCatArray)>0){ echo 'display:block;'; } else if(isset($_POST['licensed']) && $_POST['licensed']=='yes') { echo 'display:block;'; } else { echo 'display:none;';}?>" class="form-group row">
									<div class="col-md-5 ml-5 text-right">
										<select name="license_cat[]" id="license_cat" multiple>
											<option value="8" <?php if(isset($_POST['license_cat']) && in_array(8, $_POST['license_cat'])){ echo 'selected'; } else if(isset($licenseCatArray) && in_array(8, $licenseCatArray)){ echo 'selected'; }?> ><?php echo getCategory(8); ?></option>
											<option value="21" <?php if(isset($_POST['license_cat']) && in_array(21, $_POST['license_cat'])){ echo 'selected'; } else if(isset($licenseCatArray) && in_array(21, $licenseCatArray)){ echo 'selected'; }?> ><?php echo getCategory(21); ?></option>
										</select>
									</div>
								</div>
								<?php //} ?>
								<div class="form-group row">
									<label class="control-label col-md-3">Night Work</label>
									<div class="col-md-8 row pt-2">
										<div class="col-md-3">        
											<input name="night_work" id="night_work_yes" <?php if(isset($_POST['night_work']) && $_POST['night_work']=='yes'){ echo 'checked'; } else if(isset($resEdit['night_work']) && $resEdit['night_work']=='yes'){ echo 'checked';}?> value="yes" type="radio"> Yes   
										</div>   
										<div class="col-md-3">
											<input name="night_work" id="night_work_no" <?php if(isset($_POST['night_work']) && $_POST['night_work']=='no'){ echo 'checked'; } else if(isset($resEdit['night_work']) && $resEdit['night_work']=='no'){ echo 'checked';}?> value="no" type="radio"> No
										</div>                                                	                                              
									</div>
								</div>
								<div class="form-group row">
									<label class="control-label col-md-3">Upload Photo</label>
									<div class="col-md-5">        
										<input type="file" name="photo1" id="photo1">     
										<?php if(isset($resEdit['image1']) && $resEdit['image1']!=''){ ?>  
										<div style="float:right;">                                                   		
											<img src="<?php echo TECH_IMG_URL.$resEdit['image1']; ?>" style="width:80px;" >
											<a class="t-close btn-color fa fa-times" href="index.php?page=add_technician&action=del_image1&id=<?php echo $resEdit['id']; ?>"></a>
										</div>
										<?php } ?>                                  
									</div>                                                
								</div>
								
							</div>                                        
							<div class="form-group">
								<div class="offset-md-3 col-md-9">
									<?php if($isEdit){ ?>
										<button type="submit" name="update" class="btn btn-info">Update</button>
									<?php } else { ?>
										<button type="submit" name="add" class="btn btn-info">Submit</button>
									<?php } ?>                                                
									<!--<button type="button" class="btn btn-default">Cancel</button>-->
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<script>
      $(document).ready(function() {
        // Basic usage
        $(".placepicker").placepicker();
      }); // END document.ready
      
      var geo;
		function init() {
			<?php if($isEdit){ ?>				
				var valLat = <?php echo $latitude;?>;
				var valLong = <?php echo $longitude;?>;				
				var myLatLng = {lat: <?php echo $latitude;?>, lng: <?php echo $longitude;?>};				
			<?php } else { ?>
				var valLat = document.getElementById('latitude').value;
				var valLong = document.getElementById('longitude').value;								
			<?php } ?>			
			//alert(valLong);
			if(valLat!='' && valLong!=''){ /*ok*/ } else {
				valLat = 8.5241391;
				valLong = 76.9366376;
			}			
		    var map_elem = document.getElementById('map-canvas');
		    var map = new google.maps.Map(map_elem, {
		        center: new google.maps.LatLng(valLat, valLong),
		        /*mapTypeControlOptions: {
					style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
					mapTypeIds: ['roadmap', 'terrain']
				}*/		        
		        zoom: 12,
		        minZoom: 10,
		        maxZoom: 22,
		        /*mapTypeId: google.maps.MapTypeId.HYBRID*/
		        mapTypeId: 'roadmap'
		    });
		   <?php if($isEdit){ ?>
		   	var marker = new google.maps.Marker({
	          position: myLatLng,
	          map: map,
	          title: '<?php echo $resEdit['name'];?>'
	        });		   	
		   <?php } else { ?>
		   	var marker = new google.maps.Marker({
	          position: myLatLng,
	          map: map,
	          title: 'New User'
	        });
		   <?php } ?>	
		   google.maps.event.addListener(map, 'click', function(event) {
					// place a marker
					if (marker) { marker.setMap(null) }
				    marker = new google.maps.Marker({position: event.latLng, map: map});				    
				    document.getElementById("latitude").value= event.latLng.lat();
					document.getElementById("longitude").value= event.latLng.lng();				
		  });		
		}
		google.maps.event.addDomListener(window, 'load', init); 	
		geocoder = new google.maps.Geocoder();
		
		
		
		
		function codeAddress() {
		
		    //In this case it gets the address from an element on the page, but obviously you  could just pass it to the method instead
		    var address = document.getElementById('place').value;
		    //alert(address);
		    //var address = 'Kollam,Kerala,India';
			if(address!=''){
			    geocoder.geocode( { 'address' : address }, function( results, status ) {
			        if( status == google.maps.GeocoderStatus.OK ) {
			        	var map_elem = document.getElementById('map-canvas');
						var map = new google.maps.Map(map_elem, {
					        center: new google.maps.LatLng(8.5241391, 76.9366376),
					        mapTypeControl: true,
						    /*mapTypeControlOptions: {
						      style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
						      mapTypeIds: ['roadmap', 'terrain']
						    }*/					        
					        zoom: 12,
					        minZoom: 10,
					        maxZoom: 22,
					        mapTypeId: 'roadmap'
					    });
			            //In this case it creates a marker, but you can get the lat and lng from the location.LatLng
			            map.setCenter( results[0].geometry.location );			           
			            var marker = new google.maps.Marker( {
			                map     : map,
			                position: results[0].geometry.location
			            } );
			            //change lat long values in text fields
			            document.getElementById('latitude').value = results[0].geometry.location.lat();
						document.getElementById('longitude').value = results[0].geometry.location.lng();
						//change location if pick a point
			            google.maps.event.addListener(map, 'click', function(event) {
						   //alert( 'Your location is at - ' + event.latLng.lat() + ', ' + event.latLng.lng() );
						   if (marker) { marker.setMap(null) }
				    		marker = new google.maps.Marker({position: event.latLng, map: map});
						   document.getElementById('latitude').value = event.latLng.lat();
						   document.getElementById('longitude').value = event.latLng.lng();
						});
						
			        } else {
			            alert( 'Geocode was not successful for the following reason: ' + status );
			        }
			    } );
			    
			}
		}   
</script>

