<?php
declare(strict_types=1);

require_once dirname(__DIR__) . '/includes/layout.php';
require_once dirname(__DIR__) . '/includes/report-data.php';

crm_page_start('CRM Reports', 'reports', 'Analytics');
try {
    $dashboardKpis = crm_report_dashboard_data();
} catch (Throwable $error) {
    $dashboardKpis = [
        ['Total Leads', '0', 'CRM database pending'],
        ['Follow-ups Due', '0', 'CRM database pending'],
        ['Converted', '0', 'CRM database pending'],
        ['Pipeline Value', crm_money(0), 'CRM database pending'],
    ];
}
?>
<section class="kpi-grid">
    <?php foreach ($dashboardKpis as [$label, $value, $note]): ?>
        <article class="card kpi">
            <span><?= crm_e((string)$label) ?></span>
            <strong><?= crm_e((string)$value) ?></strong>
            <small><?= crm_e((string)$note) ?></small>
        </article>
    <?php endforeach; ?>
</section>
<div class="master-section-heading">
    <div><span><i class="fa-solid fa-list-check"></i></span><h2>CRM Reports</h2></div>
    <p>Operational lead and follow-up reports</p>
</div>
<section class="master-grid">
    <?php
    $reports = [
        ['Lead Register', 'Detailed searchable lead report.', 'fa-table-list', 'lead-register.php'],
        ['Quotation List', 'Search, view, edit and print all quotations.', 'fa-file-invoice-dollar', '../operations/quotations.php'],
        ['Follow-up Summary', 'Legacy-compatible lead follow-up worklist.', 'fa-calendar-check', 'follow-up.php'],
        ['Lead List Map', 'Plot every lead using exact or approximate area locations.', 'fa-map-location-dot', 'lead-map.php'],
        ['Status Summary', 'Pipeline distribution and conversion.', 'fa-chart-pie', 'status-summary.php'],
        ['Product Summary', 'Interest and quotation by product.', 'fa-boxes-stacked', 'product-summary.php'],
        ['Cancelled / Lost', 'Lost reasons and recovery opportunities.', 'fa-circle-xmark', 'cancelled-lost.php'],
    ];
    foreach ($reports as [$title, $description, $icon, $file]): ?>
        <a class="master-card" href="<?= crm_e(crm_url('reports/' . $file)) ?>">
            <span class="master-icon"><i class="fa-solid <?= crm_e($icon) ?>"></i></span>
            <h2><?= crm_e($title) ?></h2>
            <p><?= crm_e($description) ?></p>
            <i class="fa-solid fa-arrow-right arrow"></i>
        </a>
    <?php endforeach; ?>
</section>
<div class="master-section-heading" id="mis-reports">
    <div><span><i class="fa-solid fa-chart-column"></i></span><h2>MIS Reports</h2></div>
    <p>Legacy CRM MIS menu, excluding AMC Summary</p>
</div>
<section class="master-grid">
    <?php
    $misReports = [
        ['Quotation Summary', 'Monthly quotation quantity, subtotal, GST and value.', 'fa-file-invoice', 'quotation-summary.php'],
        ['Conversion Summary', 'Approved, open and cancelled quotation outcomes.', 'fa-arrow-trend-up', 'conversion-summary.php'],
        ['Categoriwise Summary', 'Lead performance grouped by legacy CRM category.', 'fa-tags', 'categoriwise-summary.php'],
        ['User Wise Summary', 'Allocation, follow-up and conversion by CRM user.', 'fa-users', 'user-wise-summary.php'],
        ['Monthly Statistics', 'Monthly leads, conversions, losses and pipeline.', 'fa-chart-line', 'monthly-statistics.php'],
        ['Yearly Summary', 'Yearly quotation quantity, subtotal, GST and value.', 'fa-calendar-days', 'yearly-summary.php'],
    ];
    foreach ($misReports as [$title, $description, $icon, $file]): ?>
        <a class="master-card" href="<?= crm_e(crm_url('reports/' . $file)) ?>">
            <span class="master-icon"><i class="fa-solid <?= crm_e($icon) ?>"></i></span>
            <h2><?= crm_e($title) ?></h2>
            <p><?= crm_e($description) ?></p>
            <i class="fa-solid fa-arrow-right arrow"></i>
        </a>
    <?php endforeach; ?>
</section>
<?php crm_page_end(); ?>
