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

$client     = new Fusion_WooDoo_API_Client();
$result     = $client->request('/woo/api/delivery/list', ['customer_id' => get_current_user_id()]);
$deliveries = $result['success'] ? ($result['data']['deliveries'] ?? []) : [];

// Also pull locally stored delivery PDFs
$wc_orders = wc_get_orders([
    'customer' => get_current_user_id(),
    'limit'    => 50,
    'meta_key' => '_odoo_delivery_pdf',
]);
?>
<div class="fusion-woodoo-portal">
    <h2><?php esc_html_e('Deliveries', 'fusion-woodoo'); ?></h2>

    <?php if (empty($deliveries) && empty($wc_orders)): ?>
        <p class="fusion-woodoo-empty"><?php esc_html_e('No deliveries found.', 'fusion-woodoo'); ?></p>
    <?php else: ?>
        <table class="fusion-woodoo-table">
            <thead>
                <tr>
                    <th><?php esc_html_e('Reference', 'fusion-woodoo'); ?></th>
                    <th><?php esc_html_e('Order', 'fusion-woodoo'); ?></th>
                    <th><?php esc_html_e('Scheduled Date', 'fusion-woodoo'); ?></th>
                    <th><?php esc_html_e('Carrier', 'fusion-woodoo'); ?></th>
                    <th><?php esc_html_e('Tracking', 'fusion-woodoo'); ?></th>
                    <th><?php esc_html_e('Status', 'fusion-woodoo'); ?></th>
                    <th><?php esc_html_e('PDF', 'fusion-woodoo'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($deliveries as $del): ?>
                    <tr>
                        <td><?php echo esc_html($del['name'] ?? '—'); ?></td>
                        <td><?php echo esc_html($del['wc_order_id'] ? '#' . $del['wc_order_id'] : '—'); ?></td>
                        <td><?php echo esc_html($del['scheduled_date'] ?? '—'); ?></td>
                        <td><?php echo esc_html($del['carrier'] ?? '—'); ?></td>
                        <td>
                            <?php if (!empty($del['tracking_number'])): ?>
                                <?php if (!empty($del['tracking_url'])): ?>
                                    <a href="<?php echo esc_url($del['tracking_url']); ?>" target="_blank" rel="noopener">
                                        <?php echo esc_html($del['tracking_number']); ?>
                                    </a>
                                <?php else: ?>
                                    <?php echo esc_html($del['tracking_number']); ?>
                                <?php endif; ?>
                            <?php else: ?>
                                <span class="fusion-woodoo-muted"><?php esc_html_e('N/A', 'fusion-woodoo'); ?></span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <span class="fusion-woodoo-badge fusion-woodoo-badge--<?php echo esc_attr($del['state'] ?? 'draft'); ?>">
                                <?php echo esc_html(ucfirst($del['state'] ?? '—')); ?>
                            </span>
                        </td>
                        <td>
                            <?php if (!empty($del['wc_order_id'])): ?>
                                <a href="<?php echo esc_url(add_query_arg([
                                    'action'   => 'fusion_woodoo_download_pdf',
                                    'type'     => 'delivery',
                                    'order_id' => (int) $del['wc_order_id'],
                                    'nonce'    => wp_create_nonce('fusion_woodoo_nonce'),
                                ], admin_url('admin-ajax.php'))); ?>" class="button fusion-woodoo-btn-pdf">
                                    <?php esc_html_e('Download PDF', 'fusion-woodoo'); ?>
                                </a>
                            <?php else: ?>
                                <span class="fusion-woodoo-muted">—</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>

                <?php foreach ($wc_orders as $order):
                    $tracking_number  = $order->get_meta('_odoo_tracking_number');
                    $shipping_carrier = $order->get_meta('_odoo_shipping_carrier');
                ?>
                    <tr>
                        <td><?php esc_html_e('Delivery', 'fusion-woodoo'); ?></td>
                        <td><?php echo esc_html('#' . $order->get_order_number()); ?></td>
                        <td><?php echo esc_html($order->get_date_completed() ? $order->get_date_completed()->date_i18n('Y-m-d') : '—'); ?></td>
                        <td><?php echo esc_html($shipping_carrier ?: '—'); ?></td>
                        <td><?php echo esc_html($tracking_number ?: '—'); ?></td>
                        <td><span class="fusion-woodoo-badge"><?php echo esc_html(ucfirst($order->get_status())); ?></span></td>
                        <td>
                            <?php if (!empty($order->get_meta('_odoo_delivery_pdf'))): ?>
                                <a href="<?php echo esc_url(add_query_arg([
                                    'action'   => 'fusion_woodoo_download_pdf',
                                    'type'     => 'delivery',
                                    'order_id' => $order->get_id(),
                                    'nonce'    => wp_create_nonce('fusion_woodoo_nonce'),
                                ], admin_url('admin-ajax.php'))); ?>" class="button fusion-woodoo-btn-pdf">
                                    <?php esc_html_e('Download PDF', 'fusion-woodoo'); ?>
                                </a>
                            <?php else: ?>
                                <span class="fusion-woodoo-muted">—</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>
