<?php
$created = can_action('100', 'created');
$edited = can_action('100', 'edited');

$new_warning_by_html = form_lookup_add_link(
	'warning_by',
	base_url('admin/user/create'),
	lang('create') . ' ' . lang('employee'),
	'2xl'
);
$new_warning_to_html = form_lookup_add_link(
	'warning_to',
	base_url('admin/user/create'),
	lang('create') . ' ' . lang('employee'),
	'2xl'
);
$new_category_html = form_lookup_add_link(
	'warning_type',
	base_url('admin/warning/new_warning_type'),
	lang('create') . ' ' . lang('warning_type'),
	'sm'
);

$selected_warning_by = !empty($warnings->warning_by) ? $warnings->warning_by : (!empty($user_id) ? $user_id : '');
$selected_warning_by_label = null;
$employee_lookup_groups = grouped_staff_lookup_groups();
if (!empty($warnings->warning_by)) {
	$this->db->select('tbl_account_details.fullname, tbl_designations.designations');
	$this->db->from('tbl_account_details');
	$this->db->join('tbl_designations', 'tbl_designations.designations_id = tbl_account_details.designations_id', 'left');
	$this->db->where('tbl_account_details.user_id', $warnings->warning_by);
	$user = $this->db->get()->row();
	if (!empty($user)) {
		$selected_warning_by_label = $user->fullname . (!empty($user->designations) ? ' (' . $user->designations . ')' : '');
	}
}

$selected_warning_to = !empty($warnings->warning_to) ? $warnings->warning_to : (!empty($user_id) ? $user_id : '');
$selected_warning_to_label = null;
if (!empty($warnings->warning_to)) {
	$this->db->select('tbl_account_details.fullname, tbl_designations.designations');
	$this->db->from('tbl_account_details');
	$this->db->join('tbl_designations', 'tbl_designations.designations_id = tbl_account_details.designations_id', 'left');
	$this->db->where('tbl_account_details.user_id', $warnings->warning_to);
	$user = $this->db->get()->row();
	if (!empty($user)) {
		$selected_warning_to_label = $user->fullname . (!empty($user->designations) ? ' (' . $user->designations . ')' : '');
	}
}
$selected_warning_type = !empty($warnings->warning_type) ? $warnings->warning_type : '';
$selected_warning_type_label = null;
if (!empty($warnings->warning_type)) {
	$group_info = $this->db
		->where('customer_group_id', $warnings->warning_type)
		->where('type', 'warning')
		->get('tbl_customer_group')
		->row();
	if (!empty($group_info)) {
		$selected_warning_type_label = $group_info->customer_group;
	}
}
if (!empty($created) || !empty($edited)) {
?>
	<div class="panel panel-custom">
		<div class="modal-body wrap-modal wrap">
			<?php echo form_open(base_url('admin/warning/save_warning/' . (!empty($warnings->id) ? $warnings->id : '')), array('class' => '', 'enctype' => 'multipart/form-data', 'data-parsley-validate' => 'true', 'role' => 'form')); ?>

			<div class="grid gap-8">
				<!-- Main columns -->
				<div class="lg:col-span-2 space-y-4">

					<div class="bg-slate-50 p-4 rounded-xl border border-slate-200 dark:bg-slate-800 dark:border-slate-700">
						<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
							<?= form_input_group('subject', lang('subject'), (!empty($warnings->subject) ? $warnings->subject : ''), 'text', true); ?>
							<?= form_grouped_lookup_group('warning_by', lang('warning_by'), $employee_lookup_groups, $selected_warning_by, $selected_warning_by_label, true, array(), '', array('append' => $new_warning_by_html, 'helper' => 'Grouped by department for faster issuer selection.')); ?>
							<?= form_grouped_lookup_group('warning_to', lang('warning_to'), $employee_lookup_groups, $selected_warning_to, $selected_warning_to_label, true, array(), '', array('append' => $new_warning_to_html, 'helper' => 'Select the employee receiving the warning.')); ?>
							<?= form_lookup_group('warning_type', lang('warning_type'), $selected_warning_type, $selected_warning_type_label, false, array(), '', array('type' => 'category', 'endpoint' => base_url('admin/common/warning_type_lookup'), 'extra' => array('type' => 'warning'), 'append' => $new_category_html)); ?>
							<?= form_datepicker('warning_date', lang('warning_date'), !empty($warnings->warning_date) ? date('Y-m-d', safe_strtotime($warnings->warning_date)) : '', true); ?>
							<?= form_attachment_group('files[]', lang('attachments'), 'dropzone', [
								'dropzone_id' => 'attachements-files-dropzone',
								'list_id' => 'attachements-files-list',
								'comment' => true
							]); ?>
							<?php
							$uploaded_file = array();
							if (!empty($warnings->attachment)) {
								$decoded_files = json_decode($warnings->attachment);
								$uploaded_file = is_array($decoded_files) ? $decoded_files : array();
							}
							if (!empty($uploaded_file)) { ?>
								<div class="md:col-span-2 space-y-2">
									<p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('existing') . ' ' . lang('attachments'); ?></p>
									<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:border-gray-700 dark:bg-gray-800 dark:hover:border-gray-500">
												<div class="flex min-w-0 items-center gap-2">
													<span class="flex h-8 w-8 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" aria-label="<?= lang('delete'); ?>">
													<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 ?? 0); ?>">
											</div>
										<?php } ?>
									</div>
								</div>
							<?php } ?>
							<?= form_textarea_group('description', lang('description'), !empty($warnings->description) ? $warnings->description : '', 4); ?>

						</div>
						<div class="flex items-center gap-2 mt-6">
							<?= btn_update(!empty($warnings) ? lang('updates') : lang('save'), 'md') ?>

						</div>
					</div>

					<?php echo form_close(); ?>
				</div>
			</div>
		</div>
	</div>
	<script>
		$(document).on('click', '.existing_image', function() {
			$(this).closest('.group').remove();
		});
	</script>
<?php } ?>
