<?php
$statusColors = [
    'pending'=>'warning','confirmed'=>'info','cooking'=>'danger','ready'=>'success',
    'picked_up'=>'primary','on_the_way'=>'warning','delivered'=>'success','cancelled'=>'secondary',
];
$statusLabels = [
    'pending'=>'Pending','confirmed'=>'Confirmed','cooking'=>'Cooking','ready'=>'Ready',
    'picked_up'=>'Picked Up','on_the_way'=>'On the Way','delivered'=>'Delivered','cancelled'=>'Cancelled',
];
?>
<div class="page-header">
    <div>
        <h1><i class="bi bi-receipt-cutoff me-2"></i>Order-wise Report</h1>
        <p>Invoice and order details for the selected period.</p>
    </div>
    <div class="d-flex gap-2">
        <a href="<?= url('reports/sales') ?>" class="btn btn-outline-secondary btn-sm">Item Sales</a>
        <a href="<?= url('reports/customers') ?>" class="btn btn-outline-secondary btn-sm">Customer History</a>
    </div>
</div>

<?php include __DIR__ . '/_summary_cards.php'; ?>

<?php
$formAction = url('reports/invoices');
$showStatus = true;
$statusVal  = $status;
include __DIR__ . '/_filters.php';
?>

<div class="card">
    <div class="card-header">
        <i class="bi bi-table text-accent"></i>
        <h6 class="card-title">Orders</h6>
        <span class="ms-auto text-secondary" style="font-size:.8rem;"><?= count($rows) ?> records</span>
    </div>
    <div class="card-body p-0">
        <?php if (empty($rows)): ?>
        <div class="text-center py-5 text-secondary">
            <i class="bi bi-receipt fs-1 d-block mb-3 opacity-25"></i>
            <p>No orders found for this period.</p>
        </div>
        <?php else: ?>
        <div class="table-responsive">
            <table class="table mb-0" style="font-size:.85rem;">
                <thead>
                    <tr>
                        <th>Order #</th>
                        <th>Invoice #</th>
                        <th>Customer</th>
                        <th>Phone</th>
                        <th class="text-center">Status</th>
                        <th>Payment</th>
                        <th class="text-end">Subtotal</th>
                        <th class="text-end">Discount</th>
                        <th class="text-end">Total</th>
                        <th>Date</th>
                        <th class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($rows as $r): ?>
                    <tr>
                        <td>
                            <a href="<?= url('orders/'.$r['id']) ?>" class="fw-700 text-accent text-decoration-none">
                                <?= e($r['order_number']) ?>
                            </a>
                        </td>
                        <td class="text-secondary" style="font-size:.78rem;">
                            <?= e($r['invoice_number'] ?? '—') ?>
                        </td>
                        <td class="fw-600"><?= e($r['customer_name']) ?></td>
                        <td class="text-secondary"><?= e($r['customer_phone'] ?? '') ?></td>
                        <td class="text-center">
                            <span class="badge bg-<?= $statusColors[$r['status']] ?? 'secondary' ?>" style="font-size:.7rem;">
                                <?= $statusLabels[$r['status']] ?? ucfirst($r['status']) ?>
                            </span>
                        </td>
                        <td><?= ucfirst($r['payment_method']) ?></td>
                        <td class="text-end"><?= money((float)$r['subtotal']) ?></td>
                        <td class="text-end text-success">
                            <?= $r['discount_amount'] > 0 ? '-'.money((float)$r['discount_amount']) : '—' ?>
                        </td>
                        <td class="text-end fw-700 text-accent"><?= money((float)$r['total_amount']) ?></td>
                        <td class="text-secondary" style="font-size:.78rem;white-space:nowrap;">
                            <?= date('d M Y H:i', strtotime($r['placed_at'])) ?>
                        </td>
                        <td class="text-center">
                            <div class="d-flex gap-1 justify-content-center">
                                <a href="<?= url('orders/'.$r['id']) ?>"
                                   class="btn btn-sm btn-outline-primary" title="View Order">
                                    <i class="bi bi-eye"></i>
                                </a>
                                <a href="<?= url('orders/'.$r['id'].'/invoice') ?>"
                                   class="btn btn-sm btn-outline-secondary" title="Invoice" target="_blank">
                                    <i class="bi bi-printer"></i>
                                </a>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
                <tfoot>
                    <tr class="table-light fw-700">
                        <td colspan="8" class="text-end">Grand Total</td>
                        <td class="text-end text-accent"><?= money((float)array_sum(array_column($rows,'total_amount'))) ?></td>
                        <td colspan="2"></td>
                    </tr>
                </tfoot>
            </table>
        </div>
        <?php endif; ?>
    </div>
</div>
