<?php
$created = can_action_by_label('chart_of_accounts', 'created');
$edited = can_action_by_label('chart_of_accounts', 'edited');
if (!empty($created) || !empty($edited)) {
	$account_id = !empty($chart_of_account->chart_of_account_id) ? $chart_of_account->chart_of_account_id : null;
	$status_selected = isset($chart_of_account->status) ? (string) $chart_of_account->status : '1';
	$status_options = array(
		'1' => lang('active'),
		'0' => lang('inactive'),
	);

	$account_sub_type_id = !empty($chart_of_account->account_sub_type_id) ? $chart_of_account->account_sub_type_id : '';
	$selected_account_sub_type_label = null;
	if (!empty($chart_of_account->account_sub_type_id)) {
		$account_sub_type = $this->db->where('account_sub_type_id', $chart_of_account->account_sub_type_id)->get('tbl_account_sub_type')->row();
		if (!empty($account_sub_type)) {
			$selected_account_sub_type_label = $account_sub_type->account_sub_type;
		}
	}
?>

	<div class="p-0">
		<?= form_open(base_url('admin/accounting/save_chart_of_account/' . $account_id), array('class' => 'space-y-4', 'data-parsley-validate' => '', 'novalidate' => '', 'enctype' => 'multipart/form-data')); ?>
		<div class="grid grid-cols-1 md:grid-cols-2 gap-5">
			<?= form_select_group(
				'account_type_id',
				lang('account_type'),
				$account_types,
				!empty($chart_of_account),
				true,
				array('class' => 'select_box', 'onchange' => 'get_account_sub_types(this.value);')
			); ?>
			
			<?= form_lookup_group('account_sub_type_id', lang('account_sub_type'), $account_sub_type_id, $selected_account_sub_type_label, true, array(), '', array('type' => 'account_sub_type')); ?>

			<?= form_input_group('code', lang('account_code'), !empty($chart_of_account->code) ? $chart_of_account->code : '', 'text', true); ?>
			<?= form_input_group('name', lang('account_name'), !empty($chart_of_account->name) ? $chart_of_account->name : '', 'text', true); ?>
			<?= form_select_group('status', lang('status'), $status_options, $status_selected); ?>
			<?= form_textarea_group('notes', lang('description'), !empty($chart_of_account->notes) ? $chart_of_account->notes : '', 3); ?>
		</div>
		<div class="flex justify-end gap-2 border-t border-slate-200 pt-4 dark:border-slate-700">
			<button type="button" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground hover:bg-muted" data-dismiss="modal">
				<i data-lucide="x" class="h-4 w-4"></i>
				<?= lang('close') ?>
			</button>
			<button type="submit" class="inline-flex items-center gap-2 rounded-lg bg-indigo-600 px-3 py-2 text-xs font-semibold text-white shadow-sm hover:bg-indigo-500">
				<i data-lucide="check" class="h-4 w-4"></i>
				<?= lang('save') ?>
			</button>
		</div>
		<?= form_close(); ?>
	</div>
	<script type="text/javascript">
		function get_account_sub_types(account_type_id) {
			$('#account_sub_type_id').html('<option value=""><?= lang('loading') ?></option>');
			if ($('#account_sub_type_id').selectpicker) {
				$('#account_sub_type_id').selectpicker('refresh');
			}
			$.ajax({
				url: '<?php echo base_url(); ?>admin/accounting/get_account_sub_types/' + account_type_id,
				type: 'POST',
				dataType: 'json',
				success: function(data) {
					if (data.success) {
						$('#account_sub_type_id').html(data.html);
						if ($('#account_sub_type_id').selectpicker) {
							$('#account_sub_type_id').selectpicker('refresh');
						}
					}
				}
			});
		}
	</script>
<?php } ?>
