<?php
// Exit if Accessed Directly
if ( ! defined( 'ABSPATH' ) ) exit;

// Classes Require
require_once dirname( plugin_dir_path( __FILE__ ) ) . '/includes/xc-class-queries.php';
?>

<div class="xc-dash">
<h2 class="titles"><a href="<?php echo esc_url(site_url());?>/wp-admin/admin.php?page=xc-dashboard" title="Xpose Content" class="links">Xpose Content</a></h2>

<?php
// Pagination
$page_no = isset($_GET['page_no']) && $_GET['page_no'] !== "" ? intval(sanitize_text_field(wp_unslash($_GET['page_no']))) : 1;
$total_records_per_page = 20;
$offset = ($page_no - 1) * $total_records_per_page;
$previous_page = $page_no - 1;
$next_page     = $page_no + 1;
$adjacents     = 2;

// Total of Custom Posts
$total_custom_posts = XC_Queries::get_custom_posts_total();

// Number of Pages
$total_no_of_pages = ceil($total_custom_posts / $total_records_per_page);
$second_last = $total_no_of_pages - 1;
?>

<h2 class="titles"><?php esc_html_e( 'Custom Posts', 'xpose-content' ); ?></h2>

<div class="xc-table-wrapper">
    <table class="xc-results xc-results-lists">
		<?php
        $allowed_columns = array('post_words', 'post_modified', 'post_title', 'post_date', 'post_id', 'post_type', 'post_name', 'custom_category');
        $column_input = isset($_GET['column']) ? sanitize_text_field(wp_unslash($_GET['column'])) : '';
        $column = in_array($column_input, $allowed_columns) ? $column_input : 'post_words';
        
        $order_input = isset($_GET['order']) ? sanitize_text_field(wp_unslash($_GET['order'])) : '';
        $sort_order = strtolower($order_input) === 'asc' ? 'ASC' : 'DESC';
        
        // Custom Posts List
		$list_custom_posts = XC_Queries::get_custom_posts_list($column, $sort_order, $offset, $total_records_per_page);
        $rows = count($list_custom_posts);
		
        $up_or_down = str_replace(array('ASC', 'DESC'), array('up', 'down'), $sort_order);
        $asc_or_desc = $sort_order === 'ASC' ? 'desc' : 'asc';
        ?>
        <thead>
            <tr>
                <th class="left"><a href="<?php echo esc_url(site_url());?>/wp-admin/admin.php?page=xc-custom&column=post_id&order=<?php echo esc_html($asc_or_desc); ?>&page_no=1" class="links"><?php esc_html_e( 'Id WP', 'xpose-content' ); ?> <i class="fas fa-sort<?php echo $column == 'post_id' ? '-' . esc_html($up_or_down) : ''; ?>"></i></a></th>
                <th class="left"><a href="<?php echo esc_url(site_url());?>/wp-admin/admin.php?page=xc-custom&column=post_type&order=<?php echo esc_html($asc_or_desc); ?>&page_no=1" class="links"><?php esc_html_e( 'Type', 'xpose-content' ); ?> <i class="fas fa-sort<?php echo $column == 'post_type' ? '-' . esc_html($up_or_down) : ''; ?>"></i></a></th>
                <th class="left"><a href="<?php echo esc_url(site_url());?>/wp-admin/admin.php?page=xc-custom&column=post_title&order=<?php echo esc_html($asc_or_desc); ?>&page_no=1" class="links"><?php esc_html_e( 'Title', 'xpose-content' ); ?> <i class="fas fa-sort<?php echo $column == 'post_title' ? '-' . esc_html($up_or_down) : ''; ?>"></i></a></th>
                <th><a href="<?php echo esc_url(site_url());?>/wp-admin/admin.php?page=xc-custom&column=post_words&order=<?php echo esc_html($asc_or_desc); ?>&page_no=1" class="links"><?php esc_html_e( 'Words', 'xpose-content' ); ?> <i class="fas fa-sort<?php echo $column == 'post_words' ? '-' . esc_html($up_or_down) : ''; ?>"></i></a></th>
                <th><a href="<?php echo esc_url(site_url());?>/wp-admin/admin.php?page=xc-custom&column=post_modified&order=<?php echo esc_html($asc_or_desc); ?>&page_no=1" class="links"><?php esc_html_e( 'Modified', 'xpose-content' ); ?> <i class="fas fa-sort<?php echo $column == 'post_modified' ? '-' . esc_html($up_or_down) : ''; ?>"></i></a></th>
                <th><a href="<?php echo esc_url(site_url());?>/wp-admin/admin.php?page=xc-custom&column=post_date&order=<?php echo esc_html($asc_or_desc); ?>&page_no=1" class="links"><?php esc_html_e( 'Published', 'xpose-content' ); ?> <i class="fas fa-sort<?php echo $column == 'post_date' ? '-' . esc_html($up_or_down) : ''; ?>"></i></a></th>
                <th><a href="<?php echo esc_url(site_url());?>/wp-admin/admin.php?page=xc-custom&column=custom_category&order=<?php echo esc_html($asc_or_desc); ?>&page_no=1" class="links"><?php esc_html_e( 'Category', 'xpose-content' ); ?> <i class="fas fa-sort<?php echo $column == 'custom_category' ? '-' . esc_html($up_or_down) : ''; ?>"></i></a></th>
                <th class="left"><a href="<?php echo esc_url(site_url());?>/wp-admin/admin.php?page=xc-custom&column=post_name&order=<?php echo esc_html($asc_or_desc); ?>&page_no=1" class="links"><?php esc_html_e( 'URL', 'xpose-content' ); ?> <i class="fas fa-sort<?php echo $column == 'post_name' ? '-' . esc_html($up_or_down) : ''; ?>"></i></a></th>
            </tr>
        </thead>
        <tbody>
            <?php
            if ($rows > 0){
                foreach($list_custom_posts as $row){
                    echo "<tr>
                          <td class='left'>".esc_html($row->post_id)."</td>
                          <td class='left'>".esc_html($row->post_type)."</td>
                          <td class='left'>".esc_html($row->post_title)."</td>
                          <td>".esc_html($row->post_words)."</td>
                          <td>".esc_html(date("d/m/Y", strtotime($row->post_modified)))."</td>
                          <td>".esc_html(date("d/m/Y", strtotime($row->post_date)))."</td>
                          <td>".esc_html($row->custom_category)."</td>
                          <td class='left'>".esc_html($row->post_name)."</td>
                          </tr>";
                }
            }
            else{
                echo "<tr><td colspan='8'><span class='spans'>".esc_html__( 'No Results!', 'xpose-content' )."</span></td></tr>";
            }
            ?>
        </tbody>
    </table>
</div>

<div class="xc-row">
	<div class="xc-col-12">
    	<?php
		// Button For Export to CSV
        if ($rows > 0){
			echo '<a href="'.esc_url(site_url()).'/wp-admin/admin.php?page=xc-custom&export_custom=all" title="Export to CSV" target="_blank" class="xc-button-export">'.esc_html__( 'Export to CSV', 'xpose-content' ).'</a>';
		}
		?>
        
        <p class="paragraphs"><?php esc_html_e( 'Total', 'xpose-content' ); ?>: <span class="spans"><?php echo esc_html($total_custom_posts);?> <?php esc_html_e( 'Results', 'xpose-content' ); ?></span> | <?php esc_html_e( 'Page', 'xpose-content' ); ?>: <?php echo esc_html($page_no)." / ".esc_html($total_no_of_pages); ?></p>

        <ul class="pagination">
            <?php if($page_no > 1){ echo "<li><a href='".esc_url(site_url())."/wp-admin/admin.php?page=xc-custom&column=".esc_html($column)."&order=".esc_html($sort_order)."&page_no=1' class='links'>&laquo;&laquo; ".esc_html__( 'First', 'xpose-content' )."</a></li>"; } ?>
            <li <?php if($page_no <= 1){ echo "class='disabled'"; } ?>>
            <a <?php if($page_no > 1){ echo "href='".esc_url(site_url())."/wp-admin/admin.php?page=xc-custom&column=".esc_html($column)."&order=".esc_html($sort_order)."&page_no=".esc_html($previous_page)."'"; } ?> class="links">&laquo; <?php esc_html_e( 'Previous', 'xpose-content' ); ?></a>
            </li>
               
            <?php 
            if ($total_no_of_pages <= 10) {  	 
                for ($counter = 1; $counter <= $total_no_of_pages; $counter++) {
                    if ($counter == $page_no) {
                   echo "<li class='active'><a class='links'>".esc_html($counter)."</a></li>";	
                        }else{
                   echo "<li><a href='".esc_url(site_url())."/wp-admin/admin.php?page=xc-custom&column=".esc_html($column)."&order=".esc_html($sort_order)."&page_no=".esc_html($counter)."' class='links'>".esc_html($counter)."</a></li>";
                        }
                }
            }
            elseif($total_no_of_pages > 10) {
                if($page_no <= 4) {
                    for ($counter = 1; $counter < 8; $counter++) {
                        if ($counter == $page_no) {
                            echo "<li class='active'><a class='links'>".esc_html($counter)."</a></li>";
                        }
                        else{
                            echo "<li><a href='".esc_url(site_url())."/wp-admin/admin.php?page=xc-custom&column=".esc_html($column)."&order=".esc_html($sort_order)."&page_no=".esc_html($counter)."' class='links'>".esc_html($counter)."</a></li>";
                        }
                }
                echo "<li><a class='links'>...</a></li>";
                echo "<li><a href='".esc_url(site_url())."/wp-admin/admin.php?page=xc-custom&column=".esc_html($column)."&order=".esc_html($sort_order)."&page_no=".esc_html($second_last)."' class='links'>".esc_html($second_last)."</a></li>";
                echo "<li><a href='".esc_url(site_url())."/wp-admin/admin.php?page=xc-custom&column=".esc_html($column)."&order=".esc_html($sort_order)."&page_no=".esc_html($total_no_of_pages)."' class='links'>".esc_html($total_no_of_pages)."</a></li>";
                }
                elseif($page_no > 4 && $page_no < $total_no_of_pages - 4) {
                    echo "<li><a href='".esc_url(site_url())."/wp-admin/admin.php?page=xc-custom&column=".esc_html($column)."&order=".esc_html($sort_order)."&page_no=1' class='links'>1</a></li>";
                    echo "<li><a href='".esc_url(site_url())."/wp-admin/admin.php?page=xc-custom&column=".esc_html($column)."&order=".esc_html($sort_order)."&page_no=2' class='links'>2</a></li>";
                    echo "<li><a class='links'>...</a></li>";
                    for ($counter = $page_no - $adjacents; $counter <= $page_no + $adjacents; $counter++) {
                        if ($counter == $page_no) {
                            echo "<li class='active'><a class='links'>".esc_html($counter)."</a></li>";
                        }
                        else{
                            echo "<li><a href='".esc_url(site_url())."/wp-admin/admin.php?page=xc-custom&column=".esc_html($column)."&order=".esc_html($sort_order)."&page_no=".esc_html($counter)."' class='links'>".esc_html($counter)."</a></li>";
                        }                  
               }
               echo "<li><a class='links'>...</a></li>";
               echo "<li><a href='".esc_url(site_url())."/wp-admin/admin.php?page=xc-custom&column=".esc_html($column)."&order=".esc_html($sort_order)."&page_no=".esc_html($second_last)."' class='links'>".esc_html($second_last)."</a></li>";
               echo "<li><a href='".esc_url(site_url())."/wp-admin/admin.php?page=xc-custom&column=".esc_html($column)."&order=".esc_html($sort_order)."&page_no=".esc_html($total_no_of_pages)."' class='links'>".esc_html($total_no_of_pages)."</a></li>";
                }
                else {
                    echo "<li><a href='".esc_url(site_url())."/wp-admin/admin.php?page=xc-custom&column=".esc_html($column)."&order=".esc_html($sort_order)."&page_no=1' class='links'>1</a></li>";
                    echo "<li><a href='".esc_url(site_url())."/wp-admin/admin.php?page=xc-custom&column=".esc_html($column)."&order=".esc_html($sort_order)."&page_no=2' class='links'>2</a></li>";
                    echo "<li><a class='links'>...</a></li>";
                    for ($counter = $total_no_of_pages - 6; $counter <= $total_no_of_pages; $counter++) {
                        if ($counter == $page_no) {
                            echo "<li class='active'><a class='links'>".esc_html($counter)."</a></li>";
                        }
                        else{
                            echo "<li><a href='".esc_url(site_url())."/wp-admin/admin.php?page=xc-custom&column=".esc_html($column)."&order=".esc_html($sort_order)."&page_no=".esc_html($counter)."' class='links'>".esc_html($counter)."</a></li>";
                        }
                    }
                }
            }
            ?>
            <li <?php if($page_no >= $total_no_of_pages){ echo "class='disabled'"; } ?>>
            <a <?php if($page_no < $total_no_of_pages) { echo "href='".esc_url(site_url())."/wp-admin/admin.php?page=xc-custom&column=".esc_html($column)."&order=".esc_html($sort_order)."&page_no=".esc_html($next_page)."'"; } ?> class="links"><?php esc_html_e( 'Next', 'xpose-content' ); ?> &raquo;</a>
            </li>
            <?php if($page_no < $total_no_of_pages){ echo "<li><a href='".esc_url(site_url())."/wp-admin/admin.php?page=xc-custom&column=".esc_html($column)."&order=".esc_html($sort_order)."&page_no=".esc_html($total_no_of_pages)."' class='links'>".esc_html__( 'Last', 'xpose-content' )." &raquo;&raquo;</a></li>";} ?>    
        </ul>
    </div>
</div>

<?php
// Credits
require_once dirname( plugin_dir_path( __FILE__ ) ) . '/includes/footer-credits.php';
?>
</div>