<?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_designation_html = form_lookup_add_link(
	'designation_id',
	base_url('admin/promotion/new_designation'),
	lang('create') . ' ' . lang('designation'),
);



$selected_employee_name = !empty($promotions->user_id) ? $promotions->user_id : (!empty($employee_id) ? $employee_id : '');
$selected_employee_name_label = null;
$employee_lookup_groups = grouped_staff_lookup_groups();
if (!empty($promotions->user_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', $promotions->user_id);
	$user = $this->db->get()->row();
	if (!empty($user)) {
		$selected_employee_name_label = $user->fullname . (!empty($user->designations) ? ' (' . $user->designations . ')' : '');
	}
}
$selected_designation_name = !empty($promotions->designation_id) ? $promotions->designation_id : (!empty($designation_id) ? $designation_id : '');
$selected_designation_name_label = null;
if (!empty($selected_designation_name)) {
	$designation = $this->db->where('designations_id', $selected_designation_name)->get('tbl_designations')->row();
	if (!empty($designation)) {
		$selected_designation_name_label = $designation->designations;
	}
}
$designation_lookup_groups = array();
if (!empty($all_department_info)) {
	foreach ($all_department_info as $department_rows) {
		if (empty($department_rows) || !is_array($department_rows)) {
			continue;
		}
		$group_label = '';
		$items = array();
		foreach ($department_rows as $designation_row) {
			if (empty($designation_row->designations_id) || empty($designation_row->designations)) {
				continue;
			}
			if ($group_label === '' && !empty($designation_row->deptname)) {
				$group_label = $designation_row->deptname;
			}
			$items[] = array(
				'id' => (string) $designation_row->designations_id,
				'text' => (string) $designation_row->designations,
				'name' => (string) $designation_row->designations,
			);
		}
		if (!empty($items)) {
			$designation_lookup_groups[] = array(
				'label' => $group_label !== '' ? $group_label : lang('department'),
				'items' => $items,
			);
		}
	}
}

if (!empty($created) || !empty($edited)) {
?>
	<div class="panel panel-custom">
		<div class="modal-body wrap-modal wrap">
			<?php echo form_open(base_url('admin/promotion/save_promotion/' . (!empty($promotions->promotion_id) ? $promotions->promotion_id : '')), array('class' => '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_input_group('promotion_title', lang('promotion_title'), (!empty($promotions->promotion_title) ? $promotions->promotion_title : ''), 'text', true); ?>

							<?= form_grouped_lookup_group('user_id', lang('employee'), $employee_lookup_groups, $selected_employee_name, $selected_employee_name_label, true, array(), '', array('append' => $new_employee_html, 'helper' => 'Browse by department or search employee name.')); ?>
							<?= form_grouped_lookup_group('designation_id', lang('designation'), $designation_lookup_groups, $selected_designation_name, $selected_designation_name_label, true, array(), '', array('append' => $new_designation_html, 'helper' => 'Grouped by department. Select the designation for this promotion.')); ?>



							<?= form_datepicker('promotion_date', lang('promotion_date'), !empty($promotions->promotion_date) ? date('Y-m-d', safe_strtotime($promotions->promotion_date)) : '', true); ?>

							<?= form_textarea_group('description', lang('description'), !empty($promotions->description) ? $promotions->description : '', 4); ?>

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

						</div>
					</div>

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