<?php
$client_tab_data = isset($client_tab_data) && is_array($client_tab_data) ? $client_tab_data : array();
$bugs = isset($client_tab_data['bugs']) ? $client_tab_data['bugs'] : array();
$created = can_action('58', 'created');
$bug_reporter_id = !empty($client_details->primary_contact) ? $client_details->primary_contact : get_any_field('tbl_account_details', array('company' => $client_details->client_id), 'user_id');

$status_badge = function ($status) {
	$status = strtolower((string) $status);
	if ($status === 'resolved' || $status === 'fixed') {
		return 'bg-emerald-100 text-emerald-700 dark:bg-emerald-900/40 dark:text-emerald-300';
	}
	if ($status === 'open' || $status === 'new') {
		return 'bg-sky-100 text-sky-700 dark:bg-sky-900/40 dark:text-sky-300';
	}
	if ($status === 'closed') {
		return 'bg-slate-100 text-slate-700 dark:bg-slate-700 dark:text-slate-200';
	}
	return 'bg-amber-100 text-amber-700 dark:bg-amber-900/40 dark:text-amber-300';
};

$priority_badge = function ($priority) {
	$priority = strtolower((string) $priority);
	if ($priority === 'high') {
		return 'bg-rose-100 text-rose-700 dark:bg-rose-900/40 dark:text-rose-300';
	}
	if ($priority === 'medium') {
		return 'bg-amber-100 text-amber-700 dark:bg-amber-900/40 dark:text-amber-300';
	}
	return 'bg-slate-100 text-slate-700 dark:bg-slate-700 dark:text-slate-200';
};
?>

<div class="rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-800">
	<div class="flex flex-wrap items-center justify-between gap-3 border-b border-slate-200 px-5 py-4 dark:border-slate-700">
		<div>
			<h3 class="text-sm font-semibold text-slate-900 dark:text-slate-100"><?= lang('bugs') ?></h3>
			<p class="text-xs text-slate-500 dark:text-slate-400"><?= lang('client') ?> &bull; <?= html_escape($client_details->name ?? '') ?></p>
		</div>
		<div class="flex flex-wrap items-center gap-2">
			<span class="inline-flex items-center rounded-full border border-slate-200 bg-slate-50 px-2.5 py-1 text-xs font-semibold text-slate-600 dark:border-slate-600 dark:bg-slate-700/70 dark:text-slate-200">
				<?= count($bugs) ?> <?= lang('records') ?>
			</span>
			<?php if (!empty($created)) { ?>
				<a href="<?= base_url('admin/bugs/create/' . (!empty($bug_reporter_id) ? 'c_' . $bug_reporter_id : '')) ?>"
					class="inline-flex items-center rounded-lg bg-slate-900 px-3 py-1.5 text-xs font-semibold text-white hover:bg-slate-800 dark:bg-slate-100 dark:text-slate-900 dark:hover:bg-white">
					<?= lang('new_bugs') ?>
				</a>
			<?php } ?>
		</div>
	</div>

	<div class="p-5">
		<div class="overflow-hidden rounded-xl border border-slate-200 dark:border-slate-700">
			<div class="overflow-x-auto">
				<table class="min-w-[760px] w-full text-sm">
					<thead class="bg-slate-50 text-xs uppercase tracking-wide text-slate-500 dark:bg-slate-900/60 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('date') ?></th>
						</tr>
					</thead>
					<tbody class="divide-y divide-slate-100 text-slate-700 dark:divide-slate-800 dark:text-slate-200">
						<?php if (!empty($bugs)) { ?>
							<?php foreach ($bugs as $bug) { ?>
								<tr class="transition hover:bg-slate-50/80 dark:hover:bg-slate-900/40">
									<td class="px-4 py-3 font-medium text-slate-800 dark:text-slate-100"><?= !empty($bug->bug_title) ? html_escape($bug->bug_title) : '-' ?></td>
									<td class="px-4 py-3">
										<span class="inline-flex items-center rounded-full px-2.5 py-1 text-xs font-semibold <?= $status_badge($bug->bug_status ?? '') ?>">
											<?= !empty($bug->bug_status) ? lang($bug->bug_status) : '-' ?>
										</span>
									</td>
									<td class="px-4 py-3">
										<span class="inline-flex items-center rounded-full px-2.5 py-1 text-xs font-semibold <?= $priority_badge($bug->priority ?? '') ?>">
											<?= !empty($bug->priority) ? lang($bug->priority) : '-' ?>
										</span>
									</td>
									<td class="px-4 py-3"><?= !empty($bug->date_added) ? display_date($bug->date_added) : '-' ?></td>
								</tr>
							<?php } ?>
						<?php } else { ?>
							<tr>
								<td colspan="4" class="px-4 py-8 text-center text-sm text-slate-500 dark:text-slate-400">
									<?= lang('no_record_found') ?>
								</td>
							</tr>
						<?php } ?>
					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>
