<?php
if (!defined('ABSPATH')) exit;

$returns_handler = new Fusion_WooDoo_Returns();
$customer_orders = $returns_handler->get_customer_orders();
$existing        = $returns_handler->get_existing_returns();
?>
<div class="fusion-woodoo-portal fusion-woodoo-returns">
    <h2><?php esc_html_e('Returns', 'fusion-woodoo'); ?></h2>

    <?php if (!empty($existing)): ?>
        <h3><?php esc_html_e('Your Return Requests', 'fusion-woodoo'); ?></h3>
        <table class="fusion-woodoo-table">
            <thead>
                <tr>
                    <th><?php esc_html_e('Return #', 'fusion-woodoo'); ?></th>
                    <th><?php esc_html_e('Order', 'fusion-woodoo'); ?></th>
                    <th><?php esc_html_e('Date', 'fusion-woodoo'); ?></th>
                    <th><?php esc_html_e('Reason', 'fusion-woodoo'); ?></th>
                    <th><?php esc_html_e('Status', 'fusion-woodoo'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($existing as $ret): ?>
                    <tr>
                        <td><?php echo esc_html($ret['name'] ?? '—'); ?></td>
                        <td><?php echo esc_html($ret['wc_order_id'] ? '#' . $ret['wc_order_id'] : '—'); ?></td>
                        <td><?php echo esc_html($ret['date'] ?? '—'); ?></td>
                        <td><?php echo esc_html($ret['reason'] ?? '—'); ?></td>
                        <td>
                            <span class="fusion-woodoo-badge fusion-woodoo-badge--<?php echo esc_attr($ret['state'] ?? 'pending'); ?>">
                                <?php echo esc_html(ucfirst($ret['state'] ?? 'Pending')); ?>
                            </span>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>

    <h3><?php esc_html_e('Submit a New Return', 'fusion-woodoo'); ?></h3>

    <?php if (empty($customer_orders)): ?>
        <p class="fusion-woodoo-empty"><?php esc_html_e('No eligible orders found for return.', 'fusion-woodoo'); ?></p>
    <?php else: ?>
        <div id="fusion-woodoo-return-notice" style="display:none;" class="fusion-woodoo-notice"></div>

        <form id="fusion-woodoo-return-form" class="fusion-woodoo-form">
            <?php wp_nonce_field('fusion_woodoo_nonce', 'nonce'); ?>

            <div class="fusion-woodoo-form-row">
                <label for="fw-order-select"><?php esc_html_e('Select Order', 'fusion-woodoo'); ?></label>
                <select id="fw-order-select" name="order_id" required>
                    <option value=""><?php esc_html_e('— Choose an order —', 'fusion-woodoo'); ?></option>
                    <?php foreach ($customer_orders as $order): ?>
                        <option value="<?php echo esc_attr($order->get_id()); ?>">
                            #<?php echo esc_html($order->get_order_number()); ?>
                            — <?php echo esc_html($order->get_date_created() ? $order->get_date_created()->date_i18n('Y-m-d') : ''); ?>
                            (<?php echo '$' . number_format((float) $order->get_total(), 2); ?>)
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div id="fw-items-container" class="fusion-woodoo-form-row" style="display:none;">
                <label><?php esc_html_e('Select Items to Return', 'fusion-woodoo'); ?></label>
                <div id="fw-items-list"></div>
            </div>

            <div class="fusion-woodoo-form-row">
                <label for="fw-return-reason"><?php esc_html_e('Reason for Return', 'fusion-woodoo'); ?></label>
                <textarea id="fw-return-reason" name="reason" rows="4" required placeholder="<?php esc_attr_e('Please describe why you are returning these items.', 'fusion-woodoo'); ?>"></textarea>
            </div>

            <button type="submit" class="button button-primary">
                <?php esc_html_e('Submit Return Request', 'fusion-woodoo'); ?>
            </button>
        </form>

        <script type="application/json" id="fw-orders-data">
            <?php
            $orders_data = [];
            foreach ($customer_orders as $order) {
                $items = [];
                foreach ($order->get_items() as $item) {
                    $items[] = [
                        'product_id'   => $item->get_product_id(),
                        'variation_id' => $item->get_variation_id(),
                        'name'         => $item->get_name(),
                        'qty'          => $item->get_quantity(),
                    ];
                }
                $orders_data[$order->get_id()] = $items;
            }
            echo wp_json_encode($orders_data);
            ?>
        </script>
    <?php endif; ?>
</div>
