<?= message_box('success'); ?>
<?= message_box('error'); ?>
<?php
$this->load->helper('string');

$ticket_id = !empty($tickets_info) ? $tickets_info->tickets_id : null;
$ticket_code = !empty($tickets_info) ? $tickets_info->ticket_code : strtoupper(random_string('alnum', 7));
$subject = !empty($tickets_info) ? $tickets_info->subject : '';
$body = !empty($tickets_info) ? $tickets_info->body : set_value('body');

$project_opts = array('' => lang('select') . ' ' . lang('project'));
$project_id = $this->uri->segment(6);
$selected_project = !empty($tickets_info) ? $tickets_info->project_id : '';
if (!empty($project_id)) {
	$selected_project = $project_id;
}
$project = $this->db->where('client_id', $this->session->userdata('client_id'))->get('tbl_project')->result();
if (!empty($project)) {
	foreach ($project as $v_project) {
		$project_opts[$v_project->project_id] = $v_project->project_name;
	}
}

$priority_opts = array();
$priorities = $this->db->get('tbl_priority')->result();
if (!empty($priorities)) {
	foreach ($priorities as $v_priorities) {
		$priority_opts[$v_priorities->priority] = $v_priorities->priority;
	}
}
$selected_priority = !empty($tickets_info)
	? $tickets_info->priority
	: config_item('default_priority');

$department_opts = array();
$all_departments = $this->db->get('tbl_departments')->result();
if (!empty($all_departments)) {
	foreach ($all_departments as $v_dept) {
		$department_opts[$v_dept->departments_id] = $v_dept->deptname;
	}
}
$selected_department = !empty($tickets_info)
	? $tickets_info->departments_id
	: config_item('default_department');
?>

<div class="space-y-4">
	<div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
		<div>
			<h2 class="text-xl font-semibold text-foreground"><?= lang('new_ticket'); ?></h2>
			<p class="text-sm text-muted-foreground"><?= lang('tickets'); ?></p>
		</div>
		<div class="flex flex-wrap gap-2">
			<a href="<?= base_url('client/tickets'); ?>" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-sm font-medium text-foreground hover:bg-muted">
				<i data-lucide="list" class="h-4 w-4"></i>
				<?= lang('tickets'); ?>
			</a>
		</div>
	</div>

	<div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
		<form method="post" data-parsley-validate="" novalidate=""
			action="<?= base_url() ?>client/tickets/create_tickets/<?php echo $ticket_id; ?>"
			enctype="multipart/form-data" class="space-y-6">
			<div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
				<div class="lg:col-span-2 space-y-5">
					<div class="bg-slate-50 p-4 rounded-xl border border-slate-200 dark:bg-slate-800 dark:border-slate-700">
						<h4 class="text-sm font-semibold text-foreground flex items-center gap-2 text-slate-900 mb-3 border-b border-slate-200 pb-2 dark:text-slate-100 dark:border-slate-700">
							<i data-lucide="info" class="h-5 w-5"></i>
							<?= lang('ticket') . ' ' . lang('details'); ?>
						</h4>
						<div class="grid grid-cols-1 md:grid-cols-2 gap-5 mb-4">
							<?= form_input_group('ticket_code', lang('ticket_code'), $ticket_code, 'text', true); ?>
							<?= form_input_group('subject', lang('subject'), $subject, 'text', true, array('placeholder' => lang('subject'))); ?>
							<?= form_select_group('project_id', lang('project'), $project_opts, $selected_project, false, array('class' => 'select_box', 'data-search' => true)); ?>
							<?= form_select_group('priority', lang('priority'), $priority_opts, $selected_priority, true); ?>
							<?= form_select_group('departments_id', lang('department'), $department_opts, $selected_department, true, array('class' => 'select_box', 'data-search' => true)); ?>
						</div>
						<!-- <?= form_textarea_group('body', lang('ticket_message'), $body, 6, false, array('class' => 'textarea_')); ?> -->
						<?= form_editor('body', lang('ticket_message'), (!empty($tickets_info->body) ? $tickets_info->body : ''), false); ?>

					</div>
				</div>

				<div class="space-y-5">
					<div class="bg-slate-50 p-4 rounded-xl border border-slate-200 dark:bg-slate-800 dark:border-slate-700">
						<h4 class="text-sm font-semibold text-foreground flex items-center gap-2 text-slate-900 mb-3 border-b border-slate-200 pb-2 dark:text-slate-100 dark:border-slate-700">
							<i data-lucide="list-plus" class="h-5 w-5"></i>
							<?= lang('custom_fields'); ?>
						</h4>
						<?php
						$custom_ticket_id = !empty($ticket_id) ? $ticket_id : null;
						?>
						<?= custom_form_Fields(7, $custom_ticket_id); ?>
					</div>

					<div class="bg-slate-50 p-4 rounded-xl border border-slate-200 dark:bg-slate-800 dark:border-slate-700">
						<h4 class="text-sm font-semibold text-foreground flex items-center gap-2 text-slate-900 mb-3 border-b border-slate-200 pb-2 dark:text-slate-100 dark:border-slate-700">
							<i data-lucide="paperclip" class="h-5 w-5"></i>
							<?= lang('attachment'); ?>
						</h4>
						<?= form_attachment_group('files[]', lang('attachments'), 'dropzone', array(
							'dropzone_id' => 'tickets-files-dropzone',
							'list_id' => 'tickets-files-list',
							'comment' => true,
						)); ?>

						<?php
						$uploaded_file = !empty($tickets_info->upload_file) ? json_decode($tickets_info->upload_file) : array();
						if (!empty($uploaded_file)) {
						?>
							<div class="space-y-2">
								<p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('existing') . ' ' . lang('attachments'); ?></p>
								<div class="space-y-3">

									<div class="grid gap-3 sm:grid-cols-2">
										<?php foreach ($uploaded_file as $v_files_image) { ?>
											<div class="group flex items-center justify-between gap-3 rounded-lg border border-border bg-white px-2 py-2 shadow-sm transition-all hover:border-slate-300 dark:bg-gray-800 dark:border-gray-700 dark:hover:border-gray-500">
												<div class="flex min-w-0 items-center gap-1">
													<span class="flex h-6 w-6 shrink-0 items-center justify-center rounded-md bg-slate-100 text-slate-500 dark:bg-gray-700 dark:text-gray-300">
														<i data-lucide="<?= !empty($v_files_image->is_image) ? 'image' : 'file-text'; ?>" class="h-4 w-4"></i>
													</span>

													<span class="truncate text-sm font-medium text-foreground dark:text-gray-200" title="<?= html_escape($v_files_image->fileName); ?>">
														<?= html_escape($v_files_image->fileName); ?>
													</span>
												</div>

												<button type="button" class="existing_image shrink-0 rounded-md p-1 text-muted-foreground transition-colors hover:bg-red-100 hover:text-red-600 dark:text-gray-400 dark:hover:bg-red-900/30 dark:hover:text-red-400">
													<i data-lucide="x" class="h-4 w-4"></i>
												</button>

												<input type="hidden" name="path[]" value="<?= html_escape($v_files_image->path); ?>">
												<input type="hidden" name="fileName[]" value="<?= html_escape($v_files_image->fileName); ?>">
												<input type="hidden" name="fullPath[]" value="<?= html_escape($v_files_image->fullPath); ?>">
												<input type="hidden" name="size[]" value="<?= html_escape($v_files_image->size); ?>">
												<input type="hidden" name="is_image[]" value="<?= html_escape($v_files_image->is_image); ?>">
											</div>
										<?php } ?>
									</div>
								</div>
							</div>
						<?php } ?>
					</div>
				</div>
			</div>

			<div class="flex items-center gap-2">
				<button type="submit" class="inline-flex items-center rounded-lg bg-primary px-4 py-2 text-sm font-semibold text-white shadow hover:bg-primary/90 transition">
					<?= lang('create_ticket') ?>
				</button>
				<a href="<?= base_url('client/tickets'); ?>" class="inline-flex items-center rounded-lg border border-border bg-muted/40 px-4 py-2 text-sm font-semibold text-foreground hover:bg-muted">
					<?= lang('cancel') ?>
				</a>
			</div>
		</form>
	</div>
</div>
