<?php

if (!defined('ABSPATH')) { exit; }
?>
<div class="aieo-dmm-section">
    <h2><?php esc_html_e('Stock import', 'ai-eshop-optimizer'); ?></h2>
    <p class="description">
        <?php esc_html_e('Upload a CSV with stock levels. Stock is set via the WooCommerce API, so stock-status updates and low-stock emails stay consistent.', 'ai-eshop-optimizer'); ?>
    </p>

    <form id="aieo-dmm-stock-import-form" class="aieo-dmm-form" enctype="multipart/form-data">

        <!-- Step 1: tell us how your CSV is keyed -->
        <div class="aieo-dmm-card">
            <h3><?php esc_html_e('1. How is your CSV keyed?', 'ai-eshop-optimizer'); ?></h3>
            <p class="description" style="margin-top:-4px">
                <?php esc_html_e('The first column of your CSV must be either a WooCommerce SKU or a numeric product_id. The required "stock" column goes next.', 'ai-eshop-optimizer'); ?>
            </p>
            <div class="aieo-dmm-field-label"><?php esc_html_e('Key by', 'ai-eshop-optimizer'); ?></div>
            <div>
                <label class="aieo-dmm-choice"><input type="radio" name="key_by" value="sku" checked> <?php esc_html_e('SKU', 'ai-eshop-optimizer'); ?></label>
                <label class="aieo-dmm-choice"><input type="radio" name="key_by" value="product_id"> <code>product_id</code></label>
            </div>

            <div style="margin-top:10px">
                <label class="aieo-dmm-choice">
                    <input type="checkbox" name="update_stock_status" value="1" checked>
                    <?php esc_html_e('Also update stock status (in / out of stock)', 'ai-eshop-optimizer'); ?>
                </label>
            </div>
        </div>

        <!-- Step 2: need an example? -->
        <div class="aieo-dmm-card">
            <h3><?php esc_html_e('2. Need an example file?', 'ai-eshop-optimizer'); ?></h3>
            <p class="description" style="margin-top:-4px">
                <?php esc_html_e('The sample uses real products from your catalogue so column names and the key format match your shop. Overwrite the stock column with your desired values, then upload it back.', 'ai-eshop-optimizer'); ?>
            </p>
            <p style="margin:0;">
                <button type="button" class="button aieo-dmm-stock-sample" data-format="csv"><?php esc_html_e('Download sample CSV', 'ai-eshop-optimizer'); ?></button>
                <button type="button" class="button aieo-dmm-stock-sample" data-format="xlsx"><?php esc_html_e('Download sample XLSX', 'ai-eshop-optimizer'); ?></button>
            </p>
        </div>

        <!-- Step 3: upload -->
        <div class="aieo-dmm-card">
            <h3><?php esc_html_e('3. Upload and process', 'ai-eshop-optimizer'); ?></h3>
            <div class="aieo-dmm-upload-row">
                <input type="file" name="csv_file" accept=".csv" required>
                <button type="submit" class="button button-primary" data-action="aieo_dmm_stock_import_upload">
                    <?php esc_html_e('Upload & process', 'ai-eshop-optimizer'); ?>
                </button>
            </div>
        </div>

        <!-- Progress lives just below the upload card; hidden until a run starts. -->
        <div class="aieo-dmm-progress" id="aieo-dmm-stock-progress" style="display:none;">
            <div class="aieo-dmm-progress-bar"><span style="width:0%"></span></div>
            <p class="aieo-dmm-progress-text"></p>
            <pre class="aieo-dmm-progress-log"></pre>
        </div>
    </form>
</div>

<div class="aieo-dmm-section">
    <h3><?php esc_html_e('Recent imports', 'ai-eshop-optimizer'); ?></h3>
    <?php if (empty($recent_jobs)) : ?>
        <p class="description"><?php esc_html_e('No imports yet.', 'ai-eshop-optimizer'); ?></p>
    <?php else : ?>
        <table class="widefat striped aieo-table">
            <thead>
                <tr>
                    <th><?php esc_html_e('Created', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Status', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Processed', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Updated', 'ai-eshop-optimizer'); ?></th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($recent_jobs as $job) :
                    $params  = json_decode((string) ($job['params'] ?? '{}'), true);
                    $updated = is_array($params) && isset($params['updated']) ? (int) $params['updated'] : 0;
                ?>
                    <tr data-job-id="<?php echo esc_attr($job['job_id']); ?>">
                        <td><?php echo esc_html((string) $job['created_at']); ?></td>
                        <td><?php echo esc_html((string) $job['status']); ?></td>
                        <td><?php echo (int) $job['processed']; ?> / <?php echo (int) $job['total']; ?></td>
                        <td><?php echo (int) $updated; ?></td>
                        <td>
                            <button type="button" class="button button-small aieo-dmm-delete-export-job">
                                <?php esc_html_e('Delete', 'ai-eshop-optimizer'); ?>
                            </button>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>
