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

<?= message_box('success'); ?>
<?= message_box('error'); ?>

<?php
$can_edit = $this->tickets_model->can_action('tbl_tickets', 'edit', array('tickets_id' => $tickets_info->tickets_id));
$can_delete = $this->tickets_model->can_action('tbl_tickets', 'delete', array('tickets_id' => $tickets_info->tickets_id));
$edited = can_action(6, 'edited');
$deleted = can_action(6, 'deleted');

$pin_where = array('user_id' => $this->session->userdata('user_id'), 'module_id' => $tickets_info->tickets_id, 'module_name' => 'tickets');
$check_existing = $this->tickets_model->check_by($pin_where, 'tbl_pinaction');
if (!empty($check_existing)) {
	$pin_url = 'remove_todo/' . $check_existing->pinaction_id;
	$pin_btn = 'danger';
	$pin_title = lang('remove_todo');
} else {
	$pin_url = 'add_todo_list/tickets/' . $tickets_info->tickets_id;
	$pin_btn = 'warning';
	$pin_title = lang('add_todo_list');
}

$reporter_info = get_row('tbl_account_details', array('user_id' => $tickets_info->reporter));
$department_info = get_row('tbl_departments', array('departments_id' => $tickets_info->departments_id));
$project_info = !empty($tickets_info->project_id) ? get_row('tbl_project', array('project_id' => $tickets_info->project_id)) : null;

$status_value = $tickets_info->status === 'in_progress' ? 'in_progress' : $tickets_info->status;
$status_label = lang($status_value);
$status_tone = 'bg-slate-100 text-slate-700';
if ($tickets_info->status === 'open') {
	$status_tone = 'bg-rose-50 text-rose-700';
} elseif ($tickets_info->status === 'closed') {
	$status_tone = 'bg-emerald-50 text-emerald-700';
} elseif ($tickets_info->status === 'in_progress') {
	$status_tone = 'bg-amber-50 text-amber-700';
}

$reporter_html = '-';
if (!empty($reporter_info)) {
	$reporter_avatar = base_url() . (!empty($reporter_info->avatar) ? $reporter_info->avatar : 'assets/img/user/default_avatar.jpg');
	$reporter_html = '<div class="flex items-center gap-2">'
		. '<img class="h-8 w-8 rounded-full border border-border" src="' . $reporter_avatar . '" alt="' . html_escape($reporter_info->fullname) . '">'
		. '<span>' . html_escape($reporter_info->fullname) . '</span>'
		. '</div>';
}

$fields = array(
	array('label' => lang('ticket_code'), 'value' => $tickets_info->ticket_code),
	array('label' => lang('subject'), 'value' => $tickets_info->subject),
	array('label' => lang('reporter'), 'html' => $reporter_html),
	array('label' => lang('department'), 'value' => !empty($department_info) ? $department_info->deptname : '-'),
	array('label' => lang('priority'), 'value' => $tickets_info->priority),
	array('label' => lang('status'), 'html' => '<span class="inline-flex rounded-full px-2 py-0.5 text-xs font-semibold ' . $status_tone . '">' . $status_label . '</span>'),
	array('label' => lang('tags'), 'html' => get_tags($tickets_info->tags, true)),
	array('label' => lang('created'), 'value' => display_date($tickets_info->created)),
);

if (!empty($tickets_info->last_reply)) {
	$fields[] = array('label' => lang('last_reply'), 'value' => display_datetime($tickets_info->last_reply));
}

$custom_fields = custom_form_label(7, $tickets_info->tickets_id);
if (!empty($custom_fields)) {
	foreach ($custom_fields as $c_label => $v_fields) {
		if (!empty($v_fields)) {
			$fields[] = array('label' => $c_label, 'value' => $v_fields);
		}
	}
}
?>

<div class="space-y-6">
	<div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
		<div class="flex flex-wrap items-start justify-between gap-4">
			<div class="space-y-2">
				<p class="text-xs uppercase tracking-wide text-muted-foreground"><?= lang('tickets'); ?></p>
				<h2 class="text-xl font-semibold text-foreground">[<?= html_escape($tickets_info->ticket_code); ?>] <?= html_escape($tickets_info->subject); ?></h2>
				<div class="flex flex-wrap items-center gap-2 text-sm text-muted-foreground">
					<span class="inline-flex rounded-full px-2 py-0.5 text-xs font-semibold <?= $status_tone; ?>"><?= $status_label; ?></span>
					<span class="inline-flex rounded-full bg-slate-100 px-2 py-0.5 text-xs text-slate-600"><?= html_escape($tickets_info->priority); ?></span>
					<?php if (!empty($department_info)) { ?>
						<span class="inline-flex rounded-full bg-slate-100 px-2 py-0.5 text-xs text-slate-600"><?= html_escape($department_info->deptname); ?></span>
					<?php } ?>
					<?php if (!empty($project_info)) { ?>
						<a class="text-primary hover:underline" href="<?= base_url('admin/projects/project_details/' . $project_info->project_id); ?>">
							<?= html_escape($project_info->project_name); ?>
						</a>
					<?php } ?>
				</div>
			</div>
			<div class="flex flex-wrap items-center gap-2">
				<?php if (!empty($can_edit) && !empty($edited)) { ?>
					<a href="<?= base_url('admin/tickets/create/edit_tickets/' . $tickets_info->tickets_id); ?>" class="inline-flex items-center gap-2 rounded-lg bg-primary px-3 py-2 text-xs font-semibold text-white hover:bg-primary/90">
						<i data-lucide="square-pen" class="h-4 w-4"></i>
						<?= lang('edit'); ?>
					</a>
					<div class="btn-group">
						<button class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground hover:bg-muted dropdown-toggle" data-toggle="dropdown">
							<i data-lucide="refresh-cw" class="h-4 w-4"></i>
							<?= lang('change_status'); ?>
							<span class="caret"></span>
						</button>
						<ul class="dropdown-menu animated zoomIn">
							<?php
							$status_info = get_result('tbl_status');
							if (!empty($status_info)) {
								foreach ($status_info as $v_status) {
							?>
									<li><a data-toggle="modal" data-target="#myModal" href="<?= base_url('admin/tickets/change_status/' . $tickets_info->tickets_id . '/' . $v_status->status); ?>"><?= ucfirst($v_status->status); ?></a></li>
							<?php
								}
							}
							?>
						</ul>
					</div>
				<?php } ?>
				<a data-toggle="tooltip" data-placement="top" title="<?= $pin_title; ?>" href="<?= base_url('admin/projects/' . $pin_url); ?>" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground hover:bg-muted">
					<i class="fa fa-thumb-tack"></i>
				</a>
			</div>
		</div>
	</div>

	<div class="grid gap-6 lg:grid-cols-[320px_1fr]">
		<div class="space-y-4">
			<?php $this->load->view('admin/common/details_grid', array(
				'title' => lang('ticket_details'),
				'fields' => $fields,
			)); ?>
		</div>

		<div class="space-y-6">
			<div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
				<h3 class="text-sm font-semibold text-foreground mb-3"><?= lang('ticket_message'); ?></h3>
				<div class="prose max-w-none text-sm text-foreground">
					<?= nl2br($tickets_info->body); ?>
				</div>

				<?php
				$uploaded_file = json_decode($tickets_info->upload_file);
				if (!empty($uploaded_file)) :
				?>
					<div class="mt-4 space-y-2">
						<p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('attachments'); ?></p>
						<div class="grid gap-3 sm:grid-cols-2">
							<?php foreach ($uploaded_file as $fKey => $v_files) :
								if (empty($v_files)) {
									continue;
								}
							?>
								<a class="flex items-center justify-between gap-2 rounded-lg border border-border bg-white px-3 py-2 text-sm text-foreground hover:border-slate-300"
									href="<?= base_url('admin/tickets/download_file/' . $tickets_info->tickets_id . '/' . $fKey); ?>">
									<span class="flex items-center gap-2">
										<i data-lucide="<?= !empty($v_files->is_image) ? 'image' : 'file-text'; ?>" class="h-4 w-4 text-muted-foreground"></i>
										<?= html_escape($v_files->fileName); ?>
									</span>
									<span class="text-xs text-muted-foreground"><?= (int) $v_files->size; ?> <?= lang('kb'); ?></span>
								</a>
							<?php endforeach; ?>
						</div>
					</div>
				<?php endif; ?>
			</div>

			<div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
				<h3 class="text-sm font-semibold text-foreground mb-3"><?= lang('reply_ticket'); ?></h3>
				<?php echo form_open(base_url("admin/tickets/save_tickets_reply/" . $tickets_info->tickets_id), array(
					"id" => "tickets-comment-form",
					"class" => "space-y-4",
					"enctype" => "multipart/form-data",
					"role" => "form"
				)); ?>
					<textarea class="w-full rounded-lg border border-border bg-input px-3 py-2 text-sm reply-body" name="body" rows="4"
						placeholder="<?= lang('tickets') ?> #<?= html_escape($tickets_info->ticket_code); ?> <?= lang('reply'); ?>"></textarea>
					<?= form_attachment_group('files[]', null, 'dropzone', array(
						'dropzone_id' => 'ticket-reply-dropzone',
						'list_id' => 'ticket-reply-list',
					)); ?>
					<div class="flex justify-end">
						<button type="submit" id="file-save-button" class="btn btn-primary"><?= lang('reply'); ?></button>
					</div>
				<?php echo form_close(); ?>
			</div>

			<?php
			$ticket_replies = $this->db->where(array('tickets_id' => $tickets_info->tickets_id, 'ticket_reply_id' => 0))->order_by('time', 'DESC')->get('tbl_tickets_replies')->result();
			$this->load->view('admin/tickets/tickets_reply', array('ticket_replies' => $ticket_replies));
			?>
		</div>
	</div>
</div>

<script>
	$(document).ready(function() {
		$("#tickets-comment-form").appForm({
			isModal: false,
			onSuccess: function(result) {
				$(".reply-body").val("");
				$('#file-save-button').removeClass("disabled").html('<?= lang('post_comment') ?>');
				$(result.data).insertAfter("#tickets-comment-form");
				toastr[result.status](result.message);
			}
		});
	});
</script>
