<?php
$edited = can_action('57', 'edited');
$deleted = can_action('57', 'deleted');
$all_tickets_info = isset($project_tickets_info) ? $project_tickets_info : array();
$ticket_total = count($all_tickets_info);
$ticket_open = 0;
$ticket_progress = 0;
$ticket_closed = 0;
foreach ($all_tickets_info as $ticket_metric) {
	if ($ticket_metric->status === 'open') {
		$ticket_open++;
	} elseif ($ticket_metric->status === 'closed') {
		$ticket_closed++;
	} else {
		$ticket_progress++;
	}
}
?>

<div class="project-overview-shell space-y-4">
    <div class="flex flex-wrap items-center justify-between gap-3">
        <div>
            <h3 class="text-base font-semibold text-slate-900 dark:text-slate-100"><?= lang('tickets') ?></h3>
            <p class="text-sm text-slate-500 dark:text-slate-400">Support pipeline, reporter flow, and department status.</p>
        </div>
        <?php if (!empty($edited)) { ?>
            <a href="<?= base_url() ?>admin/tickets/create/project_tickets/0/<?= $project_details->project_id ?>"
               class="inline-flex items-center gap-2 rounded-2xl bg-slate-900 px-3.5 py-2 text-xs font-semibold text-white shadow-sm hover:bg-slate-800 dark:hover:bg-slate-700">
                <i data-lucide="plus" class="h-4 w-4"></i>
                <?= lang('new_ticket') ?>
            </a>
        <?php } ?>
    </div>

    <div class="grid gap-3 md:grid-cols-2 xl:grid-cols-4">
        <div class="project-kpi-card rounded-xl border border-border bg-card p-4 shadow-sm">
            <div class="flex items-center gap-3">
                <div class="rounded-xl bg-cyan-50 p-2.5 text-cyan-600 dark:bg-cyan-900/30 dark:text-cyan-400">
                    <i data-lucide="ticket" class="h-5 w-5"></i>
                </div>
                <div>
                    <div class="text-2xl font-bold text-foreground"><?= (int) $ticket_total ?></div>
                    <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-muted-foreground">Total Tickets</div>
                </div>
            </div>
        </div>
        <div class="project-kpi-card rounded-xl border border-border bg-card p-4 shadow-sm">
            <div class="flex items-center gap-3">
                <div class="rounded-xl bg-rose-50 p-2.5 text-rose-600 dark:bg-rose-900/30 dark:text-rose-400">
                    <i data-lucide="mail" class="h-5 w-5"></i>
                </div>
                <div>
                    <div class="text-2xl font-bold text-foreground"><?= (int) $ticket_open ?></div>
                    <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-muted-foreground"><?= lang('open') ?></div>
                </div>
            </div>
        </div>
        <div class="project-kpi-card rounded-xl border border-border bg-card p-4 shadow-sm">
            <div class="flex items-center gap-3">
                <div class="rounded-xl bg-amber-50 p-2.5 text-amber-600 dark:bg-amber-900/30 dark:text-amber-400">
                    <i data-lucide="loader" class="h-5 w-5"></i>
                </div>
                <div>
                    <div class="text-2xl font-bold text-foreground"><?= (int) $ticket_progress ?></div>
                    <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-muted-foreground"><?= lang('in_progress') ?></div>
                </div>
            </div>
        </div>
        <div class="project-kpi-card rounded-xl border border-border bg-card p-4 shadow-sm">
            <div class="flex items-center gap-3">
                <div class="rounded-xl bg-emerald-50 p-2.5 text-emerald-600 dark:bg-emerald-900/30 dark:text-emerald-400">
                    <i data-lucide="check-circle-2" class="h-5 w-5"></i>
                </div>
                <div>
                    <div class="text-2xl font-bold text-foreground"><?= (int) $ticket_closed ?></div>
                    <div class="text-[11px] font-medium uppercase tracking-[0.16em] text-muted-foreground"><?= lang('closed') ?></div>
                </div>
            </div>
        </div>
    </div>

    <div class="project-section-card overflow-hidden rounded-2xl border border-border bg-card shadow-sm">
        <div class="flex flex-wrap items-center justify-between gap-3 border-b border-border px-4 py-3">
            <div>
                <h4 class="text-sm font-semibold text-foreground">Ticket Queue</h4>
                <p class="text-xs text-muted-foreground">Support issues by code, reporter, team, and current status.</p>
            </div>
            <div class="flex flex-wrap gap-2 text-xs">
                <span class="project-summary-chip inline-flex items-center gap-2 px-2.5 py-1 text-slate-700 dark:text-slate-200"><span class="h-2 w-2 rounded-full bg-rose-500"></span><?= (int) $ticket_open ?> <?= lang('open') ?></span>
                <span class="project-summary-chip inline-flex items-center gap-2 px-2.5 py-1 text-slate-700 dark:text-slate-200"><span class="h-2 w-2 rounded-full bg-amber-500"></span><?= (int) $ticket_progress ?> <?= lang('in_progress') ?></span>
                <span class="project-summary-chip inline-flex items-center gap-2 px-2.5 py-1 text-slate-700 dark:text-slate-200"><span class="h-2 w-2 rounded-full bg-emerald-500"></span><?= (int) $ticket_closed ?> <?= lang('closed') ?></span>
            </div>
        </div>
        <div class="overflow-x-auto overflow-y-visible">
            <table id="table-tickets" class="min-w-full text-sm">
                <thead class="bg-slate-50 text-slate-500 dark:bg-slate-800 dark:text-slate-300">
                    <tr>
                        <th class="px-4 py-3 text-left"><?= lang('ticket_code') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('subject') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('date') ?></th>
                        <?php if ($this->session->userdata('user_type') == '1') { ?>
                            <th class="px-4 py-3 text-left"><?= lang('reporter') ?></th>
                        <?php } ?>
                        <th class="px-4 py-3 text-left"><?= lang('department') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('status') ?></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
                    <?php
                    if (!empty($all_tickets_info)) {
                        foreach ($all_tickets_info as $v_tickets_info) {
                            if ($v_tickets_info->status == 'open') {
                                $s_label = 'bg-rose-50 text-rose-700';
                            } elseif ($v_tickets_info->status == 'closed') {
                                $s_label = 'bg-emerald-50 text-emerald-700';
                            } else {
                                $s_label = 'bg-slate-100 text-slate-700';
                            }
                            $dept_name = !empty($v_tickets_info->department_name) ? $v_tickets_info->department_name : '-';
                            $status = $v_tickets_info->status == 'in_progress' ? 'In Progress' : $v_tickets_info->status;
                    ?>
                            <tr class="text-slate-700 transition hover:bg-slate-50/70 dark:text-slate-200 dark:hover:bg-slate-800/50"><td class="px-4 py-3"><span class="inline-flex rounded-full bg-emerald-50 px-2 py-0.5 text-xs text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-200"><?= $v_tickets_info->ticket_code ?></span></td>
                                <td class="px-4 py-3"><a class="font-medium text-primary hover:underline dark:text-sky-300" href="<?= base_url() ?>admin/tickets/tickets_details/<?= $v_tickets_info->tickets_id ?>"><?= $v_tickets_info->subject ?></a></td>
                                <td class="px-4 py-3"><?= display_date($v_tickets_info->created) ?></td>
                                <?php if ($this->session->userdata('user_type') == '1') { ?>
                                    <td class="px-4 py-3">
                                        <?php if (!empty($v_tickets_info->reporter_name)) { ?>
                                            <div class="flex items-center gap-2">
                                                <img class="h-8 w-8 rounded-full" src="<?= base_url() . (!empty($v_tickets_info->reporter_avatar) ? $v_tickets_info->reporter_avatar : 'assets/img/user/default_avatar.jpg') ?>" alt="">
                                                <span><?= $v_tickets_info->reporter_name ?></span>
                                            </div>
                                        <?php } else { echo '-'; } ?>
                                    </td>
                                <?php } ?>
                                <td class="px-4 py-3"><?= $dept_name ?></td>
                                <td class="px-4 py-3"><span class="inline-flex rounded-full px-2 py-0.5 text-xs <?= $s_label ?>"><?= ucfirst($status) ?></span></td>
                            </tr>
                    <?php
                        }
                    }
                    ?>
                    <?php if (empty($all_tickets_info)) { ?>
                        <tr>
                            <td colspan="6" class="px-4 py-6 text-center text-sm text-slate-500 dark:text-slate-400"><?= lang('no_data_found') ?></td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
