<?php $this->layout('main'); ?>

<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>Welcome back, <?= htmlspecialchars(trim(($user['first_name'] ?? '') . ' ' . ($user['last_name'] ?? '')) ?: 'Admin') ?>!</h2>
        <span class="text-muted"><?= date('l, F j, Y') ?></span>
    </div>

    <!-- Row 1: Stats -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card bg-primary text-white h-100">
                <div class="card-body">
                    <h5 class="card-title">Total Projects</h5>
                    <h3 class="card-text"><?= $total_projects ?? 0 ?></h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-success text-white h-100">
                <div class="card-body">
                    <h5 class="card-title">Active Projects</h5>
                    <h3 class="card-text"><?= $active_projects ?? 0 ?></h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-info text-white h-100">
                <div class="card-body">
                    <h5 class="card-title">Total Contractors</h5>
                    <h3 class="card-text"><?= $total_contractors ?? 0 ?></h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-warning text-dark h-100">
                <div class="card-body">
                    <h5 class="card-title">Total Invoice Amount</h5>
                    <h3 class="card-text"><?= formatCurrency((float)($total_invoice_amount ?? 0), $org_currency ?? DEFAULT_CURRENCY) ?></h3>
                </div>
            </div>
        </div>
    </div>

    <!-- Row 2: Charts -->
    <div class="row mb-4">
        <div class="col-md-4">
            <div class="card h-100">
                <div class="card-header">Projects by Status</div>
                <div class="card-body">
                    <canvas id="statusChart"></canvas>
                </div>
            </div>
        </div>
        <div class="col-md-8">
            <div class="card h-100">
                <div class="card-header">Monthly Project Value (Last 6 Months)</div>
                <div class="card-body">
                    <canvas id="monthlyValueChart"></canvas>
                </div>
            </div>
        </div>
    </div>

    <!-- Row 3: Lists -->
    <div class="row mb-4">
        <div class="col-md-4">
            <div class="card h-100">
                <div class="card-header">Recent Projects</div>
                <ul class="list-group list-group-flush">
                    <?php foreach ($recent_projects ?? [] as $project): ?>
                        <li class="list-group-item d-flex justify-content-between align-items-center">
                            <?= htmlspecialchars($project['name']) ?>
                            <span class="badge bg-secondary rounded-pill"><?= htmlspecialchars($project['status']) ?></span>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card h-100">
                <div class="card-header">Pending Invoice Approvals</div>
                <ul class="list-group list-group-flush">
                    <?php foreach ($pending_invoices ?? [] as $invoice): ?>
                        <li class="list-group-item">
                            Invoice #<?= htmlspecialchars($invoice['id']) ?> - <?= formatCurrency((float)$invoice['total_amount'], $invoice['currency'] ?? DEFAULT_CURRENCY) ?>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card h-100">
                <div class="card-header">Upcoming Milestones (30 days)</div>
                <ul class="list-group list-group-flush">
                    <?php foreach ($upcoming_milestones ?? [] as $milestone): ?>
                        <li class="list-group-item">
                            <?= htmlspecialchars($milestone['title']) ?> 
                            <br><small class="text-muted">Due: <?= htmlspecialchars($milestone['due_date']) ?></small>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>
    </div>

    <!-- Row 4: Budget & Activity -->
    <div class="row">
        <div class="col-md-6">
            <div class="card h-100">
                <div class="card-header">Budget vs Actual (Top 5)</div>
                <div class="card-body">
                    <canvas id="budgetChart"></canvas>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="card h-100">
                <div class="card-header">Recent Activity</div>
                <ul class="list-group list-group-flush">
                    <?php foreach ($recent_activity ?? [] as $activity): ?>
                        <li class="list-group-item">
                            <?= htmlspecialchars($activity['description']) ?>
                            <small class="float-end text-muted"><?= htmlspecialchars($activity['time_ago']) ?></small>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
    // Initialize charts here using apiStats() data in a real scenario
</script>
