<?php
/**
 * @var object $invoices
 * @var string $dateFrom
 * @var string $dateTo
 * @var string $status
 * @var string $customerId
 * @var string $search
 * @var array $customers
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-file-invoice me-2 text-gold"></i>Invoices</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title">All Invoices</h6>
        </div>
        <div class="card-header-right">
            <a href="<?= url('pos') ?>" class="btn btn-sm btn-gold">
                <i class="fas fa-plus me-1"></i>New Sale
            </a>
        </div>
    </div>
    <div class="card-body">
        <form method="GET" action="<?= url('invoices') ?>" class="row g-2 mb-3">
            <div class="col-md-2">
                <label class="form-label small">Date From</label>
                <input type="date" name="date_from" class="form-control form-control-sm bg-transparent"
                       value="<?= e($dateFrom) ?>">
            </div>
            <div class="col-md-2">
                <label class="form-label small">Date To</label>
                <input type="date" name="date_to" class="form-control form-control-sm bg-transparent"
                       value="<?= e($dateTo) ?>">
            </div>
            <div class="col-md-2">
                <label class="form-label small">Status</label>
                <select name="status" class="form-select form-select-sm bg-transparent">
                    <option value="">All Status</option>
                    <?php foreach (paymentStatuses() as $val => $label): ?>
                        <option value="<?= $val ?>" <?= $status === $val ? 'selected' : '' ?>><?= e($label) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label small">Customer</label>
                <select name="customer_id" class="form-select form-select-sm bg-transparent">
                    <option value="">All Customers</option>
                    <?php foreach ($customers as $c): ?>
                        <option value="<?= $c->id ?>" <?= $customerId == $c->id ? 'selected' : '' ?>>
                            <?= e($c->first_name . ' ' . $c->last_name) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label small">Search</label>
                <input type="text" name="search" class="form-control form-control-sm bg-transparent"
                       placeholder="Invoice # or name..." value="<?= e($search) ?>">
            </div>
            <div class="col-md-2 d-flex align-items-end gap-1">
                <button type="submit" class="btn btn-sm btn-gold"><i class="fas fa-filter me-1"></i>Filter</button>
                <a href="<?= url('invoices') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-times"></i></a>
            </div>
        </form>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th>Invoice #</th>
                        <th>Customer</th>
                        <th>Date</th>
                        <th class="text-end">Total</th>
                        <th class="text-end">Paid</th>
                        <th class="text-end">Balance</th>
                        <th class="text-center">Status</th>
                        <th width="120" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($invoices->data)): ?>
                        <tr>
                            <td colspan="8" class="text-center text-muted py-4">
                                <i class="fas fa-file-invoice fa-2x mb-2"></i>
                                <p class="mb-0">No invoices found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($invoices->data as $inv): ?>
                            <tr>
                                <td>
                                    <a href="<?= url('pos/invoice/' . $inv->id) ?>" class="text-gold text-decoration-none">
                                        <strong><?= e($inv->invoice_number) ?></strong>
                                    </a>
                                </td>
                                <td>
                                    <?php if ($inv->customer_id): ?>
                                        <?= e($inv->customer_first_name . ' ' . $inv->customer_last_name) ?>
                                        <br><small class="text-muted"><?= e($inv->customer_phone ?? '') ?></small>
                                    <?php else: ?>
                                        <span class="text-muted">Walk-in</span>
                                    <?php endif; ?>
                                </td>
                                <td><?= formatDate($inv->created_at) ?></td>
                                <td class="text-end"><?= currency($inv->total) ?></td>
                                <td class="text-end"><?= currency($inv->paid_amount) ?></td>
                                <td class="text-end"><?= currency($inv->balance_amount) ?></td>
                                <td class="text-center">
                                    <span class="badge bg-<?= statusBadge($inv->payment_status) ?>">
                                        <?= e(ucfirst($inv->payment_status)) ?>
                                    </span>
                                </td>
                                <td class="text-center">
                                    <div class="btn-group btn-group-sm">
                                        <a href="<?= url('pos/invoice/' . $inv->id) ?>" class="btn btn-outline-info" title="View">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <a href="<?= url('pos/invoice/' . $inv->id . '/print') ?>" class="btn btn-outline-gold" title="Print" target="_blank">
                                            <i class="fas fa-print"></i>
                                        </a>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <?php if ($invoices->totalPages > 1): ?>
            <div class="card-footer d-flex justify-content-between align-items-center">
                <div class="text-muted small">
                    Showing <?= (($invoices->currentPage - 1) * $invoices->perPage) + 1 ?>
                    to <?= min($invoices->currentPage * $invoices->perPage, $invoices->total) ?>
                    of <?= $invoices->total ?> entries
                </div>
                <?= paginateLinks($invoices, url('invoices')) ?>
            </div>
        <?php endif; ?>
    </div>
</div>
