<?php
/**
 * @var string $startDate
 * @var string $endDate
 * @var array $purchases
 * @var array $suppliers
 * @var int|null $supplierId
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-truck me-2 text-gold"></i>Purchase Order Report</h5>
</div>

<div class="glass-card p-3 mb-4">
    <form class="row g-3 align-items-end" method="GET" action="<?= url('reports/purchases') ?>">
        <div class="col-md-3">
            <label class="form-label">Start Date</label>
            <input type="date" name="start_date" class="form-control" value="<?= e($startDate) ?>">
        </div>
        <div class="col-md-3">
            <label class="form-label">End Date</label>
            <input type="date" name="end_date" class="form-control" value="<?= e($endDate) ?>">
        </div>
        <div class="col-md-2">
            <label class="form-label">Supplier</label>
            <select name="supplier_id" class="form-select">
                <option value="">All</option>
                <?php foreach ($suppliers as $s): ?>
                    <option value="<?= $s->id ?>" <?= $supplierId == $s->id ? 'selected' : '' ?>><?= e($s->name) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-4">
            <button type="submit" class="btn btn-gold"><i class="fas fa-search me-1"></i>Generate</button>
            <a href="<?= url('reports/export/purchases?start_date=' . $startDate . '&end_date=' . $endDate . '&supplier_id=' . ($supplierId ?? '')) ?>" class="btn btn-outline-gold ms-2">
                <i class="fas fa-download me-1"></i>CSV
            </a>
        </div>
    </form>
</div>

<?php
$totalPurchases = array_sum(array_column($purchases, 'total'));
$totalItems = array_sum(array_column($purchases, 'total_items'));
$pendingCount = count(array_filter($purchases, fn($p) => $p->status === 'pending'));
$receivedCount = count(array_filter($purchases, fn($p) => $p->status === 'received'));
?>
<div class="row g-4 mb-4">
    <div class="col-md-3">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:rgba(13,202,240,0.15);border-color:rgba(13,202,240,0.3);color:#0dcaf0;"><i class="fas fa-file-invoice"></i></div>
            <div class="stats-info">
                <span class="stats-label">Total Orders</span>
                <span class="stats-value"><?= count($purchases) ?></span>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:rgba(200,164,78,0.15);border-color:rgba(200,164,78,0.3);color:var(--gold-primary);"><i class="fas fa-cubes"></i></div>
            <div class="stats-info">
                <span class="stats-label">Total Items</span>
                <span class="stats-value"><?= $totalItems ?></span>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:rgba(25,135,84,0.15);border-color:rgba(25,135,84,0.3);color:#198754;"><i class="fas fa-dollar-sign"></i></div>
            <div class="stats-info">
                <span class="stats-label">Total Amount</span>
                <span class="stats-value"><?= currency($totalPurchases) ?></span>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:rgba(13,110,253,0.15);border-color:rgba(13,110,253,0.3);color:#0d6efd;"><i class="fas fa-check-circle"></i></div>
            <div class="stats-info">
                <span class="stats-label">Received</span>
                <span class="stats-value"><?= $receivedCount ?></span>
            </div>
        </div>
    </div>
</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">Purchase Orders</h6>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table table-dark table-hover mb-0">
            <thead>
                <tr>
                    <th>PO #</th>
                    <th>Supplier</th>
                    <th>Order Date</th>
                    <th class="text-center">Items</th>
                    <th class="text-end">Total</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($purchases)): ?>
                    <tr><td colspan="6" class="text-center text-muted py-4">No purchase orders found</td></tr>
                <?php else: ?>
                    <?php foreach ($purchases as $po): ?>
                        <tr>
                            <td><a href="<?= url('purchases/view/' . $po->id) ?>" class="text-gold"><?= e($po->po_number) ?></a></td>
                            <td><?= e($po->supplier_name ?? '-') ?></td>
                            <td><?= formatDate($po->order_date ?? '') ?></td>
                            <td class="text-center"><?= $po->total_items ?? 0 ?></td>
                            <td class="text-end"><?= currency($po->total ?? 0) ?></td>
                            <td><span class="badge bg-<?= statusBadge($po->status ?? '') ?>"><?= e(ucfirst($po->status ?? '')) ?></span></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
