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

<?php
$projects_mode = $this->uri->segment(4);
$status_segment = $this->uri->segment(3);
$ticket_code_value = !empty($tickets_info->ticket_code) ? $tickets_info->ticket_code : ($ticket_code_default ?? '');
$selected_priority = !empty($tickets_info->priority) ? $tickets_info->priority : ($default_priority ?? '');
$selected_department = !empty($tickets_info->departments_id) ? $tickets_info->departments_id : ($default_department ?? '');
$selected_project_id = $selected_project_id ?? (!empty($tickets_info->project_id) ? $tickets_info->project_id : '');
$selected_project_label = $selected_project_label ?? '';
$selected_reporter_id = $selected_reporter_id ?? (!empty($tickets_info->reporter) ? $tickets_info->reporter : '');
$selected_reporter_label = $selected_reporter_label ?? '';

if (!empty($tickets_info)) {
	$tickets_id = $tickets_info->tickets_id;
} else {
	$tickets_id = null;
}

$permission_value = !empty($tickets_info->permission) ? $tickets_info->permission : null;

?>

<?php echo form_open(
	base_url('admin/tickets/create_tickets/' . (!empty($tickets_info) ? $tickets_info->tickets_id : '')),
	array('class' => 'space-y-6', 'enctype' => 'multipart/form-data', 'data-parsley-validate' => 'true', 'id' => 'new_form')
); ?>

<?php if ($projects_mode != 'project_tickets') { ?>
	<input type="hidden" name="status" value="<?= html_escape($status_segment); ?>">
<?php } ?>

<div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
	<!-- Left: primary fields -->
	<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 font-semibold text-sm 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('general'); ?>
			</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_value, 'text', true); ?>
				<?= form_input_group('subject', lang('subject'), (!empty($tickets_info->subject) ? $tickets_info->subject : ''), 'text', true, array('placeholder' => lang('subject'))); ?>

				<?php if ($this->session->userdata('user_type') == '1') { ?>
					<?= form_lookup_group('reporter', lang('reporter'), $selected_reporter_id, $selected_reporter_label, true, array(), '', array('type' => 'user')); ?>
				<?php } else { ?>
					<input type="hidden" name="reporter" value="<?= (int) $this->session->userdata('user_id'); ?>">
				<?php } ?>

				<?= form_lookup_group('project_id', lang('project'), $selected_project_id, $selected_project_label, false, array(), '', array('type' => 'project')); ?>

				<?= form_select_group('priority', lang('priority'), $priority_options ?? array(), $selected_priority, true, array('class' => 'select_box')); ?>

				<?php
				$new_department_link = '';
				$department_created = can_action('70', 'created');
				if (!empty($department_created)) {
					$new_department_link = form_select_add_link('departments_id', base_url('admin/departments/edit_departments/inline/true'), lang('new') . ' ' . lang('department'), 'sm');
				}
				?>
				<?= form_select_group('departments_id', lang('department'), $department_options ?? array(), $selected_department, true, array('class' => 'select_box'), '', $new_department_link); ?>

				<?= form_tags_group(!empty($tickets_info->tags) ? $tickets_info->tags : ''); ?>

			</div>
			<?= 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 font-semibold text-sm text-slate-900 mb-3 border-b border-slate-200 pb-2 dark:text-slate-100 dark:border-slate-700">
				<i data-lucide="dollar-sign" class="h-5 w-5"></i>
				<?= lang('files'); ?>
			</h4>
			<?= custom_form_Fields(7, $tickets_id); ?>

			<?= 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 class="rounded-2xl border border-border bg-card p-5 shadow-sm space-y-4"> -->
		<div class="rounded-2xl bg-card shadow-sm space-y-4">
			<h3 class="text-sm font-semibold text-foreground"><?= lang('permission'); ?></h3>
			<?= form_permission_modern($permission_user, $permission_value, lang('assined_to')); ?>

		</div>
	</div>
</div>

<div class="flex justify-start gap-2">
	<?php if (!empty($tickets_info)) { ?>
		<?= btn_update('updates', 'sm'); ?>
		<a href="<?= base_url('admin/tickets'); ?>" class="btn btn-danger btn-sm mb-1"><?= lang('cancel'); ?></a>
	<?php } else { ?>
		<?= btn_update('create_ticket', 'sm'); ?>
	<?php } ?>
</div>

<?php echo form_close(); ?>

<script>
	$(document).ready(function() {
		$(".existing_image").on("click", function() {
			$(this).closest(".rounded-lg").remove();
		});
	});
</script>
