<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>

<?php
if (!empty($ticket_replies)) :
	foreach ($ticket_replies as $v_replies) :
		$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)) {
			continue;
		}
		$role_label = ($user_info->role_id == 1) ? lang('admin') : lang('staff');
		$avatar = !empty($profile_info->avatar) ? base_url($profile_info->avatar) : base_url('assets/img/user/default_avatar.jpg');
?>
		<div class="rounded-xl border border-border bg-card p-4 shadow-sm transition hover:border-primary/30" id="ticket-reply-container-<?php echo $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-11 w-11 rounded-full border border-border object-cover" src="<?= $avatar; ?>" alt="<?= html_escape($profile_info->fullname ?? '') ?>">
					<div>
						<div class="text-sm font-semibold text-foreground">
							<a class="hover:underline" href="<?= base_url('admin/user/user_details/' . $v_replies->replierid) ?>">
								<?= html_escape($profile_info->fullname ?? $user_info->username); ?>
							</a>
						</div>
						<div class="mt-1 inline-flex rounded-full bg-muted px-2 py-0.5 text-xs font-medium text-muted-foreground"><?= $role_label; ?></div>
					</div>
				</div>
				<div class="text-xs text-muted-foreground flex items-center gap-2">
					<span><?= time_ago($v_replies->time); ?></span>
					<?php if ($v_replies->replierid == $this->session->userdata('user_id')) { ?>
						<?= ajax_anchor(base_url("admin/tickets/delete/delete_ticket_replay/" . $v_replies->tickets_id . '/' . $v_replies->tickets_replies_id), "", array("class" => "inline-flex items-center", "title" => lang('delete'), "data-fade-out-on-success" => "#ticket-reply-container-" . $v_replies->tickets_replies_id)); ?>
					<?php } ?>
				</div>
			</div>

			<div class="mt-4 rounded-lg bg-muted/40 px-4 py-3 text-sm leading-6 text-foreground">
				<?php if (!empty($v_replies->body)) echo $v_replies->body; ?>
			</div>

			<?php
			$uploaded_file = [];
			if (!empty($v_replies->attachment) && is_string($v_replies->attachment)) {
				$uploaded_file = json_decode($v_replies->attachment);
				if (!is_array($uploaded_file) && !is_object($uploaded_file)) {
					$uploaded_file = [];
				}
			}
			if (!empty($uploaded_file)) {
			?>
				<div class="mt-3 grid gap-3 sm:grid-cols-2">
					<?php foreach ($uploaded_file as $fKey => $v_files) {
						if (empty($v_files)) {
							continue;
						}
					?>
						<a href="<?= base_url('admin/tickets/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-700/50">

							<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 data-lucide="<?= !empty($v_files->is_image) ? 'image' : 'file-text'; ?>" class="h-4 w-4"></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 } ?>

			<?php
			$comment_reply_type = 'tickets-reply';
			$comment_reply_details = $this->db->where('ticket_reply_id', $v_replies->tickets_replies_id)->order_by('time', 'ASC')->get('tbl_tickets_replies')->result();
			?>
			<div class="mt-4">
				<button type="button" class="ticket-reply-toggle inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-primary transition hover:bg-muted" data-toggle="tooltip" data-placement="top" title="<?= lang('click_to_reply') ?>" data-target="#reply_<?php echo $v_replies->tickets_replies_id ?>" id="reply__<?php echo $v_replies->tickets_replies_id ?>">
					<i data-lucide="reply" class="h-4 w-4"></i>
					<span><?= lang('reply') ?></span>
				</button>
				<?php $this->load->view('admin/tickets/comments_reply', array('comment_reply_details' => $comment_reply_details)) ?>
				<div class="ticket-inline-reply mt-3 hidden rounded-lg border border-border bg-muted/30 p-3" id="reply_<?php echo $v_replies->tickets_replies_id ?>">
					<form class="ticket-inline-reply-form space-y-3" id="<?php echo $comment_reply_type; ?>-comment-form-<?php echo $v_replies->tickets_replies_id ?>" action="<?php echo base_url() ?>admin/tickets/save_comments_reply/<?php
																																																if (!empty($v_replies->tickets_replies_id)) {
																																																	echo $v_replies->tickets_replies_id;
																																																}
																																																?>" method="post">
						<input type="hidden" name="tickets_id" value="<?php
																		if (!empty($v_replies->tickets_id)) {
																			echo $v_replies->tickets_id;
																		}
																		?>" class="form-control">
						<div class="mb-1">
							<textarea
								name="reply_comments"
								rows="3"
								placeholder="<?= lang('enter_your_reply_here'); ?>..."
								class="reply_comments w-full resize-y rounded-lg border border-border bg-input px-3 py-2 text-sm leading-6 text-foreground transition focus:border-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary/20"></textarea>
						</div>
						<div class="flex justify-end">
							<button type="submit" id="reply-<?php echo $v_replies->tickets_replies_id ?>" class="ticket-inline-reply-submit inline-flex items-center gap-2 rounded-lg px-4 py-2 text-xs font-semibold text-white shadow-sm transition btn btn-primary">
								<i data-lucide="send" class="h-4 w-4"></i>
								<span><?= lang('save') ?></span>
							</button>
						</div>
					</form>
				</div>
			</div>
		</div>
<?php
	endforeach;
endif;
?>

