/**
* wpv_admin_archive_listing_page
*
* Creates the main structure of the WPA admin listing page: wrapper and header
*
*/
function wpv_admin_archive_listing_page() {

	global $WPV_view_archive_loop;
	?>
	<div class="wrap toolset-views">

        <!-- wpv-views-listing-archive-page can be removed -->
        <div class="wpv-views-listing-page wpv-views-listing-archive-page" data-none-message="<?php _e("This WordPress Archive isn't being used for any loops.",'wpv-views') ?>" >
			<?php
				// 'trash' or 'publish'
				$current_post_status = wpv_getget( 'status', 'publish', array( 'trash', 'publish' ) );
				$search_term = urldecode( sanitize_text_field( wpv_getget( 's' ) ) );
				$arrange_by_usage = ( sanitize_text_field( wpv_getget( 'arrangeby' ) ) == 'usage' );
				
				wp_nonce_field( 'work_views_listing', 'work_views_listing' );
				wp_nonce_field( 'wpv_remove_view_permanent_nonce', 'wpv_remove_view_permanent_nonce' );
			?>
		
			<div id="icon-views" class="icon32"></div>
			<h2><!-- classname wpv-page-title removed -->
				<?php _e( 'WordPress Archives', 'wpv-views' ); ?>

					<a href="#" class="add-new-h2 page-title-action js-wpv-views-archive-add-new wpv-views-archive-add-new">
						<?php _e('Add new WordPress Archive','wpv-views') ?>
					</a>
					<?php

					if ( !empty( $search_term ) ) {
						$search_message = __('Search results for "%s"','wpv-views');
						if ( 'trash' == $current_post_status ) {
							$search_message = __('Search results for "%s" in trashed WordPress Archives', 'wpv-views');
						}
						?>
							<span class="subtitle">
								<?php echo sprintf( $search_message, $search_term ); ?>
							</span>
						<?php
					}
				?>
			</h2>

			<?php
				// Messages: trashed, untrashed, deleted

				// We can reuse the function from Views listing (there's a note saying we're doing that).
				add_filter( 'wpv_maybe_show_listing_message_undo', 'wpv_admin_view_listing_message_undo', 10, 3 );

				wpv_maybe_show_listing_message(
						'trashed', __( 'WordPress Archive moved to the Trash.', 'wpv-views' ), __( '%d WordPress Archives moved to the Trash.', 'wpv-views' ), true );
				wpv_maybe_show_listing_message(
						'untrashed', __( 'WordPress Archive restored from the Trash.', 'wpv-views' ), __( '%d WordPress Archives restored from the Trash.', 'wpv-views' ) );
				wpv_maybe_show_listing_message(
						'deleted', __( 'WordPress Archive permanently deleted.', 'wpv-views' ), __( '%d WordPress Archives permanently deleted.', 'wpv-views' ) );

				// "Arrange by" tabs
				?>
			
				<div class="wpv-admin-tabs">
					<ul class="wpv-admin-tabs-links">
						<li>
							<a href="<?php echo esc_url( add_query_arg( array( 'page' => 'view-archives' ), admin_url( 'admin.php' ) ) ); ?>"
									<?php wpv_current_class( ! $arrange_by_usage ); ?> >
								<?php _e( 'Arrange by name', 'wpv-views' ); ?>
							</a>
						</li>
						<li>
							<a href="<?php echo esc_url( add_query_arg( array( 'page' => 'view-archives', 'arrangeby' => 'usage' ), admin_url( 'admin.php' ) ) ); ?>"
									<?php wpv_current_class( $arrange_by_usage ); ?> >
								<?php _e( 'Arrange by usage', 'wpv-views' ); ?>
							</a>
						</li>
					</ul>
				</div>

            <?php
				if ( $arrange_by_usage ) {

					// Show table arranged by Usage
					wp_nonce_field( 'wpv_wp_archive_arrange_usage', 'wpv_wp_archive_arrange_usage' );

					if ( !$WPV_view_archive_loop->check_archive_loops_exists() ) {
						?>
						<p id="js-wpv-no-archive" class="toolset-alert toolset-alert-success update below-h2">
							<?php _e('All loops have a WordPress Archive assigned','wpv-views'); ?>
						</p>
						<?php
					}

					wpv_admin_wordpress_archives_listing_table_by_usage();

					?>
					<p class="add-new-view js-add-new-view">
						<a class="button js-wpv-views-archive-add-new wpv-views-archive-add-new" href="#">
							<i class="icon-plus"></i><?php _e('Add new WordPress Archive','wpv-views') ?>
						</a>
					</p>
					<?php

				} else {

					// IDs of possible results and counts per post status.
					$views_pre_query_data = wpv_prepare_view_listing_query( array( 'archive', 'layouts-loop' ), $current_post_status );

					// Do we have any Views at all?
					$has_items = ( $views_pre_query_data['total_count'] > 0 );

					// Show table arranged by item names
					if ( $has_items ) {

						wpv_admin_wordpress_archives_listing_table_by_name( $views_pre_query_data, $current_post_status );

					} else {
						// No items are present.
						
						if ( !$WPV_view_archive_loop->check_archive_loops_exists() ) {
							?>
							<p id="js-wpv-no-archive" class="toolset-alert toolset-alert-success">
								<?php _e('All loops have a WordPress Archive assigned','wpv-views'); ?>
							</p>
							<?php
						}

						?>
						<div class="wpv-view-not-exist js-wpv-view-not-exist">
							<p><?php _e( 'WordPress Archives let you customize the output of standard Archive pages.', 'wpv-views' );?></p>
							<p>
							<a class="button js-wpv-views-archive-create-new" href="#">
								<i class="icon-plus"></i>
								<?php _e( 'Create your first WordPress Archive', 'wpv-views' );?>
							</a>
							</p>
						</div>
						<?php
					}
				}
			?>
        </div> <!-- .wpv-settings-container" -->
	</div>
	<?php
}
/**
* wpv_admin_archive_listing_page
*
* Creates the main structure of the WPA admin listing page: wrapper and header
*
*/
function wpv_admin_archive_listing_page()
{
    global $WPV_view_archive_loop;
    ?>
	<div class="wrap toolset-views">

        <div class="wpv-views-listing-page wpv-views-listing-archive-page" data-none-message="<?php 
    _e("This WordPress Archive isn't being used for any loops.", 'wpv-views');
    ?>
">
		<?php 
    $has_items = wpv_check_views_exists('archive');
    $search_term = isset($_GET["search"]) ? urldecode(sanitize_text_field($_GET["search"])) : '';
    wp_nonce_field('work_views_listing', 'work_views_listing');
    wp_nonce_field('wpv_remove_view_permanent_nonce', 'wpv_remove_view_permanent_nonce');
    ?>
		<div id="icon-views" class="icon32"></div>
        	<h2><!-- classname wpv-page-title removed -->
        		<?php 
    _e('WordPress Archives', 'wpv-views');
    ?>
        		<?php 
    if ($WPV_view_archive_loop->check_archive_loops_exists() && $has_items) {
        ?>
        		<a href="#" data-target="<?php 
        echo admin_url('admin-ajax.php');
        ?>
?action=wpv_create_wp_archive_button" class="add-new-h2 js-wpv-views-archive-add-new wpv-views-archive-add-new">
				<?php 
        _e('Add new WordPress Archive', 'wpv-views');
        ?>
			</a>
			<?php 
    }
    ?>
			<?php 
    if (!empty($search_term)) {
        $search_message = __('Search results for "%s"', 'wpv-views');
        if (isset($_GET["status"]) && 'trash' == sanitize_text_field($_GET["status"])) {
            $search_message = __('Search results for "%s" in trashed WordPress Archives', 'wpv-views');
        }
        ?>
				<span class="subtitle">
					<?php 
        echo sprintf($search_message, $search_term);
        ?>
				</span>
			<?php 
    }
    ?>
        	</h2>
        	
       <?php 
    // Messages: trashed, untrashed, deleted
    if (isset($_GET['trashed']) && is_numeric($_GET['trashed'])) {
        ?>
			<div id="message" class="updated below-h2">
				<p>
					<?php 
        _e('WordPress Archive moved to the Trash', 'wpv-views');
        ?>
. <a href="<?php 
        echo admin_url();
        ?>
admin.php?page=view-archives&amp;untrashed=1" class="js-wpv-untrash" data-id="<?php 
        echo $_GET['trashed'];
        ?>
" data-nonce="<?php 
        echo wp_create_nonce('wpv_view_listing_actions_nonce');
        ?>
"><?php 
        _e('Undo', 'wpv-views');
        ?>
</a>
				</p>
			</div>
		<?php 
    }
    if (isset($_GET['untrashed']) && is_numeric($_GET['untrashed']) && (int) $_GET['untrashed'] == 1) {
        ?>
			<div id="message" class="updated below-h2">
				<p>
					<?php 
        _e('WordPress Archive restored from the Trash', 'wpv-views');
        ?>
				</p>
			</div>
		<?php 
    }
    if (isset($_GET['deleted']) && is_numeric($_GET['deleted']) && (int) $_GET['deleted'] == 1) {
        ?>
			<div id="message" class="updated below-h2">
				<p>
					<?php 
        _e('WordPress Archive permanently deleted', 'wpv-views');
        ?>
				</p>
			</div>
		<?php 
    }
    ?>

            <?php 
    if (isset($_GET["arrangeby"]) && sanitize_text_field($_GET["arrangeby"]) == 'usage') {
        wp_nonce_field('wpv_wp_archive_arrange_usage', 'wpv_wp_archive_arrange_usage');
        ?>
			<?php 
        if (!$WPV_view_archive_loop->check_archive_loops_exists()) {
            ?>
				<p id="js-wpv-no-archive" class="toolset-alert toolset-alert-info update below-h2">
					<?php 
            _e('All loops have a WordPress Archive assigned', 'wpv-views');
            ?>
				</p>
			<?php 
        }
        ?>
			<div class="wpv-views-listing-arrange js-wpv-views-listing-arrange">
				<p><?php 
        _e('Arrange by', 'wpv-views');
        ?>
: </p>
				<ul>
				<li data-sortby="name"><a href="<?php 
        echo admin_url('admin.php');
        ?>
?page=view-archives"><?php 
        _e('Name', 'wpv-views');
        ?>
</a></li>
				<li data-sortby="usage" class="active"><?php 
        _e('Usage', 'wpv-views');
        ?>
</li>
				</ul>
			</div>
			<?php 
        wpv_admin_wordpress_archives_listing_table_by_usage();
        if ($WPV_view_archive_loop->check_archive_loops_exists()) {
            ?>
				<p class="add-new-view js-add-new-view">
					<a class="button js-wpv-views-archive-add-new wpv-views-archive-add-new" data-target="<?php 
            echo admin_url('admin-ajax.php');
            ?>
?action=wpv_create_wp_archive_button" href="admin.php?page=view-archives-new">
						<i class="icon-plus"></i><?php 
            _e('Add new WordPress Archive', 'wpv-views');
            ?>
					</a>
				</p>
			<?php 
        }
    } else {
        if ($has_items) {
            ?>
				<?php 
            wpv_admin_wordpress_archives_listing_table_by_name($has_items);
        } else {
            ?>
				<?php 
            if (!$WPV_view_archive_loop->check_archive_loops_exists()) {
                ?>
					<p id="js-wpv-no-archive" class="toolset-alert toolset-alert-info">
						<?php 
                _e('All loops have a WordPress Archive assigned', 'wpv-views');
                ?>
					</p>
				<?php 
            }
            ?>
				<div class="wpv-view-not-exist js-wpv-view-not-exist">
					<p><?php 
            _e('WordPress Archives let you customize the output of standard Archive pages.');
            ?>
</p>
					<p>
					<a class="button js-wpv-views-archive-create-new" data-target="<?php 
            echo admin_url('admin-ajax.php');
            ?>
?action=wpv_create_wp_archive_button" href="<?php 
            get_admin_url();
            ?>
admin.php?page=view-archives-new">
						<i class="icon-plus"></i>
						<?php 
            _e('Create your first WordPress Archive');
            ?>
					</a>
					</p>
				</div>
			<?php 
        }
    }
    ?>

            

        </div> <!-- .wpv-settings-container" -->


	</div>
<?php 
}