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

$new_employee_html = form_lookup_add_link(
	'user_id',
	base_url('admin/user/create'),
	lang('create') . ' ' . lang('employee'),
	'2xl'
);
$new_category_html = form_lookup_add_link(
	'termination_type',
	base_url('admin/termination/new_termination_type'),
	lang('create') . ' ' . lang('termination_type'),
	'sm'
);

$selected_employee_name = !empty($terminations->employee_id) ? $terminations->employee_id : (!empty($user_id) ? $user_id : '');
$selected_employee_name_label = null;
$employee_lookup_groups = grouped_staff_lookup_groups();
if (!empty($terminations->employee_id)) {
	$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', $terminations->employee_id);
	$user = $this->db->get()->row();
	if (!empty($user)) {
		$selected_employee_name_label = $user->fullname . (!empty($user->designations) ? ' (' . $user->designations . ')' : '');
	}
}

$selected_termination_type = !empty($terminations->termination_type) ? $terminations->termination_type : '';
$selected_termination_type_label = null;
if (!empty($terminations->termination_type)) {
	$group_info = $this->db->where('customer_group_id', $terminations->termination_type)->get('tbl_customer_group')->row();
	if (!empty($group_info)) {
		$selected_termination_type_label = $group_info->customer_group;
	}
}
$existing_attachments = array();
if (!empty($terminations->attachment)) {
	$decoded_attachments = json_decode($terminations->attachment);
	if (is_array($decoded_attachments)) {
		$existing_attachments = $decoded_attachments;
	}
}
if (!empty($created) || !empty($edited)) {
?>
	<div class="panel panel-custom">
		<div class="modal-body wrap-modal wrap">
			<?php echo form_open(base_url('admin/termination/save_termination/' . (!empty($terminations->id) ? $terminations->id : '')), array('class' => 'p-0 space-y-6 ', '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_grouped_lookup_group('employee_id', lang('employee_name'), $employee_lookup_groups, $selected_employee_name, $selected_employee_name_label, true, array(), '', array('append' => $new_employee_html, 'helper' => 'Select the employee from the department-grouped directory.')); ?>
							<?= form_lookup_group('termination_type', lang('termination_type'), $selected_termination_type, $selected_termination_type_label, false, array(), '', array('type' => 'category', 'endpoint' => base_url('admin/common/termination_type_lookup'), 'extra' => ['type' => 'termination'], 'append' => $new_category_html)); ?>
							<?= form_datepicker('notice_date', lang('notice_date'), !empty($terminations->notice_date) ? date('Y-m-d', safe_strtotime($terminations->notice_date)) : '', true); ?>
							<?= form_datepicker('termination_date', lang('termination_date'), !empty($terminations->termination_date) ? date('Y-m-d', safe_strtotime($terminations->termination_date)) : '', true); ?>
							<?= form_attachment_group('files[]', lang('attachments'), 'dropzone', [
								'dropzone_id' => 'attachements-files-dropzone',
								'list_id' => 'attachements-files-list',
								'comment' => true,
								'existing_files' => $existing_attachments,
								'download_url_base' => !empty($terminations->id) ? base_url('admin/termination/download_files/' . $terminations->id) : '',
							]); ?>
							<?= form_textarea_group('description', lang('description'), !empty($terminations->description) ? $terminations->description : '', 4); ?>
						</div>
						<div class="flex items-center gap-2 mt-6">
							<?= btn_update(!empty($terminations) ? lang('updates') : lang('save'), 'md') ?>

						</div>
					</div>

					<?php echo form_close(); ?>
				</div>
			</div>
		</div>
	</div>
<?php } ?>
