<?php
global $wppas_banner_creator, $wppas_adzone_creator, $wppas_svg;

wp_enqueue_script('wp_pro_ads_ace');

$banner_id = !empty($_GET['bid']) ? $_GET['bid'] : 0;
$adzone_id = !empty($_GET['aid']) ? $_GET['aid'] : 0;
$view = !empty($_GET['view']) ? $_GET['view'] : 'banners';

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	if( isset($_POST['buysell_settings']))
	{
		$bs_settings = array(
			'provider' => $_POST['buysell_provider'],
			'cur' => $_POST['buysell_cur'],
			'order_page' => $_POST['buysell_order_page'],
			'paypal_email' => $_POST['buysell_paypal_email'],
			'paypal_debug' => $_POST['buysell_paypal_debug'],
			'paypal_sandbox' => $_POST['buysell_paypal_sandbox'],
			'vbc_image_uploads' => !empty($_POST['buysell_vbc_image_uploads']) ? $_POST['buysell_vbc_image_uploads'] : 0,
			'vbc_text' => !empty($_POST['buysell_vbc_text']) ? $_POST['buysell_vbc_text'] : 0,
			'vbc_shapes' => !empty($_POST['buysell_vbc_shapes']) ? $_POST['buysell_vbc_shapes'] : 0,
			'vbc_icons' => !empty($_POST['buysell_vbc_icons']) ? $_POST['buysell_vbc_icons'] : 0,
			'vbc_svg' => !empty($_POST['buysell_vbc_svg']) ? $_POST['buysell_vbc_svg'] : 0,
			'vbc_btn' => !empty($_POST['buysell_vbc_btn']) ? $_POST['buysell_vbc_btn'] : 0,
			'vbc_html' => !empty($_POST['buysell_vbc_html']) ? $_POST['buysell_vbc_html'] : 0,
		);
		
		update_option( 'wpproads_buysell_settings', $bs_settings);
	}
}
?>


<div class="wppas_dashboard wppas_vbc_area">
	<div class="wrap">
    
        <!-- Wordpress Messages -->
        <h2 class="messages-position"></h2>
        
        
        <div class="wppas_box_container">        
            <?php
            /*
             * CREATE BANNER
             */
			 //echo $wppas_adzone_creator->show_adzone(array('aid' => 2607));
			 
            if( current_user_can(WP_ADS_ROLE_ADMIN))
            {
                ?>
                <div class="wppas_box_group">
                	
                    
                    <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>
                    
                    
                    <?php
					if( $view == 'banners' )
					{
						?>
                        
                      <div id="wppas_visual_bc_container">
                        <!--
                         /**
                          * WPPROADS VBC BANNERS
                          */
                        -->
                        <div class="wppas_box">
                            <div class="wppas_box_header">
                                <h3><?php _e('Visual Banner Creator','wpproads');?> <span style="color: #89c466; font-style:italic;">HTML5</span> <?php _e('Banners','wpproads'); ?></h3>
                                <a href="edit.php?post_status=trash&post_type=wppas_banners" class="wppas_trash" style="position:absolute; right: 15px; top: 12px; font-size: 25px; color: #ccc;" title="<?php _e('View Trash','wpproads'); ?>">
                                		<i class="fa fa-trash" aria-hidden="true"></i>
                                </a>
                            </div>
                            <?php echo $wppas_banner_creator->all_banners_tpl(array(
									'meta_query' => array(
										array(
											'key'     => '_wpproads_banner_type',
											'value'   => 'vbc',
											'compare' => '=',
										),
									)
								)); ?>
                            
                        </div>
                        <!-- end .wppas_box -->
                        
                        
                        
                        <!--
                         /**
                          * WPPROADS ADZONES
                          */
                        -->
                        <div class="wppas_box" style="margin-top: 10px;">
                            <div class="wppas_box_header">
                            		<h3><?php _e('Adzones - (Drag&Drop banners into adzones)','wpproads'); ?></h3>
                                	<a href="edit.php?post_status=trash&post_type=wppas_adzones" class="wppas_trash" style="position:absolute; right: 15px; top: 12px; font-size: 25px; color: #ccc;" title="<?php _e('View Trash','wpproads'); ?>">
                                		<i class="fa fa-trash" aria-hidden="true"></i>
                                </a>
                            </div>
                            <?php echo $wppas_adzone_creator->all_adzoness_tpl(); ?>
                        </div>
                      </div>
                      <!-- end #wppas_visual_bc_container -->
                 		<?php
					}
					elseif( $view == 'editor' )
					{
						$type = isset($_GET['type']) && !empty($_GET['type']) ? $_GET['type'] : 'vbc';
						?>
                        <!--
                         /**
                          * VISUAL BANNER EDITOR
                          */
                        -->
                        <div class="wppas_box">
                            <div class="wppas_box_header">
                                <h3>WPPROADS <span style="color: #89c466; font-style:italic;">Visual</span> <?php _e('Banner Creator','wpproads'); ?></h3>
                            </div>
                            <div class="wppas_actions" style="position:relative;">
                            
                                <div id="wppas_visual_bc_container">
                                    <div class="wppas_new_banner_start_container">
                                        <?php $wppas_banner_creator->vbc_container_tpl(array('bid' => $banner_id, 'type', 'type' => $type)); ?>
                                    </div>
                                    <!-- end .wppas_new_banner_start_container -->
                                </div>
                                <!-- end #wppas_visual_bc_container -->
                                
                            </div>
                            <!-- end .wppas_actions -->
                        </div>
                        <!-- end .wppas_box -->
                        <?php
					}
					elseif( $view == 'adzone_editor' )
					{
						?>
                        <!--
                         /**
                          * ADZONE EDITOR
                          */
                        -->
                        <div class="wppas_box">
                            <div class="wppas_box_header">
                                <h3>WPPROADS <span style="color: #89c466; font-style:italic;">Visual</span> <?php _e('Adzone Editor','wpproads'); ?></h3>
                            </div>
                            <div class="wppas_actions" style="position:relative;">
                            
                                <div id="wppas_visual_bc_container">
                                    <div class="wppas_new_banner_start_container">
                                        <?php $wppas_adzone_creator->vbc_container_tpl(array('aid' => $adzone_id)); ?>
                                    </div>
                                    <!-- end .wppas_new_banner_start_container -->
                                </div>
                                <!-- end #wppas_visual_bc_container -->
                                
                            </div>
                            <!-- end .wppas_actions -->
                        </div>
                        <!-- end .wppas_box -->
                        <?php
					}
					?>
                    
                    
                </div>
                <!-- end .wppas_box_group -->
                <?php
            }
            ?>
            
            
            
            
            
            
            
            
            
            <!--
             /**
              * Settings BOX
              */
            -->
            <?php
			if( current_user_can(WP_ADS_ROLE_ADMIN) && !isset($_GET['view']))
			{
				?>
                <div class="wppas_box_group">
                
                    <div class="wppas_box">
                        <div class="wppas_box_header">
                            <h3><?php _e('Settings','wpproads'); ?></h3>
                        </div>
                        <div class="wppas_actions">
                            
                            <!-- 
                             /**
                              * tabs
                              */
                             -->
                            <ul id="wppas_tab" class="wppas_menu_toggle">
                                <?php
                                if( current_user_can(WP_ADS_ROLE_ADMIN))
                                {
                                    ?>
                                    <li class="selected" data-target="buyandsell-addon" title="">
                                        <i class="fa fa-usd" aria-hidden="true" style="margin-right: 5px;"></i> <?php _e('Buy and Sell Ads', 'wpproads'); ?>
                                    </li>
                                		<?php
									}
									?>
                            </ul>
                            <div class="clearFix"></div>
                        		
                                
                            <div id="buyandsell-addon" class="nfer"> <!--style="display:none;" -->
                            		<h2><?php _e('Buy and Sell Ads', 'wpproads'); ?></h2>
                                    
                                <table class="form-table">
                                    <tbody>
                                        <tr>
                                            <td colspan="2">
                                                <?php echo WPPAS_BuySell::settings_form(); ?>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        	   
                               
                            
                        </div>
                    </div>
                </div>
            <?php
			}
			?>
        </div>
        
        
    </div>
</div>

<script type="text/javascript">
var _VBC = jQuery("#wppas_visual_bc_container");

jQuery(document).ready(function($) {
	
	// DRAGGABLE
    $('#all_vbc_items').find('.wppas_draggable').draggable({
		start: function( event, ui ) {
			$(this).addClass('dragging');
		},
		stop: function( event, ui ) {
			$(this).removeClass('dragging');
		},
		helper: function() {
			return $(this).clone().appendTo('body').css({'zIndex': 5, 'width':'100px','height':'100px','overflow':'hidden','border':'solid 1px #999' });
		},
		cursor: 'move',
		containment: "document"
    });
	
	
	// DROPPABLE
	$('.wppas_droppable').droppable({
		accept: '.wppas_draggable',
		over: function(event, ui) {
			var banner_id = jQuery(ui.draggable).data('id'),
				max_banners = jQuery(this).data('max-banners'),
				linked_banners = jQuery(this).data('linked-banners');
			
			if( max_banners == '' || linked_banners < max_banners )
			{
				if( !$(this).find("li[data-id='"+banner_id+"']").length && $(this).data('size') == jQuery(ui.draggable).data('size') || !$(this).find("li[data-id='"+banner_id+"']").length && jQuery(ui.draggable).data('size') == 'fullxfull' ){
					jQuery(this).addClass("accept_hover");
				}else{
					jQuery(this).addClass("decline_hover");
				}
			}else{
				jQuery(this).addClass("decline_hover");
			}
		},
		out: function(event, ui) {
			jQuery(this).removeClass("accept_hover");
			jQuery(this).removeClass("decline_hover");
		},
		drop: function(event, ui) {
			var banner_id = jQuery(ui.draggable).data('id');
			
			if( !$(this).hasClass('decline_hover')){
				// Check if banner not yet exists in adzone.
				if( !$(this).find("li[data-id='"+banner_id+"']").length ){
					// Check if size matches.
					if( $(this).data('size') == jQuery(ui.draggable).data('size') || jQuery(ui.draggable).data('size') == 'fullxfull' ){
						
						var clone = jQuery(ui.draggable).clone();
						
						/*clone.find('a').replaceWith(function() { return this.innerHTML; });
						jQuery(this).append( clone );*/
						jQuery(this).append( '<li class="wppas_action wppas_draggable" data-id="'+clone.data('id')+'" data-size="'+clone.data('size')+'"></li>' );
						//console.log( jQuery(ui.draggable).data('id') );
						var linked_banners = $(this).sortable('toArray', {attribute: 'data-id'});
						//console.log( linked_banners );
						
						_VBC.wppas.update_adzone_banners({ID: $(this).data('id'), banners: linked_banners});
					}
				}
			}
			
			jQuery(this).removeClass("accept_hover");
			jQuery(this).removeClass("decline_hover");
		}
	}).sortable({
		items: "li:not(.placeholder)",
		containment: "parent",
		start: function( event, ui ) { 
			jQuery(this).droppable( "disable" );
			curDrag = $(ui.item ).attr('id');
			curDrop = $(ui.item).parent().attr('id'); 
		},
		stop: function( event, ui ) { 
			jQuery(this).droppable( "enable" );
			//console.log( $(this).sortable( "serialize", { key: "sort" }) );
			console.log( $(this).sortable('toArray', {attribute: 'data-id'}) );
			var linked_banners = $(this).sortable('toArray', {attribute: 'data-id'});
			_VBC.wppas.update_adzone_banners({ID: $(this).data('id'), banners: linked_banners});
		}
	}).disableSelection();
	
	
	
	
	// SORTABLE ADZONE LINKED BANNERS
	$('#linked_banners_container').sortable({
		items: "li:not(.placeholder)",
		containment: "parent",
		start: function( event, ui ) { 
			curDrag = $(ui.item ).attr('id');
			curDrop = $(ui.item).parent().attr('id'); 
		},
		stop: function( event, ui ) { 
			//console.log( $(this).sortable( "serialize", { key: "sort" }) );
			//console.log( $(this).sortable('toArray', {attribute: 'data-id'}) );
			var linked_banners = $(this).sortable('toArray', {attribute: 'data-id'});
			_VBC.wppas.update_adzone_banners({ID: $(this).data('id'), banners: linked_banners});
		}
	}).disableSelection();
	
	
	
});



</script>