<?php defined('ABSPATH') || exit;
/**
 * @var JRA_Merchant_Offer_Id_Map $map
 * @var array                     $rows
 * @var int                       $total
 * @var int                       $pages
 * @var int                       $page
 * @var int                       $perPage
 */
?>
<div class="wrap jra-merchant">
    <h1>Google Merchant Feed — Offer ID Map</h1>

    <div class="jra-card" style="border-left:4px solid #f0a500;background:#fffbf0;">
        <h2 style="color:#8a6000;">⚠ Permanent identity mapping</h2>
        <p>
            This table tells the plugin to push a product to Google using a <strong>different offer ID</strong>
            than it would normally generate. Use this when your products already exist in Google Merchant
            Center under offer IDs from a previous feed (e.g. a stock-system feed) and you want to
            preserve their performance history.
        </p>
        <p>
            <strong>Removing a row is permanent and irreversible from Google's perspective.</strong>
            The next sync after deletion will push the product under its auto-generated offer ID.
            Google will treat it as a brand-new product — any accumulated impressions, clicks, and
            ranking signals on the old offer ID will be lost.
        </p>
    </div>

    <!-- ------------------------------------------------------------------ -->
    <!-- Current mappings                                                      -->
    <!-- ------------------------------------------------------------------ -->
    <div class="jra-card" style="max-width:none;">
        <h2>Current mappings
            <span style="font-size:13px;font-weight:normal;color:#777;margin-left:8px;"><?php echo number_format($total) ?> row(s)</span>
        </h2>

        <?php if (empty($rows)): ?>
            <p class="text-muted">No mappings configured. Import a CSV or add a row manually below.</p>
        <?php else: ?>
        <div style="overflow-x:auto;">
        <table class="widefat striped" style="font-size:13px;">
            <thead>
                <tr>
                    <th>Local offer ID <small style="font-weight:normal;">(plugin generates this)</small></th>
                    <th>Google offer ID <small style="font-weight:normal;">(pushed to Merchant Center)</small></th>
                    <th>Note</th>
                    <th>Added (UTC)</th>
                    <th style="width:80px;"></th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($rows as $row): ?>
                <tr>
                    <td><code><?php echo esc_html($row['local_offer_id']) ?></code></td>
                    <td><code><?php echo esc_html($row['google_offer_id']) ?></code></td>
                    <td style="color:#666;"><?php echo esc_html($row['note']) ?></td>
                    <td style="white-space:nowrap;font-size:11px;color:#666;"><?php echo esc_html($row['created_at']) ?></td>
                    <td>
                        <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')) ?>"
                              onsubmit="return confirm('Delete this mapping?\n\nWarning: the next sync will push this product under its auto-generated offer ID and Google will treat it as a new product.')">
                            <?php wp_nonce_field('jra_merchant_offer_id_map_delete') ?>
                            <input type="hidden" name="action"          value="jra_merchant_offer_id_map_delete">
                            <input type="hidden" name="local_offer_id"  value="<?php echo esc_attr($row['local_offer_id']) ?>">
                            <button type="submit" class="button button-link-delete">Delete</button>
                        </form>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        </div>

        <?php if ($pages > 1): ?>
        <div class="tablenav bottom" style="margin-top:8px;">
            <div class="tablenav-pages">
                <?php for ($p = 1; $p <= $pages; $p++): ?>
                    <?php if ($p === $page): ?>
                        <span class="page-numbers current"><?php echo esc_html( (string) $p ) ?></span>
                    <?php else: ?>
                        <a class="page-numbers" href="<?php echo esc_url(admin_url('admin.php?page=jra-merchant-feed-offer-id-map&paged=' . $p)) ?>"><?php echo esc_html( (string) $p ) ?></a>
                    <?php endif; ?>
                <?php endfor; ?>
            </div>
        </div>
        <?php endif; ?>
        <?php endif; ?>
    </div>

    <!-- ------------------------------------------------------------------ -->
    <!-- CSV import                                                            -->
    <!-- ------------------------------------------------------------------ -->
    <div class="jra-card">
        <h2>Import from CSV</h2>
        <p>
            Upload a CSV file with the following columns (header row required):
        </p>
        <pre style="background:#f6f7f7;padding:10px;border:1px solid #ddd;display:inline-block;margin-bottom:12px;">local_offer_id,google_offer_id,note
SKU-123,SKU-456,stock system import
SKU-124,SKU-457,stock system import</pre>
        <p style="color:#666;font-size:12px;">
            The <code>note</code> column is optional. If omitted, the import note below is used for all rows.<br>
            Importing a <code>local_offer_id</code> that already exists will <strong>replace</strong> the existing mapping.
        </p>

        <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')) ?>" enctype="multipart/form-data">
            <?php wp_nonce_field('jra_merchant_offer_id_map') ?>
            <input type="hidden" name="action" value="jra_merchant_offer_id_map_import">

            <table class="form-table">
                <tr>
                    <th><label for="offer_id_map_csv">CSV file</label></th>
                    <td><input type="file" id="offer_id_map_csv" name="offer_id_map_csv" accept=".csv,text/csv" required></td>
                </tr>
                <tr>
                    <th><label for="import_note">Import note</label></th>
                    <td>
                        <input type="text" id="import_note" name="import_note" class="regular-text"
                               value="<?php echo esc_attr('CSV import ' . current_time('Y-m-d')) ?>"
                               placeholder="e.g. stock system import 2026-04-15">
                        <p class="description">Used as the note for rows that don't have their own note column.</p>
                    </td>
                </tr>
            </table>

            <?php submit_button('Import CSV') ?>
        </form>
    </div>

    <!-- ------------------------------------------------------------------ -->
    <!-- Manual add                                                            -->
    <!-- ------------------------------------------------------------------ -->
    <div class="jra-card">
        <h2>Add single mapping</h2>

        <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')) ?>">
            <?php wp_nonce_field('jra_merchant_offer_id_map') ?>
            <input type="hidden" name="action" value="jra_merchant_offer_id_map_add">

            <table class="form-table">
                <tr>
                    <th><label for="local_offer_id">Local offer ID</label></th>
                    <td>
                        <input type="text" id="local_offer_id" name="local_offer_id" class="regular-text"
                               placeholder="e.g. SKU-123" required>
                        <p class="description">The offer ID this plugin would auto-generate for the product.</p>
                    </td>
                </tr>
                <tr>
                    <th><label for="google_offer_id">Google offer ID</label></th>
                    <td>
                        <input type="text" id="google_offer_id" name="google_offer_id" class="regular-text"
                               placeholder="e.g. SKU-456" required>
                        <p class="description">The offer ID currently in Google Merchant Center.</p>
                    </td>
                </tr>
                <tr>
                    <th><label for="note">Note</label></th>
                    <td>
                        <input type="text" id="note" name="note" class="regular-text"
                               placeholder="Optional — reason for this mapping">
                    </td>
                </tr>
            </table>

            <?php submit_button('Add mapping') ?>
        </form>
    </div>

    <!-- ------------------------------------------------------------------ -->
    <!-- Stock system export helper                                            -->
    <!-- ------------------------------------------------------------------ -->
    <div class="jra-card">
        <h2>Generating the CSV from the stock system</h2>
        <p>If you are migrating from the JRA stock system feed, run this query against the stock system database,
           substituting <code>?</code> with the <code>website_id</code> for this WooCommerce site:</p>
        <pre style="background:#f6f7f7;padding:12px;border:1px solid #ddd;overflow-x:auto;">SELECT
    CONCAT('SKU-', pw.remote_product_id) AS local_offer_id,
    CONCAT('SKU-', p.id)                 AS google_offer_id,
    'stock system export'                AS note
FROM products p
JOIN products_websites pw ON pw.product_id = p.id
WHERE pw.website_id = ?
  AND pw.remote_product_id IS NOT NULL;</pre>
        <p style="color:#666;font-size:12px;">Export the result as CSV and import it above.</p>
    </div>
</div>
