<?php
/** Finance & Procurement — overview hub. */
if (session_status() === PHP_SESSION_NONE) session_start();
if (!isset($_SESSION['user_id'])) { header('Location: ../index.php'); exit; }

require_once '../config.php';
requireModule('finance_suite');
require_once __DIR__ . '/inc/finance_lib.php';
require_once __DIR__ . '/inc/layout.php';

$conn = getDBConnection();
finance_ensure_tables($conn);
$base = defined('BASE_URL') ? rtrim(BASE_URL, '/') : '';

// Headline numbers.
$pending   = (int) ($conn->query("SELECT COUNT(*) c FROM expense_requests WHERE status='pending'")->fetch_assoc()['c'] ?? 0);
$approved  = (int) ($conn->query("SELECT COUNT(*) c FROM expense_requests WHERE status='approved'")->fetch_assoc()['c'] ?? 0);
$disbursed = (float) ($conn->query("SELECT COALESCE(SUM(amount),0) s FROM expense_requests WHERE status='disbursed'")->fetch_assoc()['s'] ?? 0);
$events    = (int) ($conn->query("SELECT COUNT(*) c FROM event_accounts WHERE status!='closed'")->fetch_assoc()['c'] ?? 0);
$receipts  = (int) ($conn->query("SELECT COUNT(*) c FROM expense_attachments")->fetch_assoc()['c'] ?? 0);

finance_page_header('Finance & Procurement', 'index');
?>
<div class="row g-3 mb-4">
    <div class="col-6 col-lg-3"><div class="fin-stat"><div class="lbl">Awaiting approval</div><div class="val"><?= $pending ?></div></div></div>
    <div class="col-6 col-lg-3"><div class="fin-stat"><div class="lbl">Ready to disburse</div><div class="val"><?= $approved ?></div></div></div>
    <div class="col-6 col-lg-3"><div class="fin-stat"><div class="lbl">Disbursed total</div><div class="val"><?= formatCurrency($disbursed) ?></div></div></div>
    <div class="col-6 col-lg-3"><div class="fin-stat"><div class="lbl">Open events</div><div class="val"><?= $events ?></div></div></div>
</div>

<div class="row g-3">
    <div class="col-md-4">
        <a href="<?= $base ?>/finance/requests.php" class="text-decoration-none">
            <div class="fin-card p-4 h-100">
                <div class="d-flex align-items-center gap-3 mb-2">
                    <i class="fas fa-file-invoice fa-lg text-success"></i><h5 class="mb-0 text-dark">Expenditure Requests</h5>
                </div>
                <p class="text-muted small mb-0">Submit purchase/repair/event spending requests and track them through the approval chain.</p>
            </div>
        </a>
    </div>
    <div class="col-md-4">
        <a href="<?= $base ?>/finance/approvals.php" class="text-decoration-none">
            <div class="fin-card p-4 h-100">
                <div class="d-flex align-items-center gap-3 mb-2">
                    <i class="fas fa-clipboard-check fa-lg text-success"></i><h5 class="mb-0 text-dark">Approvals Inbox</h5>
                    <?php if ($pending): ?><span class="badge bg-danger rounded-pill ms-auto"><?= $pending ?></span><?php endif; ?>
                </div>
                <p class="text-muted small mb-0">Review, approve or reject requests awaiting your role, then disburse to the ledger.</p>
            </div>
        </a>
    </div>
    <div class="col-md-4">
        <a href="<?= $base ?>/finance/events.php" class="text-decoration-none">
            <div class="fin-card p-4 h-100">
                <div class="d-flex align-items-center gap-3 mb-2">
                    <i class="fas fa-bus fa-lg text-success"></i><h5 class="mb-0 text-dark">Events &amp; Excursions</h5>
                </div>
                <p class="text-muted small mb-0">Track field-trip and project costs in dedicated accounts, and bill participating students.</p>
            </div>
        </a>
    </div>
</div>

<div class="fin-card p-4 mt-3 d-flex flex-wrap align-items-center gap-3">
    <div class="flex-grow-1">
        <strong>Digital receipts:</strong>
        <span class="text-muted small"><?= $receipts ?> receipt<?= $receipts === 1 ? '' : 's' ?> stored. Attach photos/scans to any request for visual auditing.</span>
    </div>
    <?php if (in_array($_SESSION['role'] ?? '', ['admin','headteacher','director'], true)): ?>
    <a href="<?= $base ?>/finance/settings.php" class="btn btn-outline-success btn-sm"><i class="fas fa-sliders me-1"></i>Configure workflow</a>
    <?php endif; ?>
</div>
<?php finance_page_footer(); ?>
