<?php
$project_id = !empty($id) ? $id : (!empty($project_details->project_id) ? $project_details->project_id : null);
$all_bugs_info = !empty($project_id) ? get_result('tbl_bug', array('project_id' => $project_id)) : array();
?>

<div class="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-slate-500"><?= lang('bugs') ?></p>
		</div>
	</div>

	<div class="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-900/40">
		<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>
					</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 ($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';
							}
							$reporter = $this->db->where('user_id', $v_bugs->reporter)->get('tbl_users')->row();
							if (!empty($reporter->role_id) && $reporter->role_id == '1') {
								$badge = 'bg-rose-50 text-rose-700';
							} elseif (!empty($reporter->role_id) && $reporter->role_id == '2') {
								$badge = 'bg-blue-50 text-blue-700';
							} else {
								$badge = 'bg-indigo-50 text-indigo-700';
							}
					?>
							<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">
									<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() ?>client/bugs/view_bug_details/<?= $v_bugs->bug_id ?>"><?= html_escape($v_bugs->bug_title) ?></a>
								</td>
								<td class="px-4 py-3">
									<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 ?>"><?= fullname($v_bugs->reporter) ?></span>
								</td>
							</tr>
						<?php endforeach; ?>
					<?php else : ?>
						<tr>
							<td colspan="4" 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>
