<?php
global $wppas_banner_creator, $pro_ads_banners, $pro_ads_advertisers;
?>
<div class="wppas_dashboard wppas_banners wppas_vbc_area wppas">
	<div class="wrap">
    	<!-- Wordpress Messages -->
        <h2 class="messages-position"></h2>
        
        <div style="margin: 0 0 10px; text-align:left; padding:10px; background-color:#FFF;">
            <div style="float:left;"><span class="wppas_logo"></span></div>
            <div style="float:left; margin: 25px 0 0 15px;">
                <h3 style="margin:0; font-size:30px; display:inline-block;">WP PRO Advertising</h3>
                <h4 style="margin: 0px 0 0 5px; font-size:18px; display:inline-block;"><?php _e('- More than just banner ads.','wpproads'); ?></h4>
            </div>
            <div style="float:right;">
           	<small>v<?php echo WP_ADS_VERSION; ?></small> 
            </div>
            <div class="clearFix"></div>
        </div>
		
       <div style="background-color:#FFF;padding:10px 20px;margin-bottom:10px;">
       		
           <div class="filters" style="margin-bottom:40px;">
           	<h2 style="border-bottom:solid 1px #CCC;padding-bottom:5px;"><?php _e('Filters','wpproads'); ?></h2>
            	
               <div class="filter_container">
					<?php
					  $filter = isset($_GET['filter']) ? $_GET['filter'] : '';
					  $filter_id = isset($_GET['filter_id']) ? $_GET['filter_id'] : '';
                    $html = '';
                    $html.= WPPAS_TPL::select_cont(array(
                        'title' => __('Filter by','wpproads'),
                        'id' => 'wppas_filter_by',
                        'value' => $filter,
                        'select_opts' => array(
                            'empty' => array('value' => '', 'text' => __('Select', 'wpproads')),
                            'advertiser' => array('text' => __('Advertiser', 'wpproads'))
                        ),
                        'size' => 'one_forth'
                    ));
					  
					  // If filters are loaded
					    if( !empty($filter))
						{
							if( $filter == 'advertiser')
							{
								$advertisers = $pro_ads_advertisers->get_advertisers();
								
								$options = array();
								$options['empty'] = array('value' => '', 'text' => __('Select', 'wpproads'));
								foreach( $advertisers as $option)
								{
									$title = !empty($option->post_title) ? $option->post_title : $option->ID;
									$options[$option->ID] = array('value' => $option->ID, 'text' => $title);
								}
							}
							
							$html.= WPPAS_TPL::select_cont(array(
								'title' => __('Filter option','wpproads'),
								'id' => 'wppas_filter_by_option',
								'value' => $filter_id,
								'select_opts' => $options,
								'size' => 'one_forth'
							));
						}
                    echo $html;
                    ?>
              </div>
				
              <a id="wppas_apply_filter" class="button button-secondary green" style="display:none;" href="<?php echo $_SERVER['REQUEST_URI']; ?>"><?php _e('Apply Filter','wpproads');?></a>
           	<div class="filter_loader" style="display:none;"><?php _e('Loading...','wpproads'); ?></div>
           </div>
           <!-- end .filters -->
           
           
           
          <script>
		   jQuery(document).ready(function($){
			   $('#wppas_filter_by').on('change', function(){
				   $('.filter_loader').show();
				   $('.filter_container').find('#wppas_filter_by_option').closest('.input_container').remove();
				   var filter_by = $(this).val();
				   
				   $.ajax({
					   type: "POST",
					   url: ajaxurl,
					   //url: "<?php //echo admin_url('admin-ajax.php'); ?>",
					   data: "action=load_banner_filter_options&filter_by="+filter_by
					}).done(function( msg ){
						//console.log('done');
						$('.filter_loader').hide();
						$('.filter_container').append(msg);
					});
			   });
			   
			   $('body').on('change', '#wppas_filter_by_option', function(){
					$('.filter_loader').show();
					$.ajax({
					   type: "POST",
					   url: ajaxurl,
					   //url: "<?php //echo admin_url('admin-ajax.php'); ?>",
					   data: "action=select_banner_filter_option&filter="+$('#wppas_filter_by').val()+"&filter_id="+$(this).val()+"&url="+encodeURIComponent("<?php echo $_SERVER['REQUEST_URI']; ?>")
					}).done(function( msg ){
						console.log(msg);
						$('.filter_loader').hide();
						$('#wppas_apply_filter').attr('href', msg);
						$('#wppas_apply_filter').show();
						
					});
					
			   });
		   });
		   </script>
      </div> 
       
       
       
       
       
       
       <div style="background-color:#FFF;padding:10px 20px;">
           <div style="display:inline-block;width:49%;vertical-align:top;"> 
                <h2 style="border-bottom:solid 1px #CCC;padding-bottom:5px;"><?php _e('Visual Banner Creator','wpproads'); ?></h2>
                <?php
                $html = '';
                $vbc_banners = $wppas_banner_creator->all_banners_query();
                $data = array();
                foreach($vbc_banners as $i => $item)
                {
					  $add = 1;
					  // Check for filters
					  if( !empty($filter) && !empty($filter_id))
					  {
						  $value = WPPAS_TPL::vbc_data(array('id' => $item->ID, 'type' => $filter, 'default' => ''));
						  $add = $value !== $filter_id ? 0 : $add;
					  }
					  
					  if( $add )
					  {
						  $banner_status = WPPAS_TPL::vbc_data(array('id' => $item->ID, 'type' => 'status', 'default' => 'active'));
						  
						  $data['data'][$i]['ID'] = $item->ID;
						  $data['data'][$i]['title'] = $item->post_title;
						  $data['data'][$i]['link'] = '<a href="admin.php?page=wppas-banner-creator&view=editor&bid='.$item->ID.'">'.$item->post_title.'</a>';
						  $data['data'][$i]['status'] = '<span class="'.$banner_status.'">'.ucfirst($banner_status).'</span>';
					  }
                }
                
                $statsTable = new Wppas_Pagination_Table();
                $statsTable->prepare_items( $data );
                
                $html.= '<form class="banner-filter" method="get">';
                    //echo '<input type="hidden" name="page" value="'.$_REQUEST['page'].'" />';
                    $statsTable->display();
                $html.= '</form>';
               ?>
            </div>
            
            
            <div style="display:inline-block;width:49%;vertical-align:top;float:right;">
                <h2 style="border-bottom:solid 1px #CCC;padding-bottom:5px;">
                    <a href="edit.php?post_type=banners" style="color:#89c566;"><i class="fa fa-list-ul"></i></a>
                    <?php _e('Alternative Banners','wpproads'); ?>
               </h2> 
                <?php
                $html = '';
                $banners = $pro_ads_banners->get_banners();
                $data = array();
                foreach($banners as $i => $item)
                {
					  $add = 1;
					  // Check for filters
				  	  if( !empty($filter) && !empty($filter_id))
					  {
						  if( $filter == 'advertiser')
						  {
							  $value = get_post_meta( $item->ID, '_banner_advertiser_id', true );
						  }
						  $add = $value !== $filter_id ? 0 : $add;
					  }
					  
					  if( $add )
					  {
						  $banner_status = get_post_meta( $item->ID, '_banner_status', true );
						  $status = $pro_ads_banners->get_status($banner_status);
						  
						  $data['data'][$i]['ID'] = $item->ID;
						  $data['data'][$i]['title'] = $item->post_title;
						  $data['data'][$i]['link'] = '<a href="post.php?post='.$item->ID.'&action=edit">'.$item->post_title.'</a>';
						  $data['data'][$i]['status'] = '<span class="'.$status['name_clean'].'">'. $status['name'].'</span>';
					  }
                }
                
                $statsTable = new Wppas_Pagination_Table();
                $statsTable->prepare_items( $data );
                
                $html.= '<form class="banner-filter" method="get">';
                    //echo '<input type="hidden" name="page" value="'.$_REQUEST['page'].'" />';
                    $statsTable->display();
                $html.= '</form>';
               ?>
            </div>
            <div class="clearFix"></div>
        </div>  
    </div>
</div>