<?php

if (!defined('ABSPATH')) { exit; }
?>
<div class="aieo-dmm-section" id="aieo-dmm-feeds-list">
    <h2><?php esc_html_e('Current feeds', 'ai-eshop-optimizer'); ?></h2>

    <?php if (empty($feeds)) : ?>
        <p class="description"><?php esc_html_e('No feeds configured yet. Fill in the form above to create one.', 'ai-eshop-optimizer'); ?></p>
    <?php else : ?>
        <table class="widefat striped aieo-table">
            <thead>
                <tr>
                    <th><?php esc_html_e('Label', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Format', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Schedule', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Cache busting', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Public URL / file path', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Last run', 'ai-eshop-optimizer'); ?></th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($feeds as $f) : ?>
                    <tr data-id="<?php echo (int) $f['id']; ?>">
                        <td>
                            <strong><?php echo esc_html($f['label']); ?></strong><br>
                            <code><?php echo esc_html($f['slug']); ?></code>
                        </td>
                        <td><?php echo esc_html(strtoupper($f['format'])); ?></td>
                        <td><?php echo esc_html($f['schedule']); ?></td>
                        <td>
                            <?php
                            switch ($f['cache_mode']) {
                                case 'unique_filename': echo esc_html__('Unique filename', 'ai-eshop-optimizer'); break;
                                case 'none':            echo esc_html__('None', 'ai-eshop-optimizer'); break;
                                default:                echo esc_html__('?v=<etag>', 'ai-eshop-optimizer'); break;
                            }
                            ?>
                        </td>
                        <td style="max-width:420px;">
                            <?php if (!empty($f['public_url'])) : ?>
                                <div style="display:flex;gap:6px;align-items:center;margin-bottom:4px;">
                                    <input type="text" readonly value="<?php echo esc_attr($f['public_url']); ?>" style="flex:1;font-size:11px;font-family:Consolas,Menlo,monospace;" onclick="this.select();">
                                    <button type="button" class="button button-small aieo-dmm-copy" data-value="<?php echo esc_attr($f['public_url']); ?>"><?php esc_html_e('Copy URL', 'ai-eshop-optimizer'); ?></button>
                                </div>
                                <div style="display:flex;gap:6px;align-items:center;">
                                    <input type="text" readonly value="<?php echo esc_attr($f['file_path']); ?>" style="flex:1;font-size:11px;font-family:Consolas,Menlo,monospace;color:#666;" onclick="this.select();">
                                    <button type="button" class="button button-small aieo-dmm-copy" data-value="<?php echo esc_attr($f['file_path']); ?>"><?php esc_html_e('Copy path', 'ai-eshop-optimizer'); ?></button>
                                </div>
                                <?php if (empty($f['file_exists'])) : ?>
                                    <p style="color:#b24b00;font-size:11px;margin:4px 0 0;">
                                        ⚠ <?php esc_html_e('File not yet generated — press “Run now”.', 'ai-eshop-optimizer'); ?>
                                    </p>
                                <?php endif; ?>
                            <?php else : ?>
                                <span class="description"><?php esc_html_e('(file not yet generated)', 'ai-eshop-optimizer'); ?></span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <?php echo esc_html((string) ($f['last_run_at'] ?? '—')); ?>
                            <?php if (!empty($f['last_run_count'])) : ?>
                                <br><small><?php echo (int) $f['last_run_count']; ?> <?php esc_html_e('items', 'ai-eshop-optimizer'); ?></small>
                            <?php endif; ?>
                            <?php if (!empty($f['last_run_etag'])) : ?>
                                <br><small style="color:#888;">etag: <code><?php echo esc_html($f['last_run_etag']); ?></code></small>
                            <?php endif; ?>
                        </td>
                        <td>
                            <button type="button" class="button button-small aieo-dmm-feed-edit"
                                    data-id="<?php echo (int) $f['id']; ?>">
                                <?php esc_html_e('Edit', 'ai-eshop-optimizer'); ?>
                            </button>
                            <button type="button" class="button button-small" data-action="aieo_dmm_feed_run_now">
                                <?php esc_html_e('Run now', 'ai-eshop-optimizer'); ?>
                            </button>
                            <button type="button" class="button button-small aieo-dmm-delete-row" data-action="aieo_dmm_feed_delete">
                                <?php esc_html_e('Delete', 'ai-eshop-optimizer'); ?>
                            </button>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>
