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

<?= message_box('success'); ?>
<?= message_box('error'); ?>

<?php
$transfer_id = !empty($transfer_info) ? $transfer_info->transfer_id : null;
$from_account = !empty($transfer_info->from_account_id) ? $transfer_info->from_account_id : '';
$to_account = !empty($transfer_info->to_account_id) ? $transfer_info->to_account_id : '';
$selected_from_account_label = $selected_from_account_label ?? '';
$selected_to_account_label = $selected_to_account_label ?? '';
$transfer_date = !empty($transfer_info->date) ? $transfer_info->date : date('Y-m-d');
$transfer_notes = !empty($transfer_info->notes) ? $transfer_info->notes : '';
$transfer_amount = !empty($transfer_info->amount) ? $transfer_info->amount : '';
$payment_method = !empty($transfer_info->payment_methods_id) ? $transfer_info->payment_methods_id : '0';
$transfer_reference = !empty($transfer_info->reference) ? $transfer_info->reference : '';
$permission_value = !empty($transfer_info->permission) ? $transfer_info->permission : null;
$disable_accounts = !empty($transfer_info);
$disable_amount = !empty($transfer_info);
$page_title = !empty($transfer_info) ? (lang('edit') . ' ' . lang('transfer')) : lang('new_transfer');
$page_subtitle = !empty($transfer_info) ? (lang('transfer') . ' #' . $transfer_info->transfer_id) : (lang('transfer') . ' ' . lang('details'));

$payment_method_options = $payment_method_options ?? array();
$kpi_amount = $transfer_amount !== '' ? $transfer_amount : '0';
$kpi_from = $selected_from_account_label ?: '-';
$kpi_to = $selected_to_account_label ?: '-';

$new_from_account_link = '';
$new_to_account_link = '';
$account_created = can_action('36', 'created');
if (!empty($account_created)) {
	$new_from_account_link = form_lookup_add_link('from_account_id', base_url('admin/account/new_account'), lang('new') . ' ' . lang('account'), 'xl');
	$new_to_account_link = form_lookup_add_link('to_account_id', base_url('admin/account/new_account'), lang('new') . ' ' . lang('account'), 'xl');
}
?>

<?php echo form_open(
	base_url('admin/transactions/save_transfer/' . $transfer_id),
	array('class' => 'space-y-6', 'enctype' => 'multipart/form-data', 'data-parsley-validate' => 'true', 'id' => 'transfer-form')
); ?>

<div class="space-y-6">
	<div class="flex flex-col gap-2 md:flex-row md:items-center md:justify-between">
		<div>
			<h2 class="text-xl font-semibold text-foreground"><?= $page_title; ?></h2>
			<p class="text-sm text-muted-foreground"><?= html_escape($page_subtitle); ?></p>
		</div>
	</div>

	<div class="rounded-2xl border border-border bg-card shadow-sm">
		<div class="grid gap-3 sm:grid-cols-2 lg:grid-cols-4 p-4">
			<div class="rounded-xl border border-border bg-background p-3">
				<p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('amount'); ?></p>
				<div class="mt-2 text-lg font-semibold text-foreground" data-kpi="amount"><?= display_money((float) $kpi_amount, default_currency()); ?></div>
			</div>
			<div class="rounded-xl border border-border bg-background p-3">
				<p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('date'); ?></p>
				<div class="mt-2 text-sm font-semibold text-foreground" data-kpi="date"><?= display_date($transfer_date); ?></div>
			</div>
			<div class="rounded-xl border border-border bg-background p-3">
				<p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('from_account'); ?></p>
				<div class="mt-2 text-sm font-semibold text-foreground" data-kpi="from"><?= html_escape($kpi_from); ?></div>
			</div>
			<div class="rounded-xl border border-border bg-background p-3">
				<p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('to_account'); ?></p>
				<div class="mt-2 text-sm font-semibold text-foreground" data-kpi="to"><?= html_escape($kpi_to); ?></div>
			</div>
		</div>
	</div>

	<div class="grid gap-6 xl:grid-cols-2">
		<div class="space-y-6">
			<div class="rounded-2xl border border-border bg-card shadow-sm">
				<div class="flex items-center justify-between border-b border-border px-5 py-4">
					<h3 class="text-sm font-semibold text-foreground"><?= lang('general'); ?></h3>
				</div>
				<div class="p-5 grid gap-4 md:grid-cols-2">
					<?php if ($disable_accounts) { ?>
						<?= form_input_group('from_account_label', lang('from_account'), $selected_from_account_label, 'text', false, array('readonly' => 'readonly')); ?>
						<input type="hidden" name="from_account_id" value="<?= html_escape($from_account); ?>">
						<?= form_input_group('to_account_label', lang('to_account'), $selected_to_account_label, 'text', false, array('readonly' => 'readonly')); ?>
						<input type="hidden" name="to_account_id" value="<?= html_escape($to_account); ?>">
					<?php } else { ?>
						<?= form_lookup_group('from_account_id', lang('from_account'), $from_account, $selected_from_account_label, true, array(), '', array('type' => 'account', 'append' => $new_from_account_link)); ?>
						<?= form_lookup_group('to_account_id', lang('to_account'), $to_account, $selected_to_account_label, true, array(), '', array('type' => 'account', 'append' => $new_to_account_link)); ?>
					<?php } ?>

					<?= form_datepicker('date', lang('date'), $transfer_date, false, array()); ?>
					<?= form_input_group('amount', lang('amount'), $transfer_amount, 'text', true, array('data-parsley-type' => 'number', 'disabled' => $disable_amount ? 'disabled' : null)); ?>
				</div>
			</div>

			<div class="rounded-2xl border border-border bg-card shadow-sm">
				<div class="flex items-center justify-between border-b border-border px-5 py-4">
					<h3 class="text-sm font-semibold text-foreground"><?= lang('additional_info'); ?></h3>
				</div>
				<div class="p-5 grid gap-4 md:grid-cols-2">
					<?= form_select_group('payment_methods_id', lang('payment_method'), array('0' => lang('select_payment_method')) + $payment_method_options, $payment_method, false, array('class' => 'select_box')); ?>
					<div class="space-y-1">
						<?= form_input_group('reference', lang('reference'), $transfer_reference, 'text', false); ?>
						<p class="text-xs text-muted-foreground"><?= lang('reference_example'); ?></p>
					</div>
					<?= form_textarea_group('notes', lang('notes'), $transfer_notes, 4, false, array()); ?>
				</div>
			</div>
		</div>

		<div class="space-y-6">
			<div class="rounded-2xl border border-border bg-card shadow-sm">
				<div class="flex items-center justify-between border-b border-border px-5 py-4">
					<h3 class="text-sm font-semibold text-foreground"><?= lang('attachment'); ?></h3>
				</div>
				<div class="p-5 space-y-4">
					<?= form_attachment_group('files[]', lang('attachment'), 'dropzone', array(
						'dropzone_id' => 'transfer-files-dropzone',
						'list_id' => 'transfer-files-list',
					)); ?>

					<?php
					$uploaded_file = !empty($transfer_info->attachement) ? json_decode($transfer_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-3">
								<?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>
			</div>

			<?php if (!empty($transfer_info)) { ?>
				<input type="hidden" name="old_from_account_id" value="<?= html_escape($from_account); ?>">
				<input type="hidden" name="old_to_account_id" value="<?= html_escape($to_account); ?>">
				<input type="hidden" name="old_amount" value="<?= html_escape($transfer_amount); ?>">
			<?php } ?>

			<div class="rounded-2xl border border-border bg-card shadow-sm">
				<div class="flex items-center justify-between border-b border-border px-5 py-4">
					<h3 class="text-sm font-semibold text-foreground"><?= lang('permission'); ?></h3>
				</div>
				<div class="p-5 space-y-4">
					<?= form_permission_modern($permission_user, $permission_value, lang('assined_to')); ?>
				</div>
			</div>
		</div>
	</div>

	<div class="flex justify-end gap-2">
		<?php if (!empty($transfer_info)) { ?>
			<?= btn_update('updates', 'sm'); ?>
			<button type="button" onclick="goBack()" class="btn btn-danger btn-sm 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>
