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

<?= message_box('success'); ?>
<?= message_box('error'); ?>
<script src="<?php echo base_url(); ?>assets/plugins/bootstrap-tagsinput/fm.tagator.jquery.js"></script>

<?php
$deposit_id = !empty($deposit_info) ? $deposit_info->transactions_id : null;
$transaction_prefix = !empty($deposit_info->transaction_prefix) ? $deposit_info->transaction_prefix : ($transaction_prefix_default ?? '');
$deposit_name = !empty($deposit_info->name) ? strip_html_tags($deposit_info->name) : '';
$selected_account = !empty($deposit_info->account_id) ? $deposit_info->account_id : '';
$selected_account_label = $selected_account_label ?? '';
$selected_category = !empty($deposit_info->category_id) ? $deposit_info->category_id : '0';
$selected_payment_method = !empty($deposit_info->payment_methods_id) ? $deposit_info->payment_methods_id : '0';
$selected_paid_by_id = $selected_paid_by_id ?? '';
$selected_paid_by_label = $selected_paid_by_label ?? '';
$selected_project_id = $selected_project_id ?? '';
$selected_project_label = $selected_project_label ?? '';
$project_client_id = $project_client_id ?? '';
$project_client_label = $project_client_label ?? '';
$deposit_date = !empty($deposit_info->date) ? $deposit_info->date : date('Y-m-d');
$deposit_amount = !empty($deposit_info->amount) ? $deposit_info->amount : '';
$deposit_notes = !empty($deposit_info->notes) ? $deposit_info->notes : '';
$deposit_reference = !empty($deposit_info->reference) ? $deposit_info->reference : '';
$deposit_tags = !empty($deposit_info->tags) ? $deposit_info->tags : '';
$permission_value = !empty($deposit_info->permission) ? $deposit_info->permission : null;
$disable_account = (!empty($deposit_info) && $deposit_info->account_id != '0');
$disable_amount = !empty($deposit_info);
$page_title = !empty($deposit_info) ? (lang('edit') . ' ' . lang('deposit')) : lang('new_deposit');
$page_subtitle = !empty($transaction_prefix) ? ('#' . $transaction_prefix) : (lang('deposit') . ' ' . lang('details'));

$new_account_link = '';
$account_created = can_action('36', 'created');
if (!empty($account_created)) {
	$new_account_link = form_lookup_add_link('account_id', base_url('admin/account/new_account'), lang('new') . ' ' . lang('account'), 'xl');
}

$new_category_link = '';
$category_created = can_action('125', 'created');
if (!empty($category_created)) {
	$new_category_link = form_select_add_link('category_id', base_url('admin/transactions/categories/income'), lang('new') . ' ' . lang('deposit_category'), 'sm');
}

$new_paid_by_link = '';
$paid_by_created = can_action('4', 'created');
if (!empty($paid_by_created)) {
	$new_paid_by_link = form_lookup_add_link('paid_by', base_url('admin/client/new_client'), lang('new') . ' ' . lang('paid_by'), 'md');
}

$new_payment_link = form_select_add_link('payment_methods_id', base_url('admin/settings/inline_payment_method'), lang('new') . ' ' . lang('payment_method'), 'md');

$page_actions = '<a href="' . base_url('admin/transactions/deposit') . '" class="erp-btn"><i data-lucide="list" class="h-4 w-4"></i><span>' . lang('all_deposit') . '</span></a>';
$page_meta = $this->load->view('admin/common/erp/badge', array(
	'label' => lang('deposit'),
	'tone' => 'success',
	'icon' => 'arrow-down-left',
), true);
if (!empty($transaction_prefix)) {
	$page_meta .= $this->load->view('admin/common/erp/badge', array(
		'label' => '#' . $transaction_prefix,
		'tone' => 'neutral',
	), true);
}
?>

<?php echo form_open(
	base_url('admin/transactions/save_deposit/' . $deposit_id),
	array('class' => 'erp-page', 'enctype' => 'multipart/form-data', 'data-parsley-validate' => 'true', 'id' => 'deposit-form')
); ?>

<div class="erp-page">
	<?= $this->load->view('admin/common/erp/page_header', array(
		'title' => $page_title,
		'subtitle' => $page_subtitle,
		'actions' => $page_actions,
		'meta' => $page_meta,
	), true); ?>

	<div class="grid gap-4 xl:grid-cols-[minmax(0,1.2fr)_minmax(0,0.8fr)]">
		<div class="space-y-4">
			<section class="erp-panel" id="deposit-general">
				<?= $this->load->view('admin/common/erp/section_header', array(
					'title' => lang('general'),
					'subtitle' => 'Core deposit details used for ledger posting.',
				), true); ?>
				<div class="erp-panel-body grid gap-3 md:grid-cols-2">
					<?= form_input_group('transaction_prefix', lang('deposit') . ' ' . lang('prefix'), $transaction_prefix, 'text', true); ?>
					<?= form_input_group('name', lang('name') . '/' . lang('title'), $deposit_name, 'text', true, array('placeholder' => lang('enter') . ' ' . lang('name') . '/' . lang('title') . ' ' . lang('for_personal'))); ?>

					<?php if (!empty($selected_project_id)) { ?>
						<?= form_input_group('project_label', lang('project'), $selected_project_label, 'text', false, array('readonly' => 'readonly')); ?>
						<input type="hidden" name="project_id" value="<?= html_escape($selected_project_id); ?>">
					<?php } else { ?>
						<?= form_lookup_group('project_id', lang('project'), '', '', false, array(), '', array('type' => 'project')); ?>
					<?php } ?>

					<?= form_datepicker('date', lang('date'), $deposit_date, false, array()); ?>

					<?php if ($disable_account) { ?>
						<?= form_input_group('account_label', lang('account'), $selected_account_label, 'text', false, array('readonly' => 'readonly')); ?>
						<input type="hidden" name="account_id" value="<?= html_escape($selected_account); ?>">
					<?php } else { ?>
						<?= form_lookup_group('account_id', lang('account'), $selected_account, $selected_account_label, true, array(), '', array('type' => 'account', 'append' => $new_account_link)); ?>
					<?php } ?>

					<?= form_input_group('amount', lang('amount'), $deposit_amount, 'text', true, array('data-parsley-type' => 'number', 'disabled' => $disable_amount ? 'disabled' : null)); ?>
					<?= form_textarea_group('notes', lang('notes'), $deposit_notes, 3, false, array()); ?>
				</div>
			</section>

			<section class="erp-panel" id="deposit-info">
				<?= $this->load->view('admin/common/erp/section_header', array(
					'title' => lang('additional_info'),
					'subtitle' => 'Classification, references, and payment source.',
				), true); ?>
				<div class="erp-panel-body grid gap-3 md:grid-cols-2">
					<?= form_select_group('category_id', lang('deposit_category'), array('0' => lang('none')) + ($income_category_options ?? array()), $selected_category, false, array('class' => 'select_box'), '', $new_category_link); ?>

					<?php if (!empty($project_client_id)) { ?>
						<?= form_input_group('paid_by_label', lang('paid_by'), $project_client_label, 'text', false, array('readonly' => 'readonly')); ?>
						<input type="hidden" name="paid_by" value="<?= html_escape($project_client_id); ?>">
					<?php } else { ?>
						<?= form_lookup_group('paid_by', lang('paid_by'), $selected_paid_by_id, $selected_paid_by_label, false, array(), '', array('type' => 'client', 'append' => $new_paid_by_link)); ?>
					<?php } ?>

					<?= form_select_group('payment_methods_id', lang('payment_method'), array('0' => lang('payment_method')) + ($payment_method_options ?? array()), $selected_payment_method, false, array('class' => 'select_box'), '', $new_payment_link); ?>

					<div class="space-y-1">
						<?= form_input_group('reference', lang('reference'), $deposit_reference, 'text', false); ?>
						<p class="text-xs text-muted-foreground"><?= lang('reference_example'); ?></p>
					</div>

					<?= form_tags_group($deposit_tags); ?>
				</div>
			</section>
		</div>

		<div class="space-y-4">
			<section class="erp-panel" id="deposit-attachments">
				<?= $this->load->view('admin/common/erp/section_header', array(
					'title' => lang('attachment'),
					'subtitle' => 'Keep source documents with the transaction.',
				), true); ?>
				<div class="erp-panel-body space-y-4">
					<?= form_attachment_group('files[]', lang('attachment'), 'dropzone', array(
						'dropzone_id' => 'deposit-files-dropzone',
						'list_id' => 'deposit-files-list',
					)); ?>

					<?php
					$uploaded_file = !empty($deposit_info->attachement) ? json_decode($deposit_info->attachement) : 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="flex flex-wrap gap-1">
								<?php foreach ($uploaded_file as $v_files_image) { ?>
									<div class="group flex min-w-0 max-w-full items-center gap-2 rounded-lg border border-border bg-background px-3 py-2 shadow-sm transition-colors hover:bg-muted/40 dark:bg-slate-900/60 dark:hover:bg-slate-800 sm:max-w-[260px]">
										<div class="flex min-w-0 flex-1 items-center gap-2">
											<span class="flex h-7 w-7 flex-shrink-0 items-center justify-center rounded-md border border-border bg-card text-muted-foreground dark:bg-slate-800">
												<i data-lucide="<?= !empty($v_files_image->is_image) ? 'image' : 'file-text'; ?>" class="h-4 w-4"></i>
											</span>
											<span class="min-w-0 truncate text-sm font-medium text-foreground" title="<?= html_escape($v_files_image->fileName); ?>"><?= html_escape($v_files_image->fileName); ?></span>
										</div>
										<button type="button" class="existing_image inline-flex h-7 w-7 flex-shrink-0 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-rose-50 hover:text-rose-600 dark:hover:bg-rose-950/40 dark:hover:text-rose-300" 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); ?>">
									</div>
								<?php } ?>
							</div>
						</div>
					<?php } ?>
				</div>
			</section>

			<?php if (!empty($deposit_info)) { ?>
				<input type="hidden" name="amount" value="<?= html_escape($deposit_amount); ?>">
				<input type="hidden" name="old_account_id" value="<?= html_escape($selected_account); ?>">
			<?php } ?>

			<section class="erp-panel">
				<?= $this->load->view('admin/common/erp/section_header', array(
					'title' => lang('additional_fields'),
					'subtitle' => 'Custom fields attached to this deposit record.',
				), true); ?>
				<div class="erp-panel-body space-y-4">
					<?php
					$transactions_id = !empty($deposit_info) ? $deposit_info->transactions_id : null;
					echo custom_form_Fields(1, $transactions_id);
					?>
				</div>
			</section>

			<section class="erp-panel" id="deposit-permission">
				<?= $this->load->view('admin/common/erp/section_header', array(
					'title' => lang('permission'),
					'subtitle' => 'Restrict visibility when the entry is not public.',
				), true); ?>
				<div class="erp-panel-body space-y-4">
					<?= form_permission_modern($permission_user, $permission_value, lang('assined_to')); ?>
				</div>
			</section>
		</div>
	</div>

	<div class="flex flex-wrap justify-end gap-2 rounded-xl border border-border bg-card px-4 py-3 shadow-sm">
		<?php if (!empty($deposit_info)) { ?>
			<?= btn_update('updates', 'sm'); ?>
			<button type="button" onclick="goBack()" class="btn btn-danger btn-sm h-9 mb-1"><?= lang('cancel'); ?></button>
		<?php } else { ?>
			<?= btn_update('save', 'sm'); ?>
		<?php } ?>
	</div>
</div>

<?php echo form_close(); ?>

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