<?php
        global $wpdb;
        $result = $wpdb->get_results("SELECT * FROM `wp_lp_popups`");
        $post_value = get_option("popups_shortcode_" . $object->ID);

?>
<div id="singlepage_popup_container">

<?php     if($post_value != "") {  ?>

<select disabled id="popup_select_field">
	<option>Legal Eagle Popups</option>
 <?php
  foreach ($result as $tag => $code) {
     echo '<option '. ($post_value == $code->id ? " selected " : "") .'   value="'. $code->id .'">'. $code->popupName .'</option>';
  }
 ?>
</select>

<input id="submit_popup_button" value="Remove Popup" type="submit" class="legal-button btn-blk"/>
  

<?php } else { ?>

<select id="popup_select_field">
	<option>Legal Eagle Popups</option>
 <?php
  foreach ($result as $tag => $code) {
     echo '<option value="'. $code->id .'">'. $code->popupName .'</option>';
  }
 ?>
</select>

<input id="submit_popup_button" value="Add Popup" type="submit" class="legal-button btn-blk" />


<?php } ?>
</div>
<script type="text/javascript">
jQuery(document).ready(function(){

	jQuery(document).on('click' , "#submit_popup_button" , function(e){
			e.preventDefault();

   var submit_value = $(this).val();

   if (submit_value == "Remove Popup") {
   
   	var data = {
		'action': 'uc_c_my_action_remove_popup_single_page',
		'post_id': <?php echo $object->ID; ?>     // We pass php values differently!
	};
	// We can also pass the url value separately from ajaxurl for front end AJAX implementations
	jQuery.post(ajax_object.ajax_url, data, function(response) {
		
		var r = JSON.parse(response);
		if (r.result) {
			$("#singlepage_popup_container").html(r.content);
		}
		
	});



   } else {

   		//	tinyMCE.activeEditor.selection.setContent( '[wp-legalpopup pid=' + jQuery("#popup_select_field").val() + ']' );
		//	console.log(jQuery("#popup_select_field").val());
		var field_value = $("#popup_select_field").val();
		if (field_value == "Legal Eagle Popups") {
			console.log(field_value);
		} else {
		var data = {
		'action': 'uc_c_my_action_single_page',
		'field_value': field_value ,
		'post_id': <?php echo $object->ID; ?>     // We pass php values differently!
	};
	// We can also pass the url value separately from ajaxurl for front end AJAX implementations
	jQuery.post(ajax_object.ajax_url, data, function(response) {
		 
		 var r = JSON.parse(response);
		if (r.result) {
			$("#singlepage_popup_container").html(r.content);
		}


	});
		}
// else block end here.

   }

	
		});

   
});

	
</script>