<?php
$profile_info = $this->db->where(array('user_id' => $v_replies->replierid))->get('tbl_account_details')->row();
$user_info = $this->db->where(array('user_id' => $v_replies->replierid))->get('tbl_users')->row();

if (empty($user_info)) {
	return;
}

if ($user_info->role_id == 1) {
	$label = '<span class="rounded-full bg-red-100 px-2 py-0.5 text-[10px] font-semibold uppercase text-red-700">' . lang('admin') . '</span>';
} elseif ($user_info->role_id == 3) {
	$label = '<span class="rounded-full bg-blue-100 px-2 py-0.5 text-[10px] font-semibold uppercase text-blue-700">' . lang('staff') . '</span>';
} else {
	$label = '<span class="rounded-full bg-emerald-100 px-2 py-0.5 text-[10px] font-semibold uppercase text-emerald-700">' . lang('client') . '</span>';
}

$avatar = !empty($profile_info->avatar) ? base_url($profile_info->avatar) : base_url('assets/img/user/default_avatar.jpg');
?>
<div class="rounded-2xl border border-border bg-card p-5 shadow-sm" data-ticket-reply-id="<?= (int) $v_replies->tickets_replies_id; ?>">
	<div class="flex flex-wrap items-start justify-between gap-3">
		<div class="flex items-center gap-3">
			<img class="h-10 w-10 rounded-full border border-border" src="<?= $avatar; ?>" alt="<?= html_escape($profile_info->fullname ?? '') ?>">
			<div>
				<div class="text-sm font-semibold text-foreground">
					<?= html_escape($profile_info->fullname ?? $user_info->username); ?> <?= $label ?>
				</div>
				<div class="text-xs text-muted-foreground"><?= time_ago($v_replies->time); ?></div>
			</div>
		</div>
		<div class="text-xs text-muted-foreground">
			<?php if ($v_replies->replierid == $this->session->userdata('user_id')) { ?>
				<?= btn_delete('client/tickets/delete/delete_ticket_replay/' . $v_replies->tickets_id . '/' . $v_replies->tickets_replies_id) ?>
			<?php } ?>
		</div>
	</div>

	<div class="mt-3 text-sm text-foreground">
		<?php if (!empty($v_replies->body)) echo $v_replies->body; ?>
	</div>

	<?php
	$ticket_file = [];
	if (!empty($v_replies->attachment) && is_string($v_replies->attachment)) {
		$ticket_file = json_decode($v_replies->attachment);
		if (!is_array($ticket_file) && !is_object($ticket_file)) {
			$ticket_file = [];
		}
	}
	if (!empty($ticket_file)) {
		?>
		<div class="mt-3 grid gap-3 sm:grid-cols-2">
			<?php foreach ($ticket_file as $fKey => $v_files) {
				if (empty($v_files)) {
					continue;
				}
				?>
				<a href="<?= base_url('client/tickets/index/download_file/' . $v_replies->tickets_id . '/' . $fKey . '/' . $v_replies->tickets_replies_id); ?>"
					class="group flex items-center justify-between gap-3 rounded-lg border border-border bg-white px-3 py-2.5 text-sm transition-all hover:border-slate-400 hover:shadow-sm dark:bg-gray-800 dark:border-gray-700 dark:hover:border-gray-500 dark:hover:bg-gray-750">
					<span class="flex items-center gap-3 overflow-hidden">
						<span class="flex h-8 w-8 shrink-0 items-center justify-center rounded-md bg-slate-100 text-slate-500 transition-colors group-hover:bg-slate-200 dark:bg-gray-700 dark:text-gray-300 dark:group-hover:bg-gray-600">
							<i class="<?= !empty($v_files->is_image) ? 'fa-file-image-o' : ' '; ?>" data-lucide="file-text"></i>
						</span>
						<span class="truncate font-medium text-foreground dark:text-gray-200">
							<?= html_escape($v_files->fileName); ?>
						</span>
					</span>
					<span class="shrink-0 text-xs text-muted-foreground dark:text-gray-400">
						<?= (int) $v_files->size; ?> <?= lang('kb'); ?>
					</span>
				</a>
			<?php } ?>
		</div>
	<?php } ?>
</div>
