<?php
if (!defined('ABSPATH'))
    exit;

class PRECIOUSUITE_Quotes
{

    public function __construct()
    {
        add_action('init', array($this, 'register_cpt'));
        add_filter('manage_preciousuite_quote_posts_columns', array($this, 'quote_columns'));
        add_action('manage_preciousuite_quote_posts_custom_column', array($this, 'render_quote_columns'), 10, 2);
        add_action('all_admin_notices', array($this, 'render_quote_tabs'));
        add_filter('parent_file', array($this, 'set_parent_file'));
        add_filter('submenu_file', array($this, 'set_submenu_file'));
    }

    public function register_cpt()
    {
        $args = array(
            'labels' => array(
                'name' => __('Buyback Quotes', 'preciousuite-gold-buyback-quote-calculator'),
                'singular_name' => __('Buyback Quote', 'preciousuite-gold-buyback-quote-calculator'),
                'menu_name' => __('Buyback Quotes', 'preciousuite-gold-buyback-quote-calculator'),
                'all_items' => __('Buyback Quotes', 'preciousuite-gold-buyback-quote-calculator'),
                'not_found' => __('No quotes found.', 'preciousuite-gold-buyback-quote-calculator'),
            ),
            'public' => false,
            'show_ui' => true,
            'show_in_menu' => false,
            'supports' => array('title'),
            'capabilities' => array(
                'create_posts' => 'do_not_allow',
            ),
            'map_meta_cap' => true,
        );
        register_post_type('preciousuite_quote', $args);
    }

    public function quote_columns($columns)
    {
        return array(
            'cb' => $columns['cb'],
            'title' => __('Quote ID', 'preciousuite-gold-buyback-quote-calculator'),
            'customer_email' => __('Customer Email', 'preciousuite-gold-buyback-quote-calculator'),
            'weight' => __('Weight', 'preciousuite-gold-buyback-quote-calculator'),
            'purity' => __('Purity', 'preciousuite-gold-buyback-quote-calculator'),
            'quoted_price' => __('Quoted Price', 'preciousuite-gold-buyback-quote-calculator'),
            'date' => $columns['date'],
        );
    }

    public function render_quote_columns($column, $post_id)
    {
        if ('customer_email' === $column) {
            $email = get_post_meta($post_id, '_preciousuite_email', true);
            echo $email ? '<a href="mailto:' . esc_attr($email) . '">' . esc_html($email) . '</a>' : '&mdash;';
        }

        if ('weight' === $column) {
            $weight = get_post_meta($post_id, '_preciousuite_weight', true);
            echo $weight ? esc_html($weight . 'g') : '&mdash;';
        }

        if ('purity' === $column) {
            $karat = get_post_meta($post_id, '_preciousuite_karat', true);
            echo $karat ? esc_html($karat) : '&mdash;';
        }

        if ('quoted_price' === $column) {
            $price = get_post_meta($post_id, '_preciousuite_price', true);
            echo $price ? esc_html($price) : '&mdash;';
        }
    }

    public function render_quote_tabs()
    {
        $screen = get_current_screen();

        if (!$screen || 'edit-preciousuite_quote' !== $screen->id) {
            return;
        }

        $tabs = array(
            'settings' => array(
                'label' => __('Calculator Settings', 'preciousuite-gold-buyback-quote-calculator'),
                'url'   => admin_url('admin.php?page=preciousuite-settings'),
            ),
            'quotes'   => array(
                'label' => __('Quotes', 'preciousuite-gold-buyback-quote-calculator'),
                'url'   => admin_url('edit.php?post_type=preciousuite_quote'),
            ),
        );
        ?>
        <div class="wrap preciousuite-cpt-tabs-wrap">
            <h2 class="nav-tab-wrapper preciousuite-tabs">
                <?php foreach ($tabs as $tab_key => $tab) : ?>
                    <a href="<?php echo esc_url($tab['url']); ?>" class="nav-tab <?php echo 'quotes' === $tab_key ? 'nav-tab-active' : ''; ?>">
                        <?php echo esc_html($tab['label']); ?>
                    </a>
                <?php endforeach; ?>
            </h2>
        </div>
        <?php
    }

    public function set_parent_file($parent_file)
    {
        $screen = get_current_screen();

        if ($screen && in_array($screen->id, array('edit-preciousuite_quote', 'preciousuite_quote'), true)) {
            return 'preciousuite';
        }

        return $parent_file;
    }

    public function set_submenu_file($submenu_file)
    {
        $screen = get_current_screen();

        if ($screen && in_array($screen->id, array('edit-preciousuite_quote', 'preciousuite_quote'), true)) {
            return 'preciousuite-settings';
        }

        return $submenu_file;
    }

    private function handle_actions()
    {
        // Single delete
        // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce is sanitized by sanitize_text_field in wp_verify_nonce call
        if (
            isset($_GET['action'], $_GET['quote_id'], $_GET['_wpnonce']) &&
            $_GET['action'] === 'delete' &&
            wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'])), 'preciousuite_delete_quote_' . intval($_GET['quote_id']))
        ) {
            wp_delete_post(intval($_GET['quote_id']), true);
            wp_safe_redirect(admin_url('admin.php?page=preciousuite-quotes&deleted=1'));
            exit;
        }

        // Bulk delete
        // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce is sanitized by sanitize_text_field in wp_verify_nonce call
        if (
            isset($_POST['action'], $_POST['quote_ids'], $_POST['_wpnonce']) &&
            $_POST['action'] === 'bulk_delete' &&
            wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'preciousuite_bulk_action')
        ) {
            $ids = array_map('intval', $_POST['quote_ids']);
            foreach ($ids as $id) {
                wp_delete_post($id, true);
            }
            wp_safe_redirect(admin_url('admin.php?page=preciousuite-quotes&deleted=' . count($ids)));
            exit;
        }
    }

    private function get_quotes($search = '', $paged = 1, $per_page = 20)
    {
        $args = array(
            'post_type' => 'preciousuite_quote',
            'post_status' => 'publish',
            'posts_per_page' => $per_page,
            'paged' => $paged,
            'orderby' => 'date',
            'order' => 'DESC',
        );

        if (!empty($search)) {
            // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Necessary for email search functionality
            $args['meta_query'] = array(
                'relation' => 'OR',
                array(
                    'key' => '_preciousuite_email',
                    'value' => $search,
                    'compare' => 'LIKE',
                ),
            );
            // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Necessary for search functionality
            add_filter('posts_search', function ($sql) use ($search) {
                global $wpdb;
                $like = '%' . $wpdb->esc_like($search) . '%';
                $sql .= $wpdb->prepare(" OR {$wpdb->posts}.post_title LIKE %s", $like);
                return $sql;
            });
        }

        $query = new WP_Query($args);
        return array(
            'posts' => $query->posts,
            'total' => $query->found_posts,
            'total_pages' => $query->max_num_pages,
        );
    }

    public function render_quotes_page()
    {
        // Verify we're in admin context before processing any form data
        if (!current_user_can('manage_options')) {
            wp_die('Unauthorized access');
        }

        $this->handle_actions();

        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only display page with no state changes
        $search = isset($_GET['s']) ? sanitize_text_field(wp_unslash($_GET['s'])) : '';
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only pagination parameter
        $paged = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1;
        $per_page = 20;
        $data = $this->get_quotes($search, $paged, $per_page);
        $posts = $data['posts'];
        $total = $data['total'];
        $pages = $data['total_pages'];
        $base_url = admin_url('admin.php?page=preciousuite-quotes');

        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only status display parameter
        $deleted_count = isset($_GET['deleted']) ? intval($_GET['deleted']) : 0;

        // Today count
        $today_query = new WP_Query(array(
            'post_type' => 'preciousuite_quote',
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'date_query' => array(array('after' => 'today midnight')),
        ));
        $today_count = $today_query->found_posts;

        // This month count
        $month_query = new WP_Query(array(
            'post_type' => 'preciousuite_quote',
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'date_query' => array(array('after' => gmdate('Y-m-01'))),
        ));
        $month_count = $month_query->found_posts;
?>

        <div class="preciousuite-gold-price">
            <div class="preciousuite-wrapper">

                <!-- ── Main Content ───────────────────── -->
                <div class="preciousuite-setting-page">

                    <!-- Header -->
                    <div class="preciousuite-boxes preciousuite-header">
                        <div class="preciousuite-box preciousuite-page-title">
                            <h1><?php esc_html_e('Gold Buyback Quotes', 'preciousuite-gold-buyback-quote-calculator'); ?></h1>
                        </div>
                        <div class="preciousuite-box preciousuite-logo">
                            <a href="https://www.preciousuite.com" target="_blank" class="preciousuite-page-title">
                                <img src="<?php echo esc_url(PRECIOUSUITE_URL); ?>assets/uploads/Preciousuite_Logo.png" alt="Logo">
                            </a>
                        </div>
                    </div>

                    <!-- Form Wrap -->
                    <div class="preciousuite form-wrap">
                        <div id="preciousuite-container">

                            <?php if ($deleted_count > 0): ?>
                                <div class="notice notice-success is-dismissible" style="margin:0 0 16px 0;">
                                    <p><?php echo esc_html(intval($deleted_count) === 1 ? '1 quote deleted.' : intval($deleted_count) . ' quotes deleted.'); ?>
                                    </p>
                                </div>
                            <?php endif; ?>

                            <!-- Stats Row — uses preciousuite-boxes + preciousuite-box -->
                            <div class="preciousuite-boxes grid" style="gap:16px; margin-bottom:20px;">
                                <div class="preciousuite-box"
                                    style="flex:1; background:#f9eef4; border:1px solid rgba(137,22,82,0.15); border-radius:8px; padding:16px 20px; text-align:center;">
                                    <div style="font-size:26px; font-weight:700; color:#891652;"><?php echo esc_html(intval($total)); ?></div>
                                    <div style="font-size:12px; color:#6e6e73; font-weight:500; margin-top:2px;">Total Quotes
                                    </div>
                                </div>
                                <div class="preciousuite-box"
                                    style="flex:1; background:#f9eef4; border:1px solid rgba(137,22,82,0.15); border-radius:8px; padding:16px 20px; text-align:center;">
                                    <div style="font-size:26px; font-weight:700; color:#891652;"><?php echo esc_html(intval($today_count)); ?>
                                    </div>
                                    <div style="font-size:12px; color:#6e6e73; font-weight:500; margin-top:2px;">Today</div>
                                </div>
                                <div class="preciousuite-box"
                                    style="flex:1; background:#f9eef4; border:1px solid rgba(137,22,82,0.15); border-radius:8px; padding:16px 20px; text-align:center;">
                                    <div style="font-size:26px; font-weight:700; color:#891652;"><?php echo esc_html(intval($month_count)); ?>
                                    </div>
                                    <div style="font-size:12px; color:#6e6e73; font-weight:500; margin-top:2px;">This Month
                                    </div>
                                </div>
                            </div>


                            <?php if (empty($posts)): ?>
                                <!-- Empty State -->
                                <div
                                    style="text-align:center; padding:60px 20px; background:#fafafa; border:1px solid #e8e8e8; border-radius:8px;">
                                    <div style="font-size:48px; margin-bottom:12px;">📋</div>
                                    <h3 style="color:#333; margin:0 0 8px;">
                                        <?php echo $search ? 'No quotes found for "' . esc_html($search) . '"' : 'No quotes yet'; ?>
                                    </h3>
                                    <p style="color:#6e6e73; font-size:14px; margin:0;">
                                        <?php echo $search ? 'Try a different search term.' : 'Quotes will appear here once customers use the calculator.'; ?>
                                    </p>
                                </div>

                            <?php else: ?>

                                <!-- Bulk Delete Form -->
                                <form method="post" id="preciousuite-quotes-form">
                                    <?php wp_nonce_field('preciousuite_bulk_action', '_wpnonce'); ?>
                                    <input type="hidden" name="action" value="bulk_delete">

                                    <!-- Bulk Bar — uses preciousuite-boxes -->
                                    <div class="preciousuite-boxes"
                                        style="justify-content:flex-start; gap:14px; padding:10px 14px; background:#fff; border:1px solid #e8e8e8; border-bottom:none; border-radius:8px 8px 0 0;">
                                        <label
                                            style="display:flex;align-items:center;gap:8px;font-size:13px;color:#6e6e73;cursor:pointer;font-weight:500;">
                                            <input type="checkbox" id="preciousuite-select-all"
                                                style="accent-color:#891652;width:15px;height:15px;">
                                            Select All
                                        </label>
                                        <button type="submit" class="btn-primary delete-selected"
                                            onclick="return confirm('Delete selected quotes?')">
                                            🗑 Delete Selected
                                        </button>
                                    </div>

                                    <!-- Table -->
                                    <div
                                        style="overflow-x:auto; border:1px solid #e8e8e8; border-radius:0 0 8px 8px; background:#fff;">
                                        <table class="widefat"
                                            style="border:none; border-collapse:collapse; font-size:13px; width:100%;">
                                            <thead>
                                                <tr style="background:linear-gradient(135deg,#891652,#6b1040);">
                                                    <th
                                                        style="padding:12px 14px;color:#fff;font-size:11px;font-weight:600;text-transform:uppercase;letter-spacing:0.5px;width:40px;border:none;">
                                                        <input type="checkbox" id="preciousuite-select-all-top"
                                                            style="accent-color:#fff;width:15px;height:15px;">
                                                    </th>
                                                    <th
                                                        style="padding:12px 14px;color:#fff;font-size:11px;font-weight:600;text-transform:uppercase;letter-spacing:0.5px;border:none;">
                                                        Quote ID</th>
                                                    <th
                                                        style="padding:12px 14px;color:#fff;font-size:11px;font-weight:600;text-transform:uppercase;letter-spacing:0.5px;border:none;">
                                                        Customer Email</th>
                                                    <th
                                                        style="padding:12px 14px;color:#fff;font-size:11px;font-weight:600;text-transform:uppercase;letter-spacing:0.5px;border:none;">
                                                        Weight</th>
                                                    <th
                                                        style="padding:12px 14px;color:#fff;font-size:11px;font-weight:600;text-transform:uppercase;letter-spacing:0.5px;border:none;">
                                                        Purity</th>
                                                    <th
                                                        style="padding:12px 14px;color:#fff;font-size:11px;font-weight:600;text-transform:uppercase;letter-spacing:0.5px;border:none;">
                                                        Quoted Price</th>
                                                    <th
                                                        style="padding:12px 14px;color:#fff;font-size:11px;font-weight:600;text-transform:uppercase;letter-spacing:0.5px;border:none;">
                                                        Date</th>
                                                    <th
                                                        style="padding:12px 14px;color:#fff;font-size:11px;font-weight:600;text-transform:uppercase;letter-spacing:0.5px;border:none;text-align:center;">
                                                        Actions</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php foreach ($posts as $index => $post):
                                                    $email = get_post_meta($post->ID, '_preciousuite_email', true);
                                                    $weight = get_post_meta($post->ID, '_preciousuite_weight', true);
                                                    $karat = get_post_meta($post->ID, '_preciousuite_karat', true);
                                                    $price = get_post_meta($post->ID, '_preciousuite_price', true);
                                                    $date = get_the_date('M j, Y', $post->ID);
                                                    $time = get_the_date('g:i A', $post->ID);
                                                    $delete_nonce = wp_create_nonce('preciousuite_delete_quote_' . $post->ID);
                                                    $delete_url = esc_url(add_query_arg(array(
                                                        'page' => 'preciousuite-quotes',
                                                        'action' => 'delete',
                                                        'quote_id' => $post->ID,
                                                        '_wpnonce' => $delete_nonce,
                                                    ), admin_url('admin.php')));
                                                    $row_bg = ($index % 2 === 0) ? '#ffffff' : '#fafafa';
                                                ?>
                                                    <tr style="background:<?php echo esc_attr($row_bg); ?>;"
                                                        onmouseover="this.style.background='#f9eef4'"
                                                        onmouseout="this.style.background='<?php echo esc_attr($row_bg); ?>'">
                                                        <td
                                                            style="padding:11px 14px;border-bottom:1px solid #f0f0f0;vertical-align:middle;">
                                                            <input type="checkbox" name="quote_ids[]" value="<?php echo intval($post->ID); ?>"
                                                                class="preciousuite-row-check"
                                                                style="accent-color:#891652;width:15px;height:15px;cursor:pointer;">
                                                        </td>
                                                        <td
                                                            style="padding:11px 14px;border-bottom:1px solid #f0f0f0;vertical-align:middle;">
                                                            <span
                                                                style="background:#f9eef4;color:#891652;font-weight:700;font-size:12px;padding:3px 8px;border-radius:5px;font-family:'Courier New',monospace;letter-spacing:0.5px;">
                                                                <?php echo esc_html($post->post_title); ?>
                                                            </span>
                                                        </td>
                                                        <td
                                                            style="padding:11px 14px;border-bottom:1px solid #f0f0f0;vertical-align:middle;">
                                                            <a href="mailto:<?php echo esc_attr($email); ?>"
                                                                style="color:#891652;text-decoration:none;font-weight:500;">
                                                                <?php echo esc_html($email ?: '—'); ?>
                                                            </a>
                                                        </td>
                                                        <td
                                                            style="padding:11px 14px;border-bottom:1px solid #f0f0f0;vertical-align:middle;">
                                                            <span style="font-weight:600;color:#1d1d1f;">
                                                                <?php echo esc_html($weight); ?><small
                                                                    style="font-size:11px;color:#6e6e73;">g</small>
                                                            </span>
                                                        </td>
                                                        <td
                                                            style="padding:11px 14px;border-bottom:1px solid #f0f0f0;vertical-align:middle;">
                                                            <span
                                                                style="background:#fef9e7;color:#b7860b;border:1px solid #f0d060;font-size:10px;font-weight:700;padding:3px 8px;border-radius:5px;">
                                                                <?php echo esc_html($karat ?: '—'); ?>
                                                            </span>
                                                        </td>
                                                        <td
                                                            style="padding:11px 14px;border-bottom:1px solid #f0f0f0;vertical-align:middle;">
                                                            <span style="color:#27ae60;font-weight:700;font-size:15px;">
                                                                <?php echo esc_html($price ?: '—'); ?>
                                                            </span>
                                                        </td>
                                                        <td
                                                            style="padding:11px 14px;border-bottom:1px solid #f0f0f0;vertical-align:middle;">
                                                            <span
                                                                style="font-size:13px;color:#1d1d1f;display:block;"><?php echo esc_html($date); ?></span>
                                                            <small
                                                                style="font-size:11px;color:#6e6e73;"><?php echo esc_html($time); ?></small>
                                                        </td>
                                                        <td
                                                            style="padding:11px 14px;border-bottom:1px solid #f0f0f0;vertical-align:middle;text-align:center;">
                                                            <div style="display:flex;gap:6px;justify-content:center;">
                                                                <!-- <a href="mailto:<?php echo esc_attr($email); ?>"
                                                            title="Email Customer"
                                                            style="display:inline-flex;align-items:center;justify-content:center;width:30px;height:30px;background:#eaf4fe;border:1.5px solid #aed6f5;border-radius:6px;font-size:14px;text-decoration:none;color:#2980b9;"
                                                            onmouseover="this.style.background='#2980b9';this.style.color='#fff'"
                                                            onmouseout="this.style.background='#eaf4fe';this.style.color='#2980b9'">
                                                            ✉
                                                        </a> -->
                                                                <a href="<?php echo esc_url($delete_url); ?>" title="Delete Quote"
                                                                    onclick="return confirm('Delete quote <?php echo esc_js($post->post_title); ?>?')"
                                                                    style="display:inline-flex;align-items:center;justify-content:center;width:30px;height:30px;background:#fdf2f2;border:1.5px solid #f5b7b1;border-radius:6px;font-size:14px;text-decoration:none;color:#e74c3c;"
                                                                    onmouseover="this.style.background='#e74c3c';this.style.color='#fff'"
                                                                    onmouseout="this.style.background='#fdf2f2';this.style.color='#e74c3c'">
                                                                    🗑
                                                                </a>
                                                            </div>
                                                        </td>
                                                    </tr>
                                                <?php endforeach; ?>
                                            </tbody>
                                        </table>
                                    </div>

                                    <!-- Pagination -->
                                    <?php if ($pages > 1): ?>
                                        <div class="preciousuite-boxes"
                                            style="justify-content:space-between; padding:14px 0 0; flex-wrap:wrap; gap:10px;">
                                            <span style="font-size:13px;color:#6e6e73;">
                                                Showing
                                                <?php echo intval((($paged - 1) * $per_page) + 1); ?>–<?php echo intval(min($paged * $per_page, $total)); ?>
                                                of <?php echo intval($total); ?>
                                            </span>
                                            <div style="display:flex;gap:5px;">
                                                <?php if ($paged > 1): ?>
                                                    <a href="<?php echo esc_url(add_query_arg(array('paged' => 1, 's' => $search), $base_url)); ?>"
                                                        style="display:inline-flex;align-items:center;justify-content:center;width:32px;height:32px;border-radius:6px;background:#fff;border:1.5px solid #e8e8e8;font-size:13px;color:#333;text-decoration:none;">«</a>
                                                    <a href="<?php echo esc_url(add_query_arg(array('paged' => $paged - 1, 's' => $search), $base_url)); ?>"
                                                        style="display:inline-flex;align-items:center;justify-content:center;width:32px;height:32px;border-radius:6px;background:#fff;border:1.5px solid #e8e8e8;font-size:13px;color:#333;text-decoration:none;">‹</a>
                                                <?php endif; ?>
                                                <?php
                                                $start = max(1, $paged - 2);
                                                $end = min($pages, $paged + 2);
                                                for ($i = $start; $i <= $end; $i++):
                                                    $is_current = ($i === $paged);
                                                ?>
                                                    <a href="<?php echo esc_url(add_query_arg(array('paged' => $i, 's' => $search), $base_url)); ?>"
                                                        style="display:inline-flex;align-items:center;justify-content:center;width:32px;height:32px;border-radius:6px;font-size:13px;font-weight:<?php echo $is_current ? '700' : '500'; ?>;text-decoration:none;
                                               background:<?php echo $is_current ? '#891652' : '#fff'; ?>;
                                               color:<?php echo $is_current ? '#fff' : '#333'; ?>;
                                               border:1.5px solid <?php echo $is_current ? '#891652' : '#e8e8e8'; ?>;">
                                                        <?php echo intval($i); ?>
                                                    </a>
                                                <?php endfor; ?>
                                                <?php if ($paged < $pages): ?>
                                                    <a href="<?php echo esc_url(add_query_arg(array('paged' => $paged + 1, 's' => $search), $base_url)); ?>"
                                                        style="display:inline-flex;align-items:center;justify-content:center;width:32px;height:32px;border-radius:6px;background:#fff;border:1.5px solid #e8e8e8;font-size:13px;color:#333;text-decoration:none;">›</a>
                                                    <a href="<?php echo esc_url(add_query_arg(array('paged' => $pages, 's' => $search), $base_url)); ?>"
                                                        style="display:inline-flex;align-items:center;justify-content:center;width:32px;height:32px;border-radius:6px;background:#fff;border:1.5px solid #e8e8e8;font-size:13px;color:#333;text-decoration:none;">»</a>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                    <?php endif; ?>

                                </form>
                            <?php endif; ?>

                        </div><!-- /#preciousuite-container -->
                    </div><!-- /.preciousuite.form-wrap -->

                    <!-- Footer -->
                    <div class="preciousuite-footer">
                        <div class="preciousuite-box preciousuite-page-title">
                            <h3><?php esc_html_e('Gold Buyback Quotes', 'preciousuite-gold-buyback-quote-calculator'); ?></h3>
                        </div>
                        <div class="preciousuite-box preciousuite-logo">
                            <label>Developed by - </label>
                            <a href="https://www.preciousuite.com/" target="_blank" class="preciousuite-page-title">
                                <img src="<?php echo esc_url(PRECIOUSUITE_URL); ?>assets/uploads/Preciousuite_Logo.png" alt="Logo">
                            </a>
                        </div>
                    </div>

                </div><!-- /.preciousuite-setting-page -->

                <!-- ── Ads Sidebar ────────────────────── -->
                <div class="preciousuite-ads">
                    <div class="preciousuite-ads-card">
                        <div class="preciousuite-box preciousuite-logo">
                            <a href="https://www.preciousuite.com" target="_blank" class="preciousuite-page-title">
                                <img src="<?php echo esc_url(PRECIOUSUITE_URL); ?>assets/uploads/Preciousuite_Logo.png" alt="Logo">
                            </a>
                        </div>
                        <div class="preciousuite-ads-card-content">
                            <div class="preciousuite-ads-card-pd">
                                <p class="preciousuite-ads-card-title">Free <span class="color-dp">Gold Price</span> Plugin</p>
                                <a href="https://www.preciousuite.com/product-details/9" target="_blank"
                                    class="preciousuite-page-title">
                                    <button class="btn-primary">
                                        <?php esc_html_e('Get Plugin', 'preciousuite-gold-buyback-quote-calculator'); ?>
                                    </button>
                                </a>
                            </div>
                            <div class="preciousuite-ads-img">
                                <img src="<?php echo esc_url(PRECIOUSUITE_URL); ?>assets/uploads/gold-bg.png" alt="Ads">
                            </div>
                        </div>
                    </div>

                    <div class="preciousuite-ads-card">
                        <div class="preciousuite-box preciousuite-logo ">
                            <a href="https://www.preciousuite.com" target="_blank" class="preciousuite-page-title">
                                <img src="<?php echo esc_url(PRECIOUSUITE_URL); ?>assets/uploads/Preciousuite_Logo.png" alt="Logo">
                            </a>
                        </div>
                        <div class="preciousuite-ads-card-content">
                            <div class="preciousuite-ads-card-pd">
                                <p class="preciousuite-ads-card-title">Free <span class="color-dp">Metal Rate Charts </span>
                                    Plugin</p>
                                <a href="https://www.preciousuite.com/product-details/10" target="_blank"
                                    class="preciousuite-page-title">
                                    <button class="btn-primary">
                                        <?php esc_html_e('Get Plugin', 'preciousuite-gold-buyback-quote-calculator'); ?>
                                    </button>
                                </a>
                            </div>
                            <div class="preciousuite-ads-img">
                                <img src="<?php echo esc_url(PRECIOUSUITE_URL); ?>assets/uploads/gold-bg.png" alt="Ads">
                            </div>
                        </div>
                    </div>

                </div><!-- /.preciousuite-ads -->

            </div><!-- /.preciousuite-wrapper -->
        </div><!-- /.preciousuite-gold-price -->

        <script>
            jQuery(document).ready(function($) {
                $('#preciousuite-select-all, #preciousuite-select-all-top').on('change', function() {
                    var checked = $(this).is(':checked');
                    $('.preciousuite-row-check').prop('checked', checked);
                    $('#preciousuite-select-all, #preciousuite-select-all-top').prop('checked', checked);
                    updateCount();
                });
                $(document).on('change', '.preciousuite-row-check', function() {
                    var total = $('.preciousuite-row-check').length;
                    var checked = $('.preciousuite-row-check:checked').length;
                    $('#preciousuite-select-all, #preciousuite-select-all-top').prop('checked', total === checked);
                    updateCount();
                });

                function updateCount() {
                    var count = $('.preciousuite-row-check:checked').length;
                    $('#preciousuite-bulk-count').text(count > 0 ? count + ' selected' : '');
                }
            });
        </script>
<?php
    }
}

new PRECIOUSUITE_Quotes();
