<?php ob_start(); ?>
<div class="container-fluid py-4">
    <div class="row mb-4">
        <div class="col-xl-3 col-sm-6 mb-xl-0 mb-4">
            <div class="card">
                <div class="card-body p-3">
                    <div class="row">
                        <div class="col-8">
                            <div class="numbers">
                                <p class="text-sm mb-0 text-capitalize font-weight-bold">Total Invoices</p>
                                <h5 class="font-weight-bolder mb-0">
                                    <?= $stats['total_invoices'] ?? 0 ?>
                                </h5>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-3 col-sm-6 mb-xl-0 mb-4">
            <div class="card">
                <div class="card-body p-3">
                    <div class="row">
                        <div class="col-8">
                            <div class="numbers">
                                <p class="text-sm mb-0 text-capitalize font-weight-bold">Pending</p>
                                <h5 class="font-weight-bolder mb-0">
                                    <?= $stats['pending'] ?? 0 ?>
                                </h5>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-3 col-sm-6 mb-xl-0 mb-4">
            <div class="card">
                <div class="card-body p-3">
                    <div class="row">
                        <div class="col-8">
                            <div class="numbers">
                                <p class="text-sm mb-0 text-capitalize font-weight-bold">Approved</p>
                                <h5 class="font-weight-bolder mb-0">
                                    <?= $stats['approved'] ?? 0 ?>
                                </h5>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-3 col-sm-6">
            <div class="card">
                <div class="card-body p-3">
                    <div class="row">
                        <div class="col-8">
                            <div class="numbers">
                                <p class="text-sm mb-0 text-capitalize font-weight-bold">Total Paid</p>
                                <h5 class="font-weight-bolder mb-0">
                                    <?= formatCurrency((float)($stats['total_paid_amount'] ?? 0)) ?>
                                </h5>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Filter Bar -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="GET" action="<?= APP_URL ?>/finance/invoices">
                <div class="row">
                    <div class="col-md-2">
                        <select name="project_id" class="form-select">
                            <option value="">All Projects</option>
                            <?php foreach ($projects ?? [] as $p): ?>
                            <option value="<?= $p['id'] ?>" <?= (string)($filters['project_id'] ?? '') === (string)$p['id'] ? 'selected' : '' ?>><?= htmlspecialchars($p['name']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <select name="contractor_id" class="form-select">
                            <option value="">All Contractors</option>
                            <?php foreach ($contractors ?? [] as $c): ?>
                            <option value="<?= $c['id'] ?>" <?= (string)($filters['contractor_id'] ?? '') === (string)$c['id'] ? 'selected' : '' ?>><?= htmlspecialchars($c['company_name']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <select name="status" class="form-select">
                            <option value="">All Statuses</option>
                            <?php foreach (['draft', 'submitted', 'under_review', 'approved', 'rejected', 'paid'] as $st): ?>
                            <option value="<?= $st ?>" <?= ($filters['status'] ?? '') === $st ? 'selected' : '' ?>><?= ucfirst(str_replace('_', ' ', $st)) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <input type="date" name="date_from" value="<?= htmlspecialchars($filters['date_from'] ?? '') ?>" class="form-control" placeholder="From Date">
                    </div>
                    <div class="col-md-2">
                        <input type="date" name="date_to" value="<?= htmlspecialchars($filters['date_to'] ?? '') ?>" class="form-control" placeholder="To Date">
                    </div>
                    <div class="col-md-2">
                        <button type="submit" class="btn btn-primary w-100">Filter</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- Invoices Table -->
    <div class="card">
        <div class="card-header pb-0 d-flex justify-content-between">
            <h6>Invoices</h6>
            <div>
                <select class="form-select d-inline w-auto me-2">
                    <option>Bulk Actions</option>
                    <option>Export CSV</option>
                </select>
                <a href="<?= APP_URL ?>/finance/invoices/create" class="btn btn-sm btn-success">Create Invoice</a>
                <a href="<?= APP_URL ?>/finance/invoices/export" class="btn btn-sm btn-info">Export All</a>
            </div>
        </div>
        <div class="card-body px-0 pt-0 pb-2">
            <div class="table-responsive p-0">
                <table class="table align-items-center mb-0">
                    <thead>
                        <tr>
                            <th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">Invoice Number</th>
                            <?php if (!empty($isPlatformView)): ?><th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">Organization</th><?php endif; ?>
                            <th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7 ps-2">Project</th>
                            <th class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">Contractor/Supplier</th>
                            <th class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">Date</th>
                            <th class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">Amount</th>
                            <th class="text-center text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">Status</th>
                            <th class="text-secondary opacity-7"></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if(!empty($invoices['data'])): ?>
                            <?php foreach($invoices['data'] as $invoice): ?>
                            <tr>
                                <td>
                                    <div class="d-flex px-2 py-1">
                                        <div class="d-flex flex-column justify-content-center">
                                            <h6 class="mb-0 text-sm"><?= htmlspecialchars($invoice['invoice_number']) ?></h6>
                                        </div>
                                    </div>
                                </td>
                                <?php if (!empty($isPlatformView)): ?><td><p class="text-xs font-weight-bold mb-0"><?= htmlspecialchars($invoice['org_name'] ?? '-') ?></p></td><?php endif; ?>
                                <td>
                                    <p class="text-xs font-weight-bold mb-0"><?= htmlspecialchars($invoice['project_name']) ?></p>
                                </td>
                                <td class="align-middle text-center text-sm">
                                    <p class="text-xs font-weight-bold mb-0"><?= htmlspecialchars($invoice['contractor_name']) ?></p>
                                </td>
                                <td class="align-middle text-center">
                                    <span class="text-secondary text-xs font-weight-bold"><?= htmlspecialchars($invoice['date'] ?? '') ?></span>
                                </td>
                                <td class="align-middle text-center">
                                    <span class="text-secondary text-xs font-weight-bold"><?= formatCurrency((float)$invoice['total'], $invoice['currency'] ?? DEFAULT_CURRENCY) ?></span>
                                </td>
                                <td class="align-middle text-center text-sm">
                                    <span class="badge badge-sm bg-gradient-<?= $invoice['status'] == 'paid' ? 'success' : 'warning' ?>"><?= ucfirst($invoice['status']) ?></span>
                                </td>
                                <td class="align-middle">
                                    <a href="<?= APP_URL ?>/finance/invoices/<?= $invoice['id'] ?>" class="text-secondary font-weight-bold text-xs" data-toggle="tooltip" data-original-title="View invoice">
                                        View
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="<?= !empty($isPlatformView) ? 8 : 7 ?>" class="text-center">No invoices found.</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<?php
$content = ob_get_clean();
require ROOT_PATH . '/modules/shared/views/layouts/main.php';
?>

