<?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>

<div class="xc-row">
	<div class="xc-col-4">
        <h3 class="titles"><?php esc_html_e( 'Posts', 'xpose-content' ); ?></h3>
        
        <?php
        // Total of Posts
        $total_posts = XC_Queries::get_posts_total();
        ?>
        
        <div class="circle">
            <h3 class="titles"><?php echo esc_html($total_posts);?></h3>
        </div>
        
        <h3 class="titles"><?php esc_html_e( 'Posts by Type', 'xpose-content' ); ?></h3>
        
        <table class="xc-results">
            <thead>
                <tr>
                    <th class="left"><?php esc_html_e( 'Type', 'xpose-content' ); ?> (<?php esc_html_e( 'Words', 'xpose-content' ); ?>)</th>
                    <th><?php esc_html_e( 'Posts', 'xpose-content' ); ?></th>
                    <th>%</th>
                </tr>
            </thead>
            <tbody>
                <?php
                // Posts by Type
				$type_by_posts = XC_Queries::get_posts_by_type();
                if (!empty($type_by_posts)) {
                    foreach ($type_by_posts as $row) {
                        echo "<tr>
                                <td class='leftb'>" . esc_html($row->type) . "</td>
                                <td>" . esc_html($row->posts) . "</td>
                                <td>" . esc_html(round(($row->posts * 100 / $row->total))) . "%</td>
                            </tr>";
                    }
                } else {
                    echo "<tr><td colspan='3'><span class='spans'>" . esc_html__( 'No Results!', 'xpose-content' ) . "</span></td></tr>";
                }
                ?>
            </tbody>
        </table>
        </div>
	<div class="xc-col-4">
        <h3 class="titles"><?php esc_html_e( 'Posts by Month', 'xpose-content' ); ?></h3>
        
        <table class="xc-results">
            <thead>
                <tr>
                    <th class="left"><?php esc_html_e( 'Year', 'xpose-content' ); ?></th>
                    <th class="left"><?php esc_html_e( 'Month', 'xpose-content' ); ?></th>
                    <th><?php esc_html_e( 'Published', 'xpose-content' ); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php
                // Month Names
                $month_names = array(
                    1  => __('January', 'xpose-content'),
                    2  => __('February', 'xpose-content'),
                    3  => __('March', 'xpose-content'),
                    4  => __('April', 'xpose-content'),
                    5  => __('May', 'xpose-content'),
                    6  => __('June', 'xpose-content'),
                    7  => __('July', 'xpose-content'),
                    8  => __('August', 'xpose-content'),
                    9  => __('September', 'xpose-content'),
                    10 => __('October', 'xpose-content'),
                    11 => __('November', 'xpose-content'),
                    12 => __('December', 'xpose-content'),
                );
        
                // Posts by Month
				$month_by_posts = XC_Queries::get_posts_by_month();
                if (!empty($month_by_posts)) {
                    foreach ($month_by_posts as $row) {
                        $month_label = isset($month_names[(int)$row->nmonth]) ? $month_names[(int)$row->nmonth] : '-';
                        echo "<tr>
                                <td class='leftb'>" . esc_html($row->year) . "</td>
                                <td class='left'>" . esc_html($month_label) . "</td>
                                <td>" . esc_html($row->published) . "</td>
                            </tr>";
                    }
                } else {
                    echo "<tr><td colspan='3'><span class='spans'>" . esc_html__( 'No Results!', 'xpose-content' ) . "</span></td></tr>";
                }
                ?>
            </tbody>
        </table>
    </div>
	<div class="xc-col-4">
        <h3 class="titles"><?php esc_html_e( 'Posts by Author', 'xpose-content' ); ?></h3>
        
        <table class="xc-results">
            <thead>
                <tr>
                    <th class="left"><?php esc_html_e( 'User', 'xpose-content' ); ?></th>
                    <th><?php esc_html_e( 'Year', 'xpose-content' ); ?></th>
                    <th class="left"><?php esc_html_e( 'Month', 'xpose-content' ); ?></th>
                    <th><?php esc_html_e( 'Posts', 'xpose-content' ); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php
                $month_names = array(
                    1  => __('January', 'xpose-content'),
                    2  => __('February', 'xpose-content'),
                    3  => __('March', 'xpose-content'),
                    4  => __('April', 'xpose-content'),
                    5  => __('May', 'xpose-content'),
                    6  => __('June', 'xpose-content'),
                    7  => __('July', 'xpose-content'),
                    8  => __('August', 'xpose-content'),
                    9  => __('September', 'xpose-content'),
                    10 => __('October', 'xpose-content'),
                    11 => __('November', 'xpose-content'),
                    12 => __('December', 'xpose-content'),
                );
        
                // Posts by Author
				$author_by_posts = XC_Queries::get_posts_by_author();
                if (!empty($author_by_posts)) {
                    foreach ($author_by_posts as $row) {
                        $month_label = isset($month_names[(int)$row->nmonth]) ? $month_names[(int)$row->nmonth] : '-';
                        echo "<tr>
                                <td class='leftb'>" . esc_html($row->login) . "</td>
                                <td>" . esc_html($row->year) . "</td>
                                <td class='left'>" . esc_html($month_label) . "</td>
                                <td>" . esc_html($row->posts) . "</td>
                            </tr>";
                    }
                } else {
                    echo "<tr><td colspan='4'><span class='spans'>" . esc_html__( 'No Results!', 'xpose-content' ) . "</span></td></tr>";
                }
                ?>
            </tbody>
        </table>
    </div>
</div>

<div class="xc-row">
	<div class="xc-col-7">
        <h3 class="titles"><?php esc_html_e( 'Top Posts', 'xpose-content' ); ?></h3>
        
        <table class="xc-results">
            <thead>
                <tr>
                    <th class="left"><?php esc_html_e( 'Id WP', 'xpose-content' ); ?></th>
                    <th class="left"><?php esc_html_e( 'Title', 'xpose-content' ); ?></th>
                    <th><?php esc_html_e( 'Words', 'xpose-content' ); ?></th>
                    <th><?php esc_html_e( 'Modified', 'xpose-content' ); ?></th>
                    <th><?php esc_html_e( 'Published', 'xpose-content' ); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php
                // Top Posts
				$top_posts = XC_Queries::get_posts_top();
                if (!empty($top_posts)) {
                    foreach ($top_posts as $row) {
                        echo "<tr>
                                <td class='left'>" . esc_html($row->post_id) . "</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>
                            </tr>";
                    }
                } else {
                    echo "<tr><td colspan='5'><span class='spans'>" . esc_html__( 'No Results!', 'xpose-content' ) . "</span></td></tr>";
                }
                ?>
            </tbody>
        </table>
    </div>
    <div class="xc-col-5">
        <h3 class="titles"><?php esc_html_e( 'Posts by Categories', 'xpose-content' ); ?></h3>
        
        <table class="xc-results">
            <thead>
                <tr>
                    <th class="left"><?php esc_html_e( 'Category', 'xpose-content' ); ?></th>
                    <th><?php esc_html_e( 'Posts', 'xpose-content' ); ?></th>
                    <th>%</th>
                    <th><?php esc_html_e( 'Modified', 'xpose-content' ); ?></th>
                    <th><?php esc_html_e( 'Published', 'xpose-content' ); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php
                // Posts by Categories
				$categories_by_posts = XC_Queries::get_posts_by_categories();
                if (!empty($categories_by_posts)) {
                    foreach ($categories_by_posts as $row) {
                        echo "<tr>
                                <td class='leftb'>" . esc_html($row->category) . "</td>
                                <td>" . esc_html($row->posts) . "</td>
                                <td>" . esc_html(round(($row->posts * 100 / $row->total))) . "%</td>
                                <td>" . esc_html(date("d/m/Y", strtotime($row->modified))) . "</td>
                                <td>" . esc_html(date("d/m/Y", strtotime($row->published))) . "</td>
                            </tr>";
                    }
                } else {
                    echo "<tr><td colspan='5'><span class='spans'>" . esc_html__( 'No Results!', 'xpose-content' ) . "</span></td></tr>";
                }
                ?>
            </tbody>
        </table>
    </div>
</div>

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