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

// Load invoices from Odoo
$client   = new Fusion_WooDoo_API_Client();
$result   = $client->request('/woo/api/invoice/list', ['customer_id' => get_current_user_id()]);
$invoices = $result['success'] ? ($result['data']['invoices'] ?? []) : [];

// Also pull locally stored invoices from WC orders
$wc_orders = wc_get_orders([
    'customer' => get_current_user_id(),
    'limit'    => 50,
    'meta_key' => '_odoo_invoice_pdf',
]);
?>
<div class="fusion-woodoo-portal">
    <h2><?php esc_html_e('Invoices', 'fusion-woodoo'); ?></h2>

    <?php if (empty($invoices) && empty($wc_orders)): ?>
        <p class="fusion-woodoo-empty"><?php esc_html_e('No invoices found.', 'fusion-woodoo'); ?></p>
    <?php else: ?>
        <table class="fusion-woodoo-table">
            <thead>
                <tr>
                    <th><?php esc_html_e('Invoice #', '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('Amount Due', '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 ($invoices as $inv): ?>
                    <tr>
                        <td><?php echo esc_html($inv['name'] ?? '—'); ?></td>
                        <td><?php echo esc_html($inv['wc_order_id'] ?? '—'); ?></td>
                        <td><?php echo esc_html($inv['invoice_date'] ?? '—'); ?></td>
                        <td><?php echo '$' . number_format((float) ($inv['amount_due'] ?? 0), 2); ?></td>
                        <td>
                            <span class="fusion-woodoo-badge fusion-woodoo-badge--<?php echo esc_attr($inv['state'] ?? 'draft'); ?>">
                                <?php echo esc_html(ucfirst($inv['state'] ?? '—')); ?>
                            </span>
                        </td>
                        <td>
                            <?php if (!empty($inv['wc_order_id'])): ?>
                                <a href="<?php echo esc_url(add_query_arg([
                                    'action'   => 'fusion_woodoo_download_pdf',
                                    'type'     => 'invoice',
                                    'order_id' => (int) $inv['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"><?php esc_html_e('Unavailable', 'fusion-woodoo'); ?></span>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>

                <?php foreach ($wc_orders as $order):
                    if (in_array($order->get_id(), array_column($invoices, 'wc_order_id'))) continue;
                    $has_pdf = !empty($order->get_meta('_odoo_invoice_pdf'));
                    if (!$has_pdf) continue;
                ?>
                    <tr>
                        <td><?php esc_html_e('Invoice', 'fusion-woodoo'); ?></td>
                        <td><?php echo esc_html('#' . $order->get_order_number()); ?></td>
                        <td><?php echo esc_html($order->get_date_created() ? $order->get_date_created()->date_i18n('Y-m-d') : '—'); ?></td>
                        <td><?php echo '$' . number_format((float) $order->get_total(), 2); ?></td>
                        <td><span class="fusion-woodoo-badge"><?php echo esc_html(ucfirst($order->get_status())); ?></span></td>
                        <td>
                            <a href="<?php echo esc_url(add_query_arg([
                                'action'   => 'fusion_woodoo_download_pdf',
                                'type'     => 'invoice',
                                '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>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>
