<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php
$money = function ($value) use ($currency_symbol) { return $currency_symbol . number_format((float) $value, 2); };
$date_text = function ($value) { return !empty($value) ? date('M d, Y', strtotime($value)) : '--'; };
?>
<div class="space-y-4 sm:space-y-6">
    <div class="grid grid-cols-1 gap-4 sm:grid-cols-2 xl:grid-cols-4">
        <article class="rounded-2xl border border-border bg-background/70 p-4 sm:p-5 shadow-sm"><p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Leads</p><p class="mt-3 text-2xl font-semibold text-foreground sm:text-3xl"><?= (int) $crm['lead_count'] ?></p></article>
        <article class="rounded-2xl border border-border bg-background/70 p-4 sm:p-5 shadow-sm"><p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Opportunities</p><p class="mt-3 text-2xl font-semibold text-foreground sm:text-3xl"><?= (int) $crm['opportunity_count'] ?></p></article>
        <article class="rounded-2xl border border-border bg-background/70 p-4 sm:p-5 shadow-sm"><p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Tickets</p><p class="mt-3 text-2xl font-semibold text-foreground sm:text-3xl"><?= (int) $crm['ticket_count'] ?></p></article>
        <article class="rounded-2xl border border-border bg-background/70 p-4 sm:p-5 shadow-sm"><p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Pipeline Value</p><p class="mt-3 text-2xl font-semibold text-violet-500 sm:text-3xl"><?= $money($crm['pipeline_total']) ?></p></article>
    </div>

    <div class="grid grid-cols-1 gap-6 xl:grid-cols-3">
        <article class="rounded-2xl border border-border bg-background/60 p-4 sm:p-5"><div class="mb-4"><p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Leads</p><h3 class="text-lg font-semibold text-foreground">Source mix</h3></div><div data-chart="bar" data-height="260" data-height-sm="220" data-series='<?= json_encode($crm['lead_source_series'], JSON_UNESCAPED_SLASHES) ?>'></div></article>
        <article class="rounded-2xl border border-border bg-background/60 p-4 sm:p-5"><div class="mb-4"><p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Pipeline</p><h3 class="text-lg font-semibold text-foreground">Opportunity stages</h3></div><div data-chart="bar" data-height="260" data-height-sm="220" data-series='<?= json_encode($crm['pipeline_series'], JSON_UNESCAPED_SLASHES) ?>'></div></article>
        <article class="rounded-2xl border border-border bg-background/60 p-4 sm:p-5"><div class="mb-4"><p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Support</p><h3 class="text-lg font-semibold text-foreground">Ticket status</h3></div><div data-chart="bar" data-height="260" data-height-sm="220" data-series='<?= json_encode($crm['ticket_status_series'], JSON_UNESCAPED_SLASHES) ?>'></div></article>
    </div>

    <div class="grid grid-cols-1 gap-6 xl:grid-cols-3">
        <article class="rounded-2xl border border-border bg-background/60 p-4 sm:p-5">
            <div class="mb-4"><p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Recent Leads</p><h3 class="text-lg font-semibold text-foreground">Newest prospects</h3></div>
            <div class="space-y-3">
                <?php foreach ($crm['recent_leads'] as $lead): ?>
                    <div class="rounded-xl border border-border bg-card p-4">
                        <p class="text-sm font-semibold text-foreground"><?= html_escape($lead->lead_name) ?></p>
                        <p class="mt-1 text-xs text-muted-foreground"><?= html_escape($lead->lead_source ?: 'Direct') ?> • <?= html_escape($lead->lead_status ?: 'New') ?></p>
                    </div>
                <?php endforeach; ?>
            </div>
        </article>
        <article class="rounded-2xl border border-border bg-background/60 p-4 sm:p-5">
            <div class="mb-4"><p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Recent Opportunities</p><h3 class="text-lg font-semibold text-foreground">Current commercial pipeline</h3></div>
            <div class="space-y-3">
                <?php foreach ($crm['recent_opportunities'] as $opp): ?>
                    <div class="rounded-xl border border-border bg-card p-4">
                        <p class="text-sm font-semibold text-foreground"><?= html_escape($opp->opportunity_name) ?></p>
                        <p class="mt-1 text-xs text-muted-foreground"><?= html_escape(ucfirst($opp->stages ?: 'open')) ?> • Close <?= $date_text($opp->close_date) ?></p>
                        <p class="mt-2 text-sm font-semibold text-primary"><?= $money($opp->expected_revenue) ?></p>
                    </div>
                <?php endforeach; ?>
            </div>
        </article>
        <article class="rounded-2xl border border-border bg-background/60 p-4 sm:p-5">
            <div class="mb-4"><p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Recent Tickets</p><h3 class="text-lg font-semibold text-foreground">Support queue snapshot</h3></div>
            <div class="space-y-3">
                <?php foreach ($crm['recent_tickets'] as $ticket): ?>
                    <div class="rounded-xl border border-border bg-card p-4">
                        <div class="flex items-start justify-between gap-3">
                            <div>
                                <p class="text-sm font-semibold text-foreground"><?= html_escape($ticket->ticket_code ?: 'Ticket') ?></p>
                                <p class="mt-1 text-xs text-muted-foreground"><?= html_escape($ticket->subject) ?></p>
                            </div>
                            <span class="rounded-full bg-muted px-2.5 py-1 text-[11px] font-semibold text-foreground"><?= html_escape(ucfirst($ticket->priority ?: 'medium')) ?></span>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </article>
    </div>
</div>
