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

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

<?php
$account_options = $account_options ?? array();
$category_options = $category_options ?? array();
$payment_method_options = $payment_method_options ?? array();
$category_label = $category_label ?? lang('categories');
$permission_value = !empty($deposit_info->permission) ? $deposit_info->permission : null;
$uploaded_file = !empty($deposit_info->attachement) ? json_decode($deposit_info->attachement) : array();
?>

<div class="space-y-4">
	<div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
		<div>
			<h2 class="text-xl font-semibold text-foreground"><?= html_escape($title); ?></h2>
			<p class="text-sm text-muted-foreground"><?= lang('import'); ?></p>
		</div>
		<div class="flex flex-wrap gap-2">
			<a href="<?= base_url('assets/sample/transactions_sample.csv'); ?>" class="rounded-lg bg-primary px-4 py-2 text-sm font-medium text-white shadow hover:bg-primary/90 transition">
				<?= lang('download_sample'); ?>
			</a>
		</div>
	</div>

	<div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
		<?php echo form_open(base_url('admin/transactions/save_imported'), array('class' => 'space-y-6', 'enctype' => 'multipart/form-data', 'data-parsley-validate' => 'true')); ?>
		<input type="hidden" name="type" value="<?= html_escape($type); ?>">

		<div class="grid gap-4 md:grid-cols-2">
			<label class="space-y-1 text-sm font-medium text-foreground block">
				<span><?= lang('choose_file'); ?> <span class="text-red-500">*</span></span>
				<input type="file" name="upload_file" accept=".xlsx,.csv" required class="w-full rounded-lg border border-border bg-input px-3 py-2 text-sm">
			</label>

			<?= form_lookup_group('account_id', lang('account'), '', '', true, array(), '', array('type' => 'account')); ?>
			<?= form_select_group('category_id', $category_label, array('0' => lang('none')) + $category_options, '0', false, array('class' => 'select_box')); ?>
			<?= form_lookup_group('paid_by', lang('paid_by'), '', '', false, array(), '', array('type' => 'client')); ?>
			<?= form_select_group('payment_methods_id', lang('payment_method'), array('0' => lang('select_payment_method')) + $payment_method_options, '0', false, array('class' => 'select_box')); ?>
		</div>

		<div class="rounded-xl border border-border bg-background p-4 space-y-3">
			<h3 class="text-sm font-semibold text-foreground"><?= lang('attachment'); ?></h3>
			<div id="import-attachments" class="space-y-3">
				<div class="attachment-row">
					<input type="file" name="attachement[]" class="w-full rounded-lg border border-border bg-input px-3 py-2 text-sm">
				</div>
			</div>
			<button type="button" id="add_more" class="inline-flex items-center gap-2 text-xs font-semibold text-primary">
				<i data-lucide="plus" class="h-3 w-3"></i>
				<?= lang('add_more'); ?>
			</button>
		</div>

		<?php if (!empty($uploaded_file)) { ?>
			<div class="rounded-xl border border-border bg-background p-4 space-y-3">
				<h3 class="text-sm font-semibold text-foreground"><?= lang('existing') . ' ' . lang('attachments'); ?></h3>
				<div class="grid gap-3 sm:grid-cols-2">
					<?php foreach ($uploaded_file as $v_files) { ?>
						<div class="flex items-center justify-between gap-3 rounded-lg border border-border bg-white px-3 py-2">
							<div class="flex items-center gap-2">
								<i data-lucide="file-text" class="h-4 w-4 text-muted-foreground"></i>
								<span class="text-sm text-foreground"><?= html_escape($v_files->fileName); ?></span>
							</div>
							<button type="button" class="remCFile text-slate-400 hover:text-rose-500">&times;</button>
							<input type="hidden" name="path[]" value="<?= html_escape($v_files->path); ?>">
							<input type="hidden" name="fileName[]" value="<?= html_escape($v_files->fileName); ?>">
							<input type="hidden" name="fullPath[]" value="<?= html_escape($v_files->fullPath); ?>">
							<input type="hidden" name="size[]" value="<?= html_escape($v_files->size); ?>">
						</div>
					<?php } ?>
				</div>
			</div>
		<?php } ?>

		<div class="rounded-2xl border border-border bg-card p-5 shadow-sm space-y-4">
			<h3 class="text-sm font-semibold text-foreground"><?= lang('permission'); ?></h3>
			<?= form_permission_modern($permission_user ?? array(), $permission_value, lang('assined_to'), true); ?>
		</div>

		<div class="flex justify-end">
			<button type="submit" class="btn btn-primary btn-sm"><?= lang('upload'); ?></button>
		</div>
		<?php echo form_close(); ?>
	</div>
</div>

<script>
	$(document).ready(function() {
		$('#add_more').on('click', function() {
			var row = $('<div class="attachment-row"><input type="file" name="attachement[]" class="w-full rounded-lg border border-border bg-input px-3 py-2 text-sm"></div>');
			$('#import-attachments').append(row);
		});

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