<?php

/**
 * Create a metabox with multiple fields.  
**/

//Create Metabox

function dap_create_metabox() {

	// Can only be used on a single post type (ie. page or post or a custom post type). 
	//$screens = array( 'post', 'page' );
	$screens = get_all_posts_type();
	foreach ( $screens as $screen ) {
		add_meta_box(
			'dap_metabox', // Metabox ID
			'<i class="fa fa-credit-card"></i> Pay-per-post', // Title to display
			'dap_render_metabox', // Function to call that contains the metabox content
			$screen,
			'normal', // Where to put it (normal = main colum, side = sidebar, etc.)
			'default' // Priority relative to other metaboxes
		);
	}     
}
add_action( 'add_meta_boxes', 'dap_create_metabox' );

 
	/**
	 * Render the metabox markup
	 * This is the function called in `dap_create_metabox()`
	 */
	function dap_render_metabox() {
      
		// Variables
		 global $post; // Get the current post data		 
		//$details = Dap_PayPerPost::loadPayPerPostByPostId($post->ID);
		
		//wp_enqueue_script("dap_postmetabox-min-js", "//cdn.tinymce.com/4/tinymce.min.js?ver=4.9.4", false, "10.0.2");
		//wp_enqueue_style( "dap_postmetabox-css",plugin_dir_url(__FILE__). 'includes/css/dap_postmetabox.css?v='.rand(10,1000), false, "10.0.2" );
		//wp_enqueue_script("dap-tinymce-js",  '//cdn.tinymce.com/4/tinymce.min.js', false, "4.9.10" );
		wp_enqueue_script( "dap_bootstrap-js",'//maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js', false, "10.0.2" );
		//wp_enqueue_script( "dap_postmetabox-js",plugin_dir_url(__FILE__). 'includes/js/dap_postmetabox.js?v='.rand(10,1000), false, "10.0.2" );
		 
		echo '<link  rel="stylesheet" type="text/css" href="'.plugin_dir_url(__FILE__). 'includes/css/dap_postmetabox.css?v='.rand(10,1000).'">';
		echo '<script src="'.plugin_dir_url(__FILE__). 'includes/js/dap_postmetabox.js?v='.rand(10,1000).'"></script>';
		
		
		$lldocroot = defined('SITEROOT') ? SITEROOT : $_SERVER['DOCUMENT_ROOT'];
		$loader_html =  DapMemberShowLoader();
		
		$pppEnabled = 'N';
		
		
		if(isset($_GET['post'])){
			$dapwp_post_obj = Dap_PayPerPost::loadPayPerPostByPostId($post->ID);
			if(isset($dapwp_post_obj)){
				$pppEnabled = $dapwp_post_obj->getEnablePayPerPost();
				if($pppEnabled != 'Y'){
					wp_enqueue_script( "dap_sweetalert-js",'//cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js', false, "2.1.2" );	
				} 
			}
	        
		}
		
		if(file_exists($lldocroot . "/dap/dap-config.php")) include_once ($lldocroot . "/dap/dap-config.php"); 	
		echo $loader_html;
?>
    
	<fieldset>
		<input type="hidden" id="dapwp_website_url" name="dapwp_website_url" value="<?php echo site_url('');?>">
		<input type="hidden" id="pppEnabled" name="pppEnabled" value="<?php echo $pppEnabled;?>">
		
		<input type="hidden" id="dapwp_post_id" name="dapwp_post_id" value="<?php echo $post->ID;?>">
		<div class="wrapper_pay_per_post" style="display:none;">
			<div class="pay_per_post_content">
				<ul class="nav nav-tabs  pay_per_post_left" id="myTab" role="tablist">
					<li class="nav-item active">
						<a class="nav-link active"  href="#general" role="tab" aria-controls="general"><span class="right_tick"><i class="fa fa-cog" aria-hidden="true"></i></span>General Settings</a>
					</li>
					<li class="nav-item">
						<a class="nav-link "  href="#messages" role="tab" aria-controls="messages"><span class="right_tick"><i class="fa fa-envelope-o" aria-hidden="true"></i></span>Messages</a>
					</li>
					 <li class="nav-item">
						<a class="nav-link "  href="#button_configuration" role="tab" aria-controls="button_configuration"><span class="right_tick"><i class="fa fa-tasks" aria-hidden="true"></i></span>Button Configuration </a>
					</li>
					 <li class="nav-item">
						<a class="nav-link "  href="#payment_button" role="tab" aria-controls="payment_button"><span class="right_tick"><i class="fa fa-money" aria-hidden="true"></i></span>Payment Button </a>
					</li>
					 <!--li class="nav-item">
						<a class="nav-link "  href="#button_layout" role="tab" aria-controls="button_layout"><span class="right_tick"><i class="fa fa-eye" aria-hidden="true"></i></span>Button Preview </a>
					</li-->
					 <li class="nav-item">
						<a class="nav-link "  href="#landing_page" role="tab" aria-controls="landing_page"><span class="right_tick"><i class="fa fa-openid" aria-hidden="true"></i></span>Landing Page </a>
					</li>
				</ul>

				<div class="tab-content pppAllTabsMainOuter">
					<div class="tab-pane active" id="general" role="tabpanel">
						<h3>General Settings</h3>
						<div class="tab-content-wrapper">
							<div class="metabox_individual metabox_list">
								<label for="dap_custom_metabox_show_teaser" class="metabox_label">Do you want to enable Pay-Per-Post?</label>
								<div class="metabox_right-content">
									<div class="switch_onoff">
										<input class="checkbox" name="dap_custom_metabox[enable_pay_per_post]" type="checkbox" id="enable_pay_per_post" value="Y" <?php if(isset($pppEnabled) && ($pppEnabled == 'Y')){ echo 'checked="checked"';}?>>
										<label for="enable_pay_per_post"></label>
									</div>
								</div>
							</div>
							<div class="metabox--actions-btn">
			
								<a href="#" class="metabox-btn metabox-next--btn metabox-save--btn" onclick="dapwp_save_payper_post(this,'yes')" >Save</a>
								<a href="#" class="metabox-btn metabox-next--btn" onclick="dapwp_goto_next_tab('messages')">Next</a>
							</div>
						</div>
					</div>
					
				</div>					
			</div>
		</div>    
	</fieldset>

		<?php
 

	} 
