<?php
$answered = 0;
$closed = 0;
$open = 0;
$in_progress = 0;
$tickets_info = $this->report_model->get_permission('tbl_tickets');
$ticket_count = !empty($tickets_info) ? count($tickets_info) : 0;
if (!empty($tickets_info)) {
    foreach ($tickets_info as $v_tickets) {
        if ($v_tickets->status == 'answered') {
            $answered++;
        }
        if ($v_tickets->status == 'closed') {
            $closed++;
        }
        if ($v_tickets->status == 'open') {
            $open++;
        }
        if ($v_tickets->status == 'in_progress') {
            $in_progress++;
        }
    }
}

$summary_cards = array(
    array(
        'label' => lang('tickets'),
        'value' => $ticket_count,
        'meta' => 'Support cases included in the current report scope',
        'icon' => 'ticket',
        'tone' => 'text-sky-600 dark:text-sky-300 bg-sky-500/10',
    ),
    array(
        'label' => lang('open'),
        'value' => $open,
        'meta' => 'Cases waiting for action or first response',
        'icon' => 'circle-dot',
        'tone' => 'text-sky-600 dark:text-sky-300 bg-sky-500/10',
    ),
    array(
        'label' => lang('answered'),
        'value' => $answered,
        'meta' => 'Cases updated with a response to the requester',
        'icon' => 'message-square-reply',
        'tone' => 'text-amber-600 dark:text-amber-300 bg-amber-500/10',
    ),
    array(
        'label' => lang('closed'),
        'value' => $closed,
        'meta' => 'Cases fully resolved and closed out',
        'icon' => 'check-circle-2',
        'tone' => 'text-emerald-600 dark:text-emerald-300 bg-emerald-500/10',
    ),
);

$status_categories = array(
    lang('answered'),
    lang('in_progress'),
    lang('open'),
    lang('closed'),
);
$status_series = array(array(
    'name' => lang('tickets'),
    'categories' => $status_categories,
    'data' => array($answered, $in_progress, $open, $closed),
));

$assignment_categories = array();
$assignment_answered = array();
$assignment_in_progress = array();
$assignment_open = array();
$assignment_closed = array();
if (!empty($user_tickets) && !empty($assign_user)) {
    foreach ($user_tickets as $user => $v_ticket_user) {
        if ($user == 'all') {
            continue;
        }
        foreach ($assign_user as $v_user) {
            if ((string)$v_user->user_id !== (string)$user) {
                continue;
            }
            $assignment_categories[] = !empty($v_user->username) ? $v_user->username : fullname($v_user->user_id);
            $assignment_answered[] = !empty($v_ticket_user['answered']) ? count((array)$v_ticket_user['answered']) : 0;
            $assignment_in_progress[] = !empty($v_ticket_user['in_progress']) ? count((array)$v_ticket_user['in_progress']) : 0;
            $assignment_open[] = !empty($v_ticket_user['open']) ? count((array)$v_ticket_user['open']) : 0;
            $assignment_closed[] = !empty($v_ticket_user['closed']) ? count((array)$v_ticket_user['closed']) : 0;
            break;
        }
    }
}
if (empty($assignment_categories)) {
    $assignment_categories = array(lang('no_data_found'));
    $assignment_answered = array(0);
    $assignment_in_progress = array(0);
    $assignment_open = array(0);
    $assignment_closed = array(0);
}
$assignment_series = array(
    array('name' => lang('answered'), 'categories' => $assignment_categories, 'data' => $assignment_answered),
    array('name' => lang('in_progress'), 'categories' => $assignment_categories, 'data' => $assignment_in_progress),
    array('name' => lang('open'), 'categories' => $assignment_categories, 'data' => $assignment_open),
    array('name' => lang('closed'), 'categories' => $assignment_categories, 'data' => $assignment_closed),
);

$trend_categories = array();
$trend_answered = array();
$trend_open = array();
$trend_closed = array();
if (!empty($yearly_report)) {
    foreach ($yearly_report as $name => $v_report) {
        $trend_categories[] = date('M', safe_strtotime(date('Y') . '-' . $name . '-01'));
        $month_answered = 0;
        $month_open = 0;
        $month_closed = 0;
        if (!empty($v_report)) {
            foreach ($v_report as $s_report) {
                if ($s_report->status == 'answered') {
                    $month_answered++;
                }
                if ($s_report->status == 'open') {
                    $month_open++;
                }
                if ($s_report->status == 'closed') {
                    $month_closed++;
                }
            }
        }
        $trend_answered[] = $month_answered;
        $trend_open[] = $month_open;
        $trend_closed[] = $month_closed;
    }
}
$trend_series = array(
    array('name' => lang('answered'), 'categories' => $trend_categories, 'data' => $trend_answered),
    array('name' => lang('open'), 'categories' => $trend_categories, 'data' => $trend_open),
    array('name' => lang('closed'), 'categories' => $trend_categories, 'data' => $trend_closed),
);
?>

<div class="space-y-6 sales-report-page">
    <div class="grid grid-cols-1 gap-3 md:grid-cols-2 xl:grid-cols-4 sales-report-cardgrid">
        <?php foreach ($summary_cards as $card): ?>
            <div class="rounded-xl border border-border bg-card p-4 shadow-sm sales-report-card">
                <div class="flex items-start justify-between gap-3">
                    <div>
                        <p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= $card['label'] ?></p>
                        <p class="mt-2 text-xl font-bold text-foreground"><?= $card['value'] ?></p>
                        <p class="mt-1 text-xs text-muted-foreground"><?= $card['meta'] ?></p>
                    </div>
                    <span class="inline-flex h-10 w-10 shrink-0 items-center justify-center rounded-xl <?= $card['tone'] ?>">
                        <i data-lucide="<?= $card['icon'] ?>" class="h-5 w-5"></i>
                    </span>
                </div>
            </div>
        <?php endforeach; ?>
    </div>

    <section class="grid grid-cols-1 gap-6 xl:grid-cols-12">
        <div class="rounded-2xl border border-border bg-background/60 sales-report-module p-4 xl:col-span-4">
            <div class="mb-4 border-b border-border pb-4">
                <h3 class="text-lg font-semibold text-foreground"><?= lang('tickets') . ' ' . lang('status') ?></h3>
                <p class="text-sm text-muted-foreground">Current support queue mix by status.</p>
            </div>
            <div class="sales-report-chart" data-chart="bar" data-height="320" data-height-sm="240" data-series='<?= json_encode($status_series, JSON_UNESCAPED_SLASHES | JSON_HEX_APOS) ?>'></div>
        </div>
        <div class="rounded-2xl border border-border bg-background/60 sales-report-module p-4 xl:col-span-8">
            <div class="mb-4 border-b border-border pb-4">
                <h3 class="text-lg font-semibold text-foreground"><?= lang('tickets_r_assignment') ?></h3>
                <p class="text-sm text-muted-foreground">Case allocation by owner and stage.</p>
            </div>
            <div class="sales-report-chart" data-chart="bar" data-height="320" data-height-sm="240" data-series='<?= json_encode($assignment_series, JSON_UNESCAPED_SLASHES | JSON_HEX_APOS) ?>'></div>
        </div>
    </section>

    <section class="rounded-2xl border border-border bg-background/60 sales-report-module p-4">
        <div class="mb-4 border-b border-border pb-4">
            <h3 class="text-lg font-semibold text-foreground"><?= lang('tickets') ?> Trend</h3>
            <p class="text-sm text-muted-foreground">Monthly ticket handling volume for the current year.</p>
        </div>
        <div class="sales-report-chart" data-chart="area" data-height="320" data-height-sm="240" data-series='<?= json_encode($trend_series, JSON_UNESCAPED_SLASHES | JSON_HEX_APOS) ?>'></div>
    </section>
</div>
