<?php

if (!defined('ABSPATH')) { exit; }
?>
<div class="aieo-dmm-section">
    <h2><?php esc_html_e('Branches', 'ai-eshop-optimizer'); ?></h2>
    <p class="description">
        <?php esc_html_e('Define the branches available to the staff scanner. Each branch points at a stock-board table in the database.', 'ai-eshop-optimizer'); ?>
    </p>

    <form id="aieo-dmm-branch-form" class="aieo-dmm-form aieo-dmm-inline-form">
        <input type="hidden" name="id" value="0">
        <div class="aieo-dmm-grid">
            <p>
                <label><?php esc_html_e('Slug', 'ai-eshop-optimizer'); ?></label>
                <input type="text" name="slug" pattern="[a-z0-9_\-]+" required>
            </p>
            <p>
                <label><?php esc_html_e('Label', 'ai-eshop-optimizer'); ?></label>
                <input type="text" name="label" required class="regular-text">
            </p>
            <p>
                <label><?php esc_html_e('Stock table', 'ai-eshop-optimizer'); ?></label>
                <input type="text" name="stock_table" required class="regular-text" placeholder="<?php echo esc_attr($GLOBALS['wpdb']->prefix . 'store_board_mybranch'); ?>">
            </p>
            <p>
                <label><?php esc_html_e('Match column', 'ai-eshop-optimizer'); ?></label>
                <select name="match_col">
                    <option value="gtin">gtin</option>
                    <option value="sku">sku</option>
                    <option value="product_id">product_id</option>
                    <option value="barcode">barcode</option>
                </select>
            </p>
            <p>
                <label><?php esc_html_e('Sort order', 'ai-eshop-optimizer'); ?></label>
                <input type="number" name="sort_order" value="0" class="small-text">
            </p>
            <p>
                <label><?php esc_html_e('SoftOne branch ID', 'ai-eshop-optimizer'); ?></label>
                <input type="number" name="soft1_id" value="" class="small-text" placeholder="100">
                <span class="description"><?php esc_html_e('Maps the order\'s `soft1_branches` ERP value to this branch. Leave empty if not synced from SoftOne.', 'ai-eshop-optimizer'); ?></span>
            </p>
            <p>
                <label><?php esc_html_e('Enabled', 'ai-eshop-optimizer'); ?></label>
                <input type="checkbox" name="enabled" value="1" checked>
            </p>
        </div>
        <p class="submit">
            <button type="submit" class="button button-primary" data-action="aieo_dmm_branch_save">
                <?php esc_html_e('Save branch', 'ai-eshop-optimizer'); ?>
            </button>
        </p>
    </form>

    <table class="widefat striped aieo-table">
        <thead>
            <tr>
                <th><?php esc_html_e('Slug', 'ai-eshop-optimizer'); ?></th>
                <th><?php esc_html_e('Label', 'ai-eshop-optimizer'); ?></th>
                <th><?php esc_html_e('Stock table', 'ai-eshop-optimizer'); ?></th>
                <th><?php esc_html_e('Match', 'ai-eshop-optimizer'); ?></th>
                <th><?php esc_html_e('SoftOne ID', 'ai-eshop-optimizer'); ?></th>
                <th><?php esc_html_e('Enabled', 'ai-eshop-optimizer'); ?></th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ((array) $branches as $b) : ?>
                <tr data-id="<?php echo (int) $b['id']; ?>">
                    <td><code><?php echo esc_html($b['slug']); ?></code></td>
                    <td><?php echo esc_html($b['label']); ?></td>
                    <td><code><?php echo esc_html($b['stock_table']); ?></code></td>
                    <td><?php echo esc_html($b['match_col']); ?></td>
                    <td><?php echo isset($b['soft1_id']) && $b['soft1_id'] !== null && $b['soft1_id'] !== ''
                            ? '<code>' . (int) $b['soft1_id'] . '</code>'
                            : '<span style="color:#9ca3af;">&mdash;</span>'; ?></td>
                    <td><?php echo !empty($b['enabled']) ? '✔' : '—'; ?></td>
                    <td>
                        <button type="button" class="button button-small aieo-dmm-delete-row" data-action="aieo_dmm_branch_delete">
                            <?php esc_html_e('Delete', 'ai-eshop-optimizer'); ?>
                        </button>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

<div class="aieo-dmm-section">
    <h3><?php esc_html_e('Shortcodes', 'ai-eshop-optimizer'); ?></h3>
    <ul>
        <li><code>[aieo_dmm_branch_scanner]</code> — <?php esc_html_e('Staff flow: pick a branch, scan a barcode, update stock (requires manage_woocommerce or edit_products).', 'ai-eshop-optimizer'); ?></li>
        <li><code>[aieo_dmm_customer_scanner]</code> — <?php esc_html_e('End-customer flow: scan a barcode, see the product card with the role-adjusted price, add to cart.', 'ai-eshop-optimizer'); ?></li>
    </ul>
</div>

<div class="aieo-dmm-section">
    <h3><?php esc_html_e('Outstanding pre-orders by supplier', 'ai-eshop-optimizer'); ?></h3>
    <p class="description"><?php esc_html_e('Aggregated from staff-recorded `desired_qty` updates. One row per supplier with the unfulfilled quantity total. Use to drive the next purchase order.', 'ai-eshop-optimizer'); ?></p>
    <?php if (empty($preorder_summary)) : ?>
        <p><?php esc_html_e('No outstanding pre-orders. Staff have either not signalled desired_qty yet, or every signal has been marked fulfilled.', 'ai-eshop-optimizer'); ?></p>
    <?php else : ?>
        <table class="widefat striped aieo-table">
            <thead>
                <tr>
                    <th><?php esc_html_e('Supplier', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Lines', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Total qty requested', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Most recent', 'ai-eshop-optimizer'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($preorder_summary as $s) : ?>
                    <tr>
                        <td><strong><?php echo esc_html((string) $s['supplier_name']); ?></strong></td>
                        <td><?php echo (int) $s['line_count']; ?></td>
                        <td><?php echo (int) $s['total_qty']; ?></td>
                        <td><?php echo esc_html((string) $s['most_recent_at']); ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>

<div class="aieo-dmm-section">
    <h3><?php esc_html_e('Recent staff pre-order signals', 'ai-eshop-optimizer'); ?></h3>
    <p class="description"><?php esc_html_e('Last 50 desired_qty updates appended by the staff scanner. Each row is one product / branch / qty signal that flows to the supplier.', 'ai-eshop-optimizer'); ?></p>
    <?php if (empty($recent_preorders)) : ?>
        <p><?php esc_html_e('No pre-order signals captured yet. Staff scans that update `desired_qty` will appear here.', 'ai-eshop-optimizer'); ?></p>
    <?php else : ?>
        <table class="widefat striped aieo-table" style="font-size:12px;">
            <thead>
                <tr>
                    <th><?php esc_html_e('When', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Branch', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Product', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Brand', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Supplier', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Had', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Want', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Status', 'ai-eshop-optimizer'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($recent_preorders as $r) : ?>
                    <tr>
                        <td><?php echo esc_html((string) $r['created_at']); ?></td>
                        <td><?php echo esc_html((string) ($r['branch_label'] ?? ('#' . (int) $r['branch_id']))); ?></td>
                        <td><?php echo esc_html((string) $r['product_title']); ?></td>
                        <td><?php echo esc_html((string) $r['brand_name']); ?></td>
                        <td><?php echo $r['supplier_name'] === '' ? '<span style="color:#9ca3af;">— unresolved —</span>' : esc_html((string) $r['supplier_name']); ?></td>
                        <td><?php echo $r['prior_existing'] !== null ? (int) $r['prior_existing'] : '—'; ?></td>
                        <td><strong><?php echo (int) $r['requested_qty']; ?></strong></td>
                        <td><?php echo (int) $r['fulfilled'] === 1 ? '✔ ' . esc_html((string) $r['fulfilled_at']) : '<span style="color:#ca8a04;">pending</span>'; ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>

<div class="aieo-dmm-section">
    <h3><?php esc_html_e('Recent scans', 'ai-eshop-optimizer'); ?></h3>
    <?php if (empty($recent_log)) : ?>
        <p><?php esc_html_e('No scans logged yet.', 'ai-eshop-optimizer'); ?></p>
    <?php else : ?>
        <table class="widefat striped aieo-table">
            <thead>
                <tr>
                    <th><?php esc_html_e('When', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Barcode', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Status', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('Branch', 'ai-eshop-optimizer'); ?></th>
                    <th><?php esc_html_e('User', 'ai-eshop-optimizer'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($recent_log as $e) : ?>
                    <tr>
                        <td><?php echo esc_html($e['created_at']); ?></td>
                        <td><code><?php echo esc_html($e['barcode']); ?></code></td>
                        <td><?php echo esc_html($e['status']); ?></td>
                        <td><?php echo (int) ($e['branch_id'] ?? 0); ?></td>
                        <td><?php echo esc_html((string) ($e['user_login'] ?? '')); ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>
