<?php

if (!defined('ABSPATH')) {
    exit;
}

?>
<div class="aieo-store-admin">

    <h2><?php esc_html_e('Store Pages', 'ai-eshop-optimizer'); ?></h2>
    <p class="description" style="max-width:840px;">
        <?php esc_html_e('Your store / location pages and the plugin store-page settings, in one place. The list shows every store page on the site; “Edit” opens the block editor.', 'ai-eshop-optimizer'); ?>
    </p>

    <!-- ── Config card (migrated from SEO → Schema) ──────────────── -->
    <div class="card aieo-store-card">
        <h3 style="margin-top:0;"><?php esc_html_e('Store pages (plugin store CPT)', 'ai-eshop-optimizer'); ?></h3>
        <p class="description" style="max-width:880px;">
            <?php esc_html_e('Optional: a block-based page per shop, each bound to a location. A store page becomes that shop\'s canonical URL + LocalBusiness landing page. You choose the URL base and label — nothing is hardcoded. This governs the plugin store CPT; your theme store pages are listed below regardless.', 'ai-eshop-optimizer'); ?>
        </p>
        <p><label><input type="checkbox" id="aieo-store-cpt-enabled" <?php checked(!empty($seo['store_cpt_enabled'])); ?>> <strong><?php esc_html_e('Enable store pages', 'ai-eshop-optimizer'); ?></strong></label></p>
        <table class="form-table">
            <tr>
                <th><label for="aieo-store-cpt-base"><?php esc_html_e('URL base', 'ai-eshop-optimizer'); ?></label></th>
                <td>
                    <code><?php echo esc_html(trailingslashit(home_url())); ?></code><input type="text" id="aieo-store-cpt-base" style="width:180px;" value="<?php echo esc_attr($seo['store_cpt_base'] ?? ''); ?>" placeholder="stores"><code>/&lt;slug&gt;/</code>
                    <p class="description"><?php esc_html_e('The URL segment for store pages — e.g. stores, shops, katastimata. Letters, numbers and dashes only.', 'ai-eshop-optimizer'); ?></p>
                </td>
            </tr>
            <tr>
                <th><label for="aieo-store-cpt-singular"><?php esc_html_e('Label (singular)', 'ai-eshop-optimizer'); ?></label></th>
                <td><input type="text" id="aieo-store-cpt-singular" class="regular-text" value="<?php echo esc_attr($seo['store_cpt_singular'] ?? ''); ?>" placeholder="<?php esc_attr_e('Store', 'ai-eshop-optimizer'); ?>"></td>
            </tr>
            <tr>
                <th><label for="aieo-store-cpt-plural"><?php esc_html_e('Label (plural)', 'ai-eshop-optimizer'); ?></label></th>
                <td><input type="text" id="aieo-store-cpt-plural" class="regular-text" value="<?php echo esc_attr($seo['store_cpt_plural'] ?? ''); ?>" placeholder="<?php esc_attr_e('Stores', 'ai-eshop-optimizer'); ?>"></td>
            </tr>
        </table>
        <p class="description"><?php esc_html_e('After enabling + saving, the plugin store CPT becomes available. Create one page per shop, bind it to its location, and publish — the LocalBusiness URL + locations sitemap follow automatically.', 'ai-eshop-optimizer'); ?></p>
        <p>
            <button type="button" class="button button-primary" id="aieo-store-save"><?php esc_html_e('Save', 'ai-eshop-optimizer'); ?></button>
            <span id="aieo-store-status" class="aieo-store-status"></span>
        </p>
    </div>

    <!-- ── Entries ───────────────────────────────────────────────── -->
    <h3 style="margin-top:28px;"><?php esc_html_e('Store pages', 'ai-eshop-optimizer'); ?></h3>
    <?php if (empty($stores)) : ?>
        <p><?php esc_html_e('No store pages yet.', 'ai-eshop-optimizer'); ?></p>
    <?php else : ?>
        <table class="wp-list-table widefat fixed striped">
            <thead><tr>
                <th><?php esc_html_e('Title', 'ai-eshop-optimizer'); ?></th>
                <th style="width:130px;"><?php esc_html_e('Type', 'ai-eshop-optimizer'); ?></th>
                <th><?php esc_html_e('Bound location', 'ai-eshop-optimizer'); ?></th>
                <th style="width:90px;"><?php esc_html_e('Status', 'ai-eshop-optimizer'); ?></th>
                <th style="width:130px;"><?php esc_html_e('Actions', 'ai-eshop-optimizer'); ?></th>
            </tr></thead>
            <tbody>
            <?php foreach ($stores as $s) :
                $edit = get_edit_post_link($s->ID, 'raw');
                $view = get_permalink($s->ID);
                $type = ('aieo_store' === $s->post_type) ? __('Plugin', 'ai-eshop-optimizer') : __('Theme', 'ai-eshop-optimizer');
                $lid  = (int) get_post_meta($s->ID, '_aieo_grv_location_id', true);
                $loc  = ($lid > 0 && isset($loc_map[$lid])) ? $loc_map[$lid] : ($lid > 0 ? '#' . $lid : '—');
                ?>
                <tr>
                    <td><strong><?php
                        $title = get_the_title($s) ?: __('(untitled)', 'ai-eshop-optimizer');
                        if ($edit) {
                            echo '<a href="' . esc_url($edit) . '">' . esc_html($title) . '</a>';
                        } else {
                            echo esc_html($title);
                        }
                    ?></strong></td>
                    <td><?php echo esc_html($type); ?> <span style="color:#646970;font-size:12px;">· <?php echo esc_html($s->post_type); ?></span></td>
                    <td><?php echo esc_html($loc); ?></td>
                    <td><?php echo esc_html(get_post_status($s)); ?></td>
                    <td style="white-space:nowrap;">
                        <?php if ($edit) : ?><a href="<?php echo esc_url($edit); ?>"><?php esc_html_e('Edit', 'ai-eshop-optimizer'); ?></a><?php endif; ?>
                        <?php if ($view) : ?> · <a href="<?php echo esc_url($view); ?>" target="_blank" rel="noopener"><?php esc_html_e('View', 'ai-eshop-optimizer'); ?></a><?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>

</div>
