<?php $this->extend('layouts/main'); $this->section('content'); $title = 'Dashboard'; ?>
<?php $currency = $settings['regional']['currency'] ?? 'GBP'; $sym = ['GBP'=>'£','EUR'=>'€','USD'=>'$']['GBP'] ?? '£'; ?>

<div class="row g-3 mb-4">
    <div class="col-6 col-lg-3">
        <div class="kpi-card kpi-blue">
            <div class="kpi-value"><?= $kpis['active_jobs'] ?></div>
            <div class="kpi-label">Active Jobs</div>
        </div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="kpi-card kpi-red">
            <div class="kpi-value"><?= $sym . number_format($kpis['overdue_invoices']) ?></div>
            <div class="kpi-label">Overdue Invoices</div>
        </div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="kpi-card kpi-green">
            <div class="kpi-value"><?= $sym . number_format($kpis['revenue_month'], 2) ?></div>
            <div class="kpi-label">Revenue This Month</div>
        </div>
    </div>
    <div class="col-6 col-lg-3">
        <div class="kpi-card kpi-purple">
            <div class="kpi-value"><?= $kpis['customers_total'] ?></div>
            <div class="kpi-label">Total Customers</div>
        </div>
    </div>
</div>

<!-- Quick Actions -->
<div class="row g-2 mb-4">
    <?php if (in_array('edit_jobs', $permissions)): ?>
    <div class="col-6 col-md-3">
        <a href="/fieldly/public/index.php/jobs/create" class="btn btn-primary w-100"><i class="bi bi-plus-circle me-1"></i> New Job</a>
    </div>
    <?php endif; ?>
    <?php if (in_array('edit_customers', $permissions)): ?>
    <div class="col-6 col-md-3">
        <a href="/fieldly/public/index.php/customers/create" class="btn btn-outline-primary w-100"><i class="bi bi-person-plus me-1"></i> New Customer</a>
    </div>
    <?php endif; ?>
    <?php if (in_array('edit_quotes', $permissions)): ?>
    <div class="col-6 col-md-3">
        <a href="/fieldly/public/index.php/quotes/create" class="btn btn-outline-secondary w-100"><i class="bi bi-file-earmark-plus me-1"></i> New Quote</a>
    </div>
    <?php endif; ?>
    <?php if (in_array('edit_invoices', $permissions)): ?>
    <div class="col-6 col-md-3">
        <a href="/fieldly/public/index.php/invoices/create" class="btn btn-outline-secondary w-100"><i class="bi bi-receipt me-1"></i> New Invoice</a>
    </div>
    <?php endif; ?>
</div>

<div class="row g-4">
    <!-- Overdue Jobs -->
    <div class="col-lg-6">
        <div class="card h-100">
            <div class="card-header">
                <h6><i class="bi bi-exclamation-triangle-fill text-warning me-2"></i>Overdue Jobs</h6>
                <a href="/fieldly/public/index.php/jobs" class="btn btn-sm btn-outline-secondary">View All</a>
            </div>
            <div class="card-body p-0">
                <?php if (empty($overdueJobs)): ?>
                    <div class="text-center text-muted py-4" style="font-size:14px"><i class="bi bi-check-circle-fill text-success d-block mb-2" style="font-size:24px"></i>No overdue jobs</div>
                <?php else: ?>
                    <table class="table table-sm mb-0">
                        <tbody>
                        <?php foreach (array_slice($overdueJobs,0,8) as $j): ?>
                            <tr>
                                <td>
                                    <a href="/fieldly/public/index.php/jobs/<?= $j['id'] ?>" class="fw-semibold text-decoration-none"><?= esc($j['title']) ?></a>
                                    <div class="text-muted small"><?= esc($j['company_name'] ?: $j['first_name'].' '.$j['last_name']) ?></div>
                                </td>
                                <td class="text-end">
                                    <span class="badge" style="background:<?= esc($j['status_color']) ?>"><?= esc($j['status_name']) ?></span>
                                    <div class="text-danger small"><?= esc($j['scheduled_date']) ?></div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Upcoming Visits -->
    <div class="col-lg-6">
        <div class="card h-100">
            <div class="card-header">
                <h6><i class="bi bi-calendar-check-fill text-primary me-2"></i>Upcoming Visits (7 days)</h6>
                <a href="/fieldly/public/index.php/calendar" class="btn btn-sm btn-outline-secondary">Calendar</a>
            </div>
            <div class="card-body p-0">
                <?php if (empty($upcomingVisits)): ?>
                    <div class="text-center text-muted py-4" style="font-size:14px"><i class="bi bi-calendar3 d-block mb-2" style="font-size:24px"></i>No upcoming visits</div>
                <?php else: ?>
                    <table class="table table-sm mb-0">
                        <tbody>
                        <?php foreach ($upcomingVisits as $v): ?>
                            <tr>
                                <td>
                                    <a href="/fieldly/public/index.php/jobs/<?= $v['job_id'] ?>" class="fw-semibold text-decoration-none"><?= esc($v['job_title']) ?></a>
                                    <div class="text-muted small"><?= esc($v['company_name'] ?: $v['first_name'].' '.$v['last_name']) ?></div>
                                </td>
                                <td class="text-end">
                                    <div class="small fw-semibold"><?= date('D d M', strtotime($v['scheduled_start'])) ?></div>
                                    <div class="text-muted small"><?= date('H:i', strtotime($v['scheduled_start'])) ?></div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Revenue Chart -->
    <div class="col-12">
        <div class="card">
            <div class="card-header"><h6><i class="bi bi-graph-up-arrow text-success me-2"></i>Monthly Revenue (Last 12 Months)</h6></div>
            <div class="card-body">
                <canvas id="revenueChart" height="80"></canvas>
            </div>
        </div>
    </div>

    <!-- Recent Activity -->
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h6><i class="bi bi-activity text-primary me-2"></i>Recent Activity</h6>
                <a href="/fieldly/public/index.php/activity" class="btn btn-sm btn-outline-secondary">View All</a>
            </div>
            <div class="card-body p-0">
                <?php if (empty($activity)): ?>
                    <div class="text-center text-muted py-4">No activity yet</div>
                <?php else: ?>
                    <table class="table table-sm mb-0">
                        <tbody>
                        <?php foreach ($activity as $a): ?>
                            <tr>
                                <td style="width:32px">
                                    <?php
                                    $icon = match(true) {
                                        str_contains($a['type'],'invoice')  => 'receipt text-primary',
                                        str_contains($a['type'],'payment')  => 'credit-card text-success',
                                        str_contains($a['type'],'job')      => 'briefcase text-warning',
                                        str_contains($a['type'],'quote')    => 'file-earmark-text text-info',
                                        str_contains($a['type'],'customer') => 'person text-secondary',
                                        default => 'dot text-muted'
                                    };
                                    ?>
                                    <i class="bi bi-<?= $icon ?>"></i>
                                </td>
                                <td style="font-size:13px"><?= esc($a['description']) ?></td>
                                <td class="text-end text-muted" style="font-size:12px;white-space:nowrap"><?= esc($a['user_name']) ?> · <?= date('d M H:i', strtotime($a['created_at'])) ?></td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?php $this->section('scripts'); ?>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>
<script>
const ctx = document.getElementById('revenueChart');
if (ctx) {
    new Chart(ctx, {
        type: 'bar',
        data: {
            labels: <?= json_encode(array_column($revenueChart, 'month')) ?>,
            datasets: [{
                label: 'Revenue',
                data: <?= json_encode(array_map(fn($r) => (float)$r['total'], $revenueChart)) ?>,
                backgroundColor: 'rgba(79,70,229,.8)',
                borderRadius: 6,
            }]
        },
        options: { responsive: true, plugins: { legend: { display: false } }, scales: { y: { beginAtZero: true, ticks: { callback: v => '<?= $sym ?>'+v.toLocaleString() } } } }
    });
}
</script>
<?php $this->endSection(); ?>
<?php $this->endSection(); ?>
