<?php
/**
 * Top images page for PinnerPress
 * This file displays top performing images by combined pin button and image banner clicks
 */

// If this file is called directly, abort.
if (!defined('WPINC')) {
    die;
}

// Check if analytics is enabled
$analytics_enabled = get_option('pinnerpress_analytics_enabled', '1') === '1';

if (!$analytics_enabled) {
    ?>
    <div class="wrap pinnerpress-admin">
        <h1><?php echo esc_html__('PinnerPress Top Images', 'pinnerpress'); ?></h1>

        <div class="pinnerpress-admin-header">
            <div class="pinnerpress-logo">
                <?php // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedImage -- Plugin logo asset ?>
                <img src="<?php echo esc_url(plugin_dir_url(dirname(__FILE__)) . 'images/pinnerpress-logo.png'); ?>" alt="<?php echo esc_attr__('PinnerPress Logo', 'pinnerpress'); ?>">
            </div>
            <div class="pinnerpress-header-content">
                <h2><?php esc_html_e('PinnerPress Top Images', 'pinnerpress'); ?></h2>
                <p><?php esc_html_e('View your top performing images by total Pinterest engagement (pin button + image banner clicks).', 'pinnerpress'); ?></p>
            </div>
        </div>

        <div class="pinnerpress-analytics-disabled-notice" style="background: #fff; border: 1px solid #c3c4c7; border-radius: 4px; padding: 60px 40px; text-align: center; margin: 20px auto 0; max-width: 800px;">
            <span class="dashicons dashicons-chart-bar" style="font-size: 48px; width: 48px; height: 48px; color: #c3c4c7; margin-bottom: 20px;"></span>
            <h2 style="font-size: 24px; margin: 0 0 15px 0; color: #1d2327;"><?php esc_html_e('Analytics Disabled', 'pinnerpress'); ?></h2>
            <p style="font-size: 14px; color: #646970; margin: 0 0 25px 0; max-width: 500px; margin-left: auto; margin-right: auto;">
                <?php esc_html_e('Analytics tracking is currently turned off. Enable analytics to start collecting data about your Pinterest engagement.', 'pinnerpress'); ?>
            </p>
            <a href="<?php echo esc_url(admin_url('admin.php?page=pinnerpress-settings&tab=general#analytics-settings')); ?>" class="button button-primary button-hero">
                <?php esc_html_e('Enable Analytics in Settings', 'pinnerpress'); ?>
            </a>
        </div>
    </div>
    <?php
    return;
}

// Check if user has premium access
$is_premium = function_exists('pinnerpress_is_premium') ? pinnerpress_is_premium() : false;

// Check if user can use premium features (includes trial users)
$can_use_premium_features = function_exists('pinnerpress_can_use_premium_features') ? pinnerpress_can_use_premium_features() : false;

// Set limits based on premium status
$image_limit = $is_premium ? 300 : 50;
$per_page = 25;

?>

<div class="wrap pinnerpress-admin pinnerpress-top-images-page">
    <h1><?php echo esc_html(get_admin_page_title()); ?></h1>

    <div class="pinnerpress-admin-header">
        <div class="pinnerpress-logo">
            <?php // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedImage -- Plugin logo asset ?>
            <img src="<?php echo esc_url(plugin_dir_url(dirname(__FILE__)) . 'images/pinnerpress-logo.png'); ?>" alt="<?php echo esc_attr__('PinnerPress Logo', 'pinnerpress'); ?>">
        </div>
        <div class="pinnerpress-header-content">
            <h2><?php esc_html_e('PinnerPress Top Images', 'pinnerpress'); ?></h2>
            <p><?php esc_html_e('View your top performing images by total Pinterest engagement (pin button + image banner clicks).', 'pinnerpress'); ?></p>
        </div>
    </div>

    <div class="pinnerpress-admin-content">
        <?php if (!$can_use_premium_features) : ?>
            <div class="pinnerpress-analytics-limitation">
                <h4><?php esc_html_e('Free Plan Limitations', 'pinnerpress'); ?></h4>
                <p><?php esc_html_e('You are viewing the top 50 images. Upgrade to Premium to view the top 300 images!', 'pinnerpress'); ?></p>
                <a href="<?php echo esc_url(function_exists('pinnerpress_get_upgrade_url') ? pinnerpress_get_upgrade_url() : admin_url('admin.php?page=pinnerpress-pricing')); ?>" class="button button-primary" target="_blank"><?php esc_html_e('Upgrade to Premium', 'pinnerpress'); ?></a>
            </div>
        <?php endif; ?>

        <!-- Info and Controls Section -->
        <div class="pinnerpress-top-images-controls">
            <div class="pinnerpress-top-images-info">
                <p>
                    <?php
                    echo esc_html(sprintf(
                        /* translators: %d: number of images being displayed */
                        __('Showing top %d images', 'pinnerpress'),
                        $image_limit
                    ));
                    ?>
                </p>
            </div>
            <div class="pinnerpress-controls-buttons">
                <div class="view-toggle">
                    <button type="button" class="view-toggle-btn" data-view="table" aria-pressed="true">
                        <span class="dashicons dashicons-list-view"></span>
                        <?php esc_html_e('Table', 'pinnerpress'); ?>
                    </button>
                    <button type="button" class="view-toggle-btn" data-view="grid" aria-pressed="false">
                        <span class="dashicons dashicons-screenoptions"></span>
                        <?php esc_html_e('Grid', 'pinnerpress'); ?>
                    </button>
                </div>
                <button type="button" id="export-csv" class="button pinnerpress-export-button" <?php if (!$can_use_premium_features) echo 'disabled'; ?>>
                    <?php esc_html_e('Export CSV', 'pinnerpress'); ?>
                    <?php if (!$can_use_premium_features): ?>
                        <span class="pinnerpress-premium-badge">PRO</span>
                    <?php endif; ?>
                </button>
            </div>
        </div>

        <!-- Top Images Container -->
        <div class="top-images-container">

            <!-- Table View -->
            <div class="image-table-container" style="display:block;">
                <table class="wp-list-table widefat fixed striped">
                    <thead>
                        <tr>
                            <th class="column-rank" style="width: 60px;"><?php esc_html_e('Rank', 'pinnerpress'); ?></th>
                            <th class="column-preview" style="width: 80px;"><?php esc_html_e('Image', 'pinnerpress'); ?></th>
                            <th class="column-post-title"><?php esc_html_e('Post Title', 'pinnerpress'); ?></th>
                            <?php /* DISABLED: Impressions tracking removed
                            <th class="column-impressions sortable" data-sort="impressions" style="width: 100px;">
                                <a href="#">
                                    <?php esc_html_e('Impressions', 'pinnerpress'); ?>
                                    <span class="sorting-indicator"></span>
                                </a>
                            </th>
                            */ ?>
                            <th class="column-pin-clicks sortable" data-sort="pin-clicks" style="width: 75px;">
                                <a href="#">
                                    <?php esc_html_e('Pin Clicks', 'pinnerpress'); ?>
                                    <span class="sorting-indicator"></span>
                                </a>
                            </th>
                            <?php /* DISABLED: Pin CTR requires impressions
                            <th class="column-pin-ctr sortable" data-sort="pin-ctr" style="width: 80px;">
                                <a href="#">
                                    <?php esc_html_e('Pin CTR', 'pinnerpress'); ?>
                                    <span class="sorting-indicator"></span>
                                </a>
                            </th>
                            */ ?>
                            <th class="column-banner-clicks sortable" data-sort="banner-clicks" style="width: 75px;">
                                <a href="#">
                                    <?php esc_html_e('Banner Clicks', 'pinnerpress'); ?>
                                    <span class="sorting-indicator"></span>
                                </a>
                            </th>
                            <?php /* DISABLED: Banner CTR requires impressions
                            <th class="column-banner-ctr sortable" data-sort="banner-ctr" style="width: 90px;">
                                <a href="#">
                                    <?php esc_html_e('Banner CTR', 'pinnerpress'); ?>
                                    <span class="sorting-indicator"></span>
                                </a>
                            </th>
                            */ ?>
                            <th class="column-total-clicks sortable desc" data-sort="total-clicks" style="width: 75px;">
                                <a href="#">
                                    <?php esc_html_e('Total Clicks', 'pinnerpress'); ?>
                                    <span class="sorting-indicator"></span>
                                </a>
                            </th>
                            <?php /* DISABLED: Conversion rate requires impressions
                            <th class="column-conversion sortable" data-sort="conversion" style="width: 100px;">
                                <a href="#" title="<?php esc_attr_e('Overall conversion rate for all clicks', 'pinnerpress'); ?>">
                                    <?php esc_html_e('Conversion', 'pinnerpress'); ?>
                                    <span class="sorting-indicator"></span>
                                </a>
                            </th>
                            */ ?>
                            <th class="column-actions" style="width: 140px;"><?php esc_html_e('Actions', 'pinnerpress'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <!-- Dynamically populated -->
                    </tbody>
                </table>
            </div>

            <!-- Grid View -->
            <div class="image-grid-container" style="display:none;">
                <!-- Dynamically populated -->
            </div>

            <!-- Pagination -->
            <div class="tablenav bottom">
                <div class="tablenav-pages">
                    <span class="displaying-num" id="displaying-num"></span>
                    <span class="pagination-links">
                        <button class="button first-page" id="first-page" disabled>
                            <span aria-hidden="true">&laquo;</span>
                        </button>
                        <button class="button prev-page" id="prev-page" disabled>
                            <span aria-hidden="true">&lsaquo;</span>
                        </button>
                        <span class="paging-input">
                            <span id="current-page-display">1</span> of <span id="total-pages-display">1</span>
                        </span>
                        <button class="button next-page" id="next-page" disabled>
                            <span aria-hidden="true">&rsaquo;</span>
                        </button>
                        <button class="button last-page" id="last-page" disabled>
                            <span aria-hidden="true">&raquo;</span>
                        </button>
                    </span>
                </div>
            </div>
        </div>

        <!-- Loading Message -->
        <div class="loading-message" style="display:none;">
            <p><?php esc_html_e('Loading top images...', 'pinnerpress'); ?></p>
        </div>

        <!-- No Data Message -->
        <div class="no-data-message" style="display:none;">
            <p><?php esc_html_e('No image analytics data available yet.', 'pinnerpress'); ?></p>
        </div>
    </div>

    <!-- Image Detail Modal -->
    <div class="image-detail-modal" style="display: none;">
        <div class="modal-content">
            <span class="close">&times;</span>
            <div class="modal-body">
                <!-- Content will be dynamically inserted here -->
            </div>
        </div>
    </div>
</div>

<!-- Add nonce field -->
<?php wp_nonce_field('pinnerpress_analytics_nonce', 'pinnerpress_analytics_nonce'); ?>

<style>
/* Top Images Page Styles - Matching Post Analytics */
.pinnerpress-top-images-page {
    /* Top images specific styles */
}

/* Header styles are inherited from main admin CSS */

/* Ensure admin content is block layout */
.pinnerpress-admin-content {
    display: block !important;
    width: 100% !important;
    float: none !important;
}

/* Combined Info and Controls Section */
.pinnerpress-top-images-controls {
    background: #fff;
    border: 1px solid #ccd0d4;
    border-radius: 4px;
    padding: 15px 20px;
    margin-bottom: 20px;
    width: 100% !important;
    display: flex !important;
    justify-content: space-between;
    align-items: center;
    gap: 15px;
    clear: both !important;
    float: none !important;
    box-sizing: border-box !important;
}

/* Info text on the left */
.pinnerpress-top-images-info {
    display: flex;
    align-items: center;
}

.pinnerpress-top-images-info p {
    margin: 0;
    font-size: 14px;
    color: #666;
}

/* Buttons container on the right */
.pinnerpress-controls-buttons {
    display: flex;
    align-items: center;
    gap: 15px;
}

.view-toggle {
    display: inline-flex;
    gap: 5px;
    border: 1px solid #ccd0d4;
    border-radius: 4px;
    overflow: hidden;
}

.view-toggle-btn {
    background: #fff;
    border: none;
    padding: 8px 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 5px;
    transition: all 0.2s ease;
}

.view-toggle-btn:hover {
    background: #f0f0f0;
}

.view-toggle-btn[aria-pressed="true"] {
    background: #E60023;
    color: #fff;
}

.view-toggle-btn .dashicons {
    font-size: 16px;
    width: 16px;
    height: 16px;
}

.pinnerpress-analytics-limitation {
    border-left: 4px solid #E60023;
    padding: 12px;
    background-color: #fef4f4;
    margin: 20px 0;
}

.pinnerpress-analytics-limitation h4 {
    margin-top: 0;
    color: #E60023;
}

.pinnerpress-analytics-limitation p {
    margin-bottom: 10px;
}

/* Table Container */
.top-images-container {
    background: #fff;
    border: 1px solid #ccd0d4;
    border-radius: 4px;
    padding: 0;
    margin-top: 0;
    width: 100% !important;
    display: block !important;
    clear: both !important;
    float: none !important;
    box-sizing: border-box !important;
}

/* Table Styles - Matching Post Analytics */
.image-table-container {
    background: #fff;
    border-radius: 4px;
    padding: 20px;
    margin-top: 0;
    overflow-x: auto;
}

.image-table-container table {
    background: #fff;
    border: none;
    border-spacing: 0;
    table-layout: fixed;
    width: 100%;
}

.image-table-container th {
    background: #fff;
    border-bottom: 1px solid #ccd0d4;
    font-weight: 600;
    color: #23282d;
    text-align: center;
    vertical-align: middle;
    padding: 12px 8px;
    white-space: normal;
    word-wrap: break-word;
    line-height: 1.2;
}

.image-table-container th.sortable {
    cursor: pointer;
}

/* Remove hover background on table headers */
.image-table-container th.sortable:hover {
    background: #fff;
}

.image-table-container th.sortable a {
    text-decoration: none;
    color: inherit;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    padding: 4px 2px;
    line-height: 1.2;
    flex-direction: row-reverse;
    text-align: center;
    height: 100%;
    min-height: 40px;
}

/* Special alignment for title column */
.image-table-container th.column-post-title,
.image-table-container td.column-post-title {
    text-align: left !important;
}

.image-table-container th.column-post-title a {
    justify-content: flex-start;
    padding-left: 4px;
    text-align: left;
    flex-direction: row;
}

/* Ensure title column content is left-aligned */
.image-table-container .column-post-title {
    text-align: left !important;
    vertical-align: middle;
}

.image-table-container .column-preview img {
    width: 60px;
    height: 60px;
    object-fit: cover;
    border-radius: 4px;
}

.image-table-container .column-rank,
.image-table-container .column-impressions,
.image-table-container .column-pin-clicks,
.image-table-container .column-pin-ctr,
.image-table-container .column-banner-clicks,
.image-table-container .column-banner-ctr,
.image-table-container .column-total-clicks,
.image-table-container .column-conversion,
.image-table-container .column-actions {
    text-align: center;
    vertical-align: middle;
}

/* Additional table styling */
.image-table-container td {
    padding: 12px 8px;
    border-bottom: 1px solid #e1e1e1;
    vertical-align: middle;
}

.image-table-container tbody tr {
    cursor: pointer;
    transition: background-color 0.2s ease;
}

.image-table-container tbody tr:hover {
    background: #f6f7f7;
}

.sorting-indicator {
    display: inline-block;
    width: 0;
    height: 0;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    margin-left: auto;
}

.image-table-container th.asc .sorting-indicator {
    border-bottom: 4px solid #E60023;
}

.image-table-container th.desc .sorting-indicator {
    border-top: 4px solid #E60023;
}

/* Action buttons styling - icon buttons */
.image-table-container .column-actions {
    white-space: nowrap;
}

.image-table-container .column-actions .button {
    padding: 6px 8px;
    margin-right: 4px;
    min-width: auto;
    font-size: 12px;
    height: auto;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
}

.image-table-container .column-actions .button:last-child {
    margin-right: 0;
}

.image-table-container .column-actions .button .dashicons {
    font-size: 18px;
    width: 18px;
    height: 18px;
    margin: 0;
    color: #E60023;
}

.image-table-container .column-actions .button:hover {
    background: rgba(230, 0, 35, 0.1);
    border-radius: 4px;
}

.image-table-container .column-actions .button:hover .dashicons {
    color: #bd051f;
}

/* Remove focus outlines specifically for action buttons */
.image-table-container .column-actions .button:focus {
    outline: none !important;
    box-shadow: none !important;
}

/* CTR and conversion styling */
.image-table-container .column-pin-ctr,
.image-table-container .column-banner-ctr,
.image-table-container .column-conversion {
    font-weight: 500;
}

/* Conversion rate pill/badge styling - matching Image Analytics */
.image-table-container .conversion-rate {
    padding: 2px 8px;
    border-radius: 12px;
    font-weight: 600;
    font-size: 12px;
    display: inline-block;
}

.image-table-container .conversion-rate.excellent {
    background-color: rgba(76, 175, 80, 0.1);
    color: #4CAF50;
}

.image-table-container .conversion-rate.good {
    background-color: rgba(255, 152, 0, 0.1);
    color: #FF9800;
}

.image-table-container .conversion-rate.fair {
    background-color: rgba(255, 193, 7, 0.1);
    color: #FFC107;
}

.image-table-container .conversion-rate.poor {
    background-color: rgba(230, 0, 35, 0.1);
    color: #E60023;
}

/* Modal Styles */
.image-detail-modal {
    position: fixed;
    z-index: 999999;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.6);
    overflow-y: auto;
}

.image-detail-modal .modal-content {
    background-color: #fefefe;
    margin: 50px auto;
    padding: 0;
    border: 1px solid #888;
    border-radius: 8px;
    width: 90%;
    max-width: 1200px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.3);
}

.image-detail-modal .close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
    padding: 10px 20px;
    cursor: pointer;
}

.image-detail-modal .close:hover,
.image-detail-modal .close:focus {
    color: #000;
}

.image-detail-modal .modal-body {
    padding: 30px;
}

/* Loading/No Data Messages */
.loading-message,
.no-data-message {
    text-align: center;
    padding: 40px;
    background: #fff;
    border: 1px solid #ccd0d4;
    border-radius: 4px;
    margin-top: 20px;
}

.loading-message .spinner {
    float: none;
    margin: 0 auto 10px;
}

.no-data-message {
    color: #666;
}

/* Pagination Styles */
.tablenav {
    padding: 20px;
    border-top: 1px solid #ccd0d4;
}

.tablenav-pages {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.displaying-num {
    color: #666;
}

.pagination-links {
    display: flex;
    align-items: center;
    gap: 5px;
}

.pagination-links .button {
    padding: 4px 8px;
    min-width: 30px;
    height: 30px;
    line-height: 1;
}

.pagination-links .button:disabled {
    opacity: 0.5;
    cursor: default;
}

.paging-input {
    display: flex;
    align-items: center;
    gap: 5px;
    margin: 0 10px;
}

/* Export button styling - matching Image Analytics */
.pinnerpress-export-button,
#export-csv {
    background-color: #ffffff !important;
    color: #E60023 !important;
    border: 1px solid #E60023 !important;
    padding: 6px 16px !important;
    font-size: 14px !important;
    font-weight: 500 !important;
    border-radius: 4px !important;
    cursor: pointer;
    transition: all 0.3s ease;
    text-shadow: none !important;
    box-shadow: none !important;
    height: auto !important;
    line-height: 1.5 !important;
}

.pinnerpress-export-button:hover:not(:disabled),
#export-csv:hover:not(:disabled) {
    background-color: #E60023 !important;
    color: #ffffff !important;
    border-color: #E60023 !important;
}

.pinnerpress-export-button:disabled,
#export-csv:disabled {
    background-color: #f1f1f1 !important;
    color: #cccccc !important;
    border-color: #cccccc !important;
    cursor: not-allowed !important;
    opacity: 0.6;
}

.pinnerpress-premium-badge {
    display: inline-block;
    background: #E60023;
    color: white;
    padding: 2px 6px;
    border-radius: 3px;
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    margin-left: 5px;
}

/* Grid View Styles */
.image-grid-container {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 20px;
    padding: 20px;
}

.image-grid-card {
    position: relative;
    background: #fff;
    border: 1px solid #ccd0d4;
    border-radius: 8px;
    overflow: hidden;
    transition: transform 0.2s, box-shadow 0.2s;
}

.image-grid-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.image-grid-card .rank-badge {
    position: absolute;
    top: 10px;
    left: 10px;
    background: #E60023;
    color: white;
    padding: 4px 10px;
    border-radius: 4px;
    font-weight: 600;
    font-size: 14px;
    z-index: 1;
}

.image-grid-card .image-preview {
    width: 100%;
    height: 200px;
    overflow: hidden;
}

.image-grid-card .image-preview img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.image-grid-card .card-body {
    padding: 15px;
}

.image-grid-card .post-title {
    font-size: 13px;
    color: #666;
    margin: 0 0 10px 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.image-grid-card .total-clicks {
    font-size: 28px;
    font-weight: 600;
    color: #E60023;
    margin: 0;
    line-height: 1.2;
}

.image-grid-card .total-clicks-label {
    font-size: 11px;
    color: #666;
    text-transform: uppercase;
    margin: 0 0 10px 0;
}

.image-grid-card .click-breakdown {
    display: flex;
    justify-content: space-between;
    padding-top: 10px;
    border-top: 1px solid #e1e1e1;
    font-size: 13px;
    color: #666;
}

.image-grid-card .click-breakdown span {
    display: flex;
    align-items: center;
    gap: 4px;
}

.image-grid-card .click-breakdown strong {
    color: #23282d;
}

@media (max-width: 782px) {
    .pinnerpress-top-images-controls {
        flex-direction: column;
        align-items: stretch;
    }

    .pinnerpress-top-images-info {
        width: 100%;
        justify-content: center;
        margin-bottom: 10px;
    }

    .pinnerpress-controls-buttons {
        flex-direction: column;
        width: 100%;
    }

    .view-toggle {
        width: 100%;
    }

    .view-toggle-btn {
        flex: 1;
        justify-content: center;
    }

    .pinnerpress-export-button,
    #export-csv {
        width: 100%;
    }

    .tablenav-pages {
        flex-direction: column;
        gap: 15px;
    }
}
</style>

<script>
// Localize variables for JavaScript
var pinnerpressVars = {
    ajaxurl: '<?php echo esc_js(admin_url('admin-ajax.php')); ?>',
    nonce: '<?php echo esc_js(wp_create_nonce('pinnerpress_analytics_nonce')); ?>',
    debug_enabled: <?php echo wp_json_encode(WP_DEBUG); ?>,
    pluginUrl: '<?php echo esc_js(plugin_dir_url(dirname(__FILE__))); ?>',
    isPremium: <?php echo wp_json_encode($is_premium); ?>,
    imageLimit: <?php echo intval($image_limit); ?>,
    perPage: <?php echo intval($per_page); ?>
};
</script>
