<?php
$edited = can_action('57', 'edited');
$deleted = can_action('57', 'deleted');
$all_bugs_info = isset($project_bugs_info) ? $project_bugs_info : array();
$b_edited = can_action('58', 'edited');
$b_deleted = can_action('58', 'deleted');
$bug_total = count($all_bugs_info);
$bug_resolved = 0;
$bug_critical = 0;
$bug_active = 0;
foreach ($all_bugs_info as $bug_metric) {
	if ($bug_metric->bug_status === 'resolved' || $bug_metric->bug_status === 'resolve') {
		$bug_resolved++;
	} else {
		$bug_active++;
	}
	if (!empty($bug_metric->priority) && in_array(strtolower($bug_metric->priority), array('high', 'critical'), true)) {
		$bug_critical++;
	}
}
?>

<div class="project-overview-shell space-y-4">
	<div class="flex flex-wrap items-center justify-between gap-2">
		<div>
			<h3 class="text-base font-semibold text-slate-900 dark:text-slate-100"><?= lang('all_bugs') ?></h3>
			<p class="text-sm text-muted-foreground">Bug backlog, issue status, and resolution flow.</p>
		</div>
		<?php if (!empty($edited)) { ?>
			<a href="<?= base_url() ?>admin/bugs/create/project/<?= $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') . ' ' . lang('bugs') ?>
			</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-rose-50 p-2.5 text-rose-600 dark:bg-rose-900/30 dark:text-rose-400">
					<i data-lucide="bug" class="h-5 w-5"></i>
				</div>
				<div>
					<div class="text-2xl font-bold text-foreground"><?= (int) $bug_total ?></div>
					<div class="text-[11px] font-medium uppercase tracking-[0.16em] text-muted-foreground">Total Issues</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="activity" class="h-5 w-5"></i>
				</div>
				<div>
					<div class="text-2xl font-bold text-foreground"><?= (int) $bug_active ?></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-emerald-50 p-2.5 text-emerald-600 dark:bg-emerald-900/30 dark:text-emerald-400">
					<i data-lucide="shield-check" class="h-5 w-5"></i>
				</div>
				<div>
					<div class="text-2xl font-bold text-foreground"><?= (int) $bug_resolved ?></div>
					<div class="text-[11px] font-medium uppercase tracking-[0.16em] text-muted-foreground"><?= lang('resolved') ?></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-violet-50 p-2.5 text-violet-600 dark:bg-violet-900/30 dark:text-violet-400">
					<i data-lucide="alert-triangle" class="h-5 w-5"></i>
				</div>
				<div>
					<div class="text-2xl font-bold text-foreground"><?= (int) $bug_critical ?></div>
					<div class="text-[11px] font-medium uppercase tracking-[0.16em] text-muted-foreground">High Priority</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">Bug Register</h4>
				<p class="text-xs text-muted-foreground">Triage, resolution state, and reporter visibility.</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) $bug_total ?> total</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) $bug_resolved ?> <?= lang('resolved') ?></span>
			</div>
		</div>
		<div class="overflow-x-auto overflow-y-visible">
			<table 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('bug_title') ?></th>
						<th class="px-4 py-3 text-left"><?= lang('status') ?></th>
						<th class="px-4 py-3 text-left"><?= lang('priority') ?></th>
						<th class="px-4 py-3 text-left"><?= lang('reporter') ?></th>
						<th class="px-4 py-3 text-left"><?= lang('action') ?></th>
					</tr>
				</thead>
				<tbody class="divide-y divide-slate-100 dark:divide-slate-800">
					<?php
					if (!empty($all_bugs_info)) : foreach ($all_bugs_info as $key => $v_bugs) :
							if (!empty($v_bugs->reporter_role_id) && $v_bugs->reporter_role_id == '1') {
								$badge = 'bg-rose-50 text-rose-700';
							} elseif (!empty($v_bugs->reporter_role_id) && $v_bugs->reporter_role_id == '2') {
								$badge = 'bg-blue-50 text-blue-700';
							} else {
								$badge = 'bg-indigo-50 text-indigo-700';
							}
					?>
							<tr id="table-bugs-<?= $v_bugs->bug_id ?>">
								<td class="px-4 py-3">
									<a class="font-medium text-primary hover:underline dark:text-sky-300" style="<?= $v_bugs->bug_status == 'resolve' ? 'text-decoration: line-through;' : '' ?>"
										href="<?= base_url() ?>admin/bugs/details/<?= $v_bugs->bug_id ?>"><?= html_escape($v_bugs->bug_title) ?></a>
								</td>
								<td class="px-4 py-3">
									<?php
									if ($v_bugs->bug_status == 'unconfirmed') {
										$label = 'bg-amber-50 text-amber-700';
									} elseif ($v_bugs->bug_status == 'confirmed') {
										$label = 'bg-blue-50 text-blue-700';
									} elseif ($v_bugs->bug_status == 'in_progress') {
										$label = 'bg-indigo-50 text-indigo-700';
									} else {
										$label = 'bg-emerald-50 text-emerald-700';
									}
									?>
									<span class="inline-flex rounded-full px-2 py-0.5 text-xs <?= $label ?>"><?= lang($v_bugs->bug_status) ?></span>
								</td>
								<td class="px-4 py-3"><?= ucfirst($v_bugs->priority) ?></td>
								<td class="px-4 py-3">
									<span class="inline-flex rounded-full px-2 py-0.5 text-xs <?= $badge ?>"><?= !empty($v_bugs->reporter_name) ? $v_bugs->reporter_name : fullname($v_bugs->reporter) ?></span>
								</td>
								<td class="px-4 py-3">
									<?php if (!empty($b_edited)) { ?>
										<?= btn_edit('admin/bugs/create/' . $v_bugs->bug_id) ?>
									<?php }
									if (!empty($b_deleted)) { ?>
										<?= ajax_anchor(base_url("admin/bugs/delete_bug/" . $v_bugs->bug_id), "", array("class" => "", "title" => lang('delete'), "data-fade-out-on-success" => "#table-bugs-" . $v_bugs->bug_id)); ?>
									<?php } ?>
								</td>
							</tr>
						<?php endforeach; ?>
					<?php else : ?>
						<tr>
							<td colspan="5" class="px-4 py-6 text-center text-sm text-slate-500 dark:text-slate-400"><?= lang('no_data_found') ?></td>
						</tr>
					<?php endif; ?>
				</tbody>
			</table>
		</div>
	</div>
</div>
