<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 type="text/javascript" src="http://www.google.com/jsapi?autoload={'modules':[{name:'maps',version:3,other_params:'sensor=false'}]}"></script>-->
<script src="js/jquery.placepicker.js"></script>
<!--<script language="javascript" src="js/geo_settings.js"></script>-->

<style>
      .placepicker-map {
        width: 100%;
        height: 300px;
      }
      .another-map-class {
        width: 100%;
        height: 300px;
      }
      .pac-container {
        border-radius: 5px;
      }
</style>

<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 Shop';} else { echo 'Add Shop'; }?></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>
                                <?php if($isEdit){ ?>
                                	<li><a class="parent-item" href="index.php?page=view_purchase_partner&id=<?php echo $_REQUEST['id']; ?>">View Shop</a>&nbsp;<i class="fa fa-angle-right"></i></li>
                                <?php } else { ?>
                                	<li><a class="parent-item" href="index.php?page=manage_purchase_partner">Manage Shops</a>&nbsp;<i class="fa fa-angle-right"></i></li>
                                <?php }?>
                                <li class="active"><?php if($isEdit){ echo 'Edit Shop';} else { echo 'Add Shop'; }?></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 Shop';} else { echo 'Add Shop'; }?></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_shop_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">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<span class="required"> * </span></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">Land 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">Mobile Number</label>
                                                <div class="col-md-4">                                                    
                                                    <input name="mobile" type="text" id="mobile" data-required="1" class="form-control" value="<?php if(isset($_POST['mobile'])) { echo htmlentities(($_POST['mobile']), ENT_QUOTES); } else if(isset($resEdit['mobile']) && $resEdit['mobile']!=''){ echo $resEdit['mobile'];} ?>"/>
                                                </div>
                                            </div>
                                            <div class="form-group row">
                                                <label class="control-label col-md-3">Shop Name</label>
                                                <div class="col-md-4">                                                    
                                                    <input name="building_name" type="text" id="building_name" data-required="1" class="form-control" value="<?php if(isset($_POST['building_name'])) { echo htmlentities(($_POST['building_name']), ENT_QUOTES); } else if(isset($resEdit['building_name']) && $resEdit['building_name']!=''){ echo $resEdit['building_name'];} ?>"/>
                                                </div>
                                            </div> 
                                            <div class="form-group row">
                                                <label class="control-label col-md-3">Address</label>
                                                <div class="col-md-4">                                                                             
                                                    <textarea name="address" id="address" ><?php if(isset($_POST['address'])) { echo htmlentities(($_POST['address']), ENT_QUOTES); } else if(isset($resEdit['address']) && $resEdit['address']!=''){ echo $resEdit['address'];} ?></textarea>
                                                </div>
                                            </div> 
                                            <div class="form-group row">
                                                <label class="control-label col-md-3">Location<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>
                                            <?php if(isset($resEdit['latitude']) && $resEdit['latitude']!=''){ ?>
                                            <div class="form-group row">
                                                <label class="control-label col-md-3">Update Lat Long</label>
                                                <div class="col-md-4 pt-3">                                                                             
                                                    <input name="change_latlong" id="change_latlong" value="yes" type="checkbox" <?php if(isset($_POST['change_latlong']) && $_POST['change_latlong']=='yes'){ echo 'checked'; }?> >
                                                </div>
                                            </div> 
                                            <?php } ?>
                                            <div class="form-group row">
                                            	<div id="map-canvas"></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>
                                            <div class="form-group row">
                                                <label class="control-label col-md-3">Commission % for Task order</label>
                                                <div class="col-md-4">                                                    
                                                    <input name="commission" type="text" id="commission" class="form-control" value="<?php if(isset($_POST['commission'])) { echo htmlentities(($_POST['commission']), ENT_QUOTES); } else if(isset($resEdit['commission']) && $resEdit['commission']!=''){ echo $resEdit['commission'];} ?>"/>
                                                </div>
                                            </div>
                                            <div class="form-group row">
                                                <label class="control-label col-md-3">Commission % for Users</label>
                                                <div class="col-md-4">                                                    
                                                    <input name="commission_user" type="text" id="commission_user" class="form-control" value="<?php if(isset($_POST['commission_user'])) { echo htmlentities(($_POST['commission_user']), ENT_QUOTES); } else if(isset($resEdit['commission_user']) && $resEdit['commission_user']!=''){ echo $resEdit['commission_user'];} ?>"/>
                                                </div>
                                            </div>
                                            <div class="form-group row">
                                                <label class="control-label col-md-3">Discount (%)</label>
                                                <div class="col-md-4">                                                    
                                                    <input name="discount" type="text" id="discount" class="form-control" value="<?php if(isset($_POST['discount'])) { echo htmlentities(($_POST['discount']), ENT_QUOTES); } else if(isset($resEdit['discount']) && $resEdit['discount']!=''){ echo $resEdit['discount'];} ?>"/>
                                                </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">Upload Logo</label>
                                                <div class="col-md-4">        
                                                	<input type="file" name="photo" id="photo">                                            
                                                </div>                                                
                                            </div>
                                            <?php if(isset($resEdit['image']) && $resEdit['image']!=''){ ?>
                                            <div class="form-group row">
                                            	<label class="control-label col-md-3"></label>
		                                        <div class="col-md-4">
		                                        	<img src="<?php echo SHOP_IMG_URL.$resEdit['image']; ?>" style="width:150px;" >
		                                        	<a onclick="return confirm('Are you sure? you want to delete this photo')" class="t-close btn-color fa fa-times" href="index.php?page=add_purchase_partner&action=del_image&id=<?php echo $resEdit['id']; ?>"></a>
		                                        </div>
		                                    </div>
		                                    <?php } ?>                      
                                            
                                        </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 $resEdit['latitude'];?>;
				var valLong = <?php echo $resEdit['longitude'];?>;
				var myLatLng = {lat: <?php echo $resEdit['latitude'];?>, lng: <?php echo $resEdit['longitude'];?>};
			<?php } else { ?>
				var valLat = document.getElementById('latitude').value;
				var valLong = document.getElementById('longitude').value;
				var myLatLng = {lat: valLat, lng: valLong};
			<?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['building_name'];?>'
	        });		   	
		   <?php } else { ?>
		   	var marker = new google.maps.Marker({
	          position: myLatLng,
	          map: map,
	          title: 'New Shop'
	        });
		   <?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
			            } );
			            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>
