<?php echo form_open(base_url('admin/return_stock/cloned_return_stock/' . $return_stock_info->return_stock_id), array('class' => 'space-y-4', 'enctype' => 'multipart/form-data', 'data-parsley-validate' => '', 'role' => 'form')); ?>

	<?php
	$module_options = array(
		'' => lang('none'),
		'supplier' => lang('supplier'),
		'client' => lang('client'),
	);
	$module_value = !empty($return_stock_info->module) ? $return_stock_info->module : '';
	?>
	<?= form_select_group('module', lang('related_to'), $module_options, $module_value, true, array('id' => 'check_related', 'class' => 'select_box', 'data-live-search' => 'true')); ?>
	<div id="related_to" class="grid grid-cols-1 md:grid-cols-2 gap-4"></div>
	<?= form_datepicker('return_stock_date', lang('return_stock') . ' ' . lang('date'), !empty($return_stock_info->return_stock_date) ? $return_stock_info->return_stock_date : date('Y-m-d'), true, array('data-auto-close' => 'true')); ?>
	<?= form_datepicker('due_date', lang('due_date'), !empty($return_stock_info->due_date) ? $return_stock_info->due_date : date('Y-m-d', safe_strtotime('+' . config_item('invoices_due_after') . ' days')), false, array('data-auto-close' => 'true')); ?>

	<div class="flex items-center justify-end gap-2 pt-2">
		<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="copy" class="h-4 w-4"></i>
			<?= lang('clone') ?>
		</button>
	</div>
<?php echo form_close(); ?>
<script>
	(function() {
		var moduleSelect = document.getElementById('check_related');
		var relatedContainer = document.getElementById('related_to');
		var moduleValue = "<?= html_escape($module_value) ?>";
		var moduleId = "<?= html_escape($return_stock_info->module_id ?? '') ?>";
		if (!moduleSelect || !moduleValue) return;
		get_related_moduleName(moduleValue, true);
		setTimeout(function() {
			var relatedSelect = relatedContainer ? relatedContainer.querySelector('select[name=\"supplier_id\"], select[name=\"client_id\"]') : null;
			if (relatedSelect && moduleId) {
				relatedSelect.value = moduleId;
				if (window.refreshSelectpicker) window.refreshSelectpicker(relatedSelect);
			}
		}, 300);
	})();
</script>
