<div class="rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-900">

	<div class="p-5">
		<form role="form" data-parsley-validate="" novalidate=""
			action="<?php echo base_url(); ?>admin/proposals/cloned_proposals/<?= $proposals_info->proposals_id ?>"
			method="post"
			class="">

			<?php
			$related_module = !empty($proposals_info->module) ? $proposals_info->module : '';
			$related_module_id = !empty($proposals_info->module_id) ? $proposals_info->module_id : '';
			$client_id = $related_module === 'client' ? $related_module_id : '';
			$leads_id = $related_module === 'leads' ? $related_module_id : '';
			$client_label = !empty($client_id) ? client_name($client_id) : '';
			$lead_label = '';
			if (!empty($leads_id)) {
				$lead_row = $this->db->select('lead_name')->where('leads_id', $leads_id)->get('tbl_leads')->row();
				$lead_label = !empty($lead_row->lead_name) ? $lead_row->lead_name : '';
			}
			$currency_selected = !empty($proposals_info->currency) ? $proposals_info->currency : config_item('default_currency');
			$currency_options = array();
			$all_currency = $this->db->get('tbl_currencies')->result();
			if (!empty($all_currency)) {
				foreach ($all_currency as $v_currency) {
					$currency_options[$v_currency->code] = $v_currency->name;
				}
			}
			$related_options = array(
				'' => lang('none'),
				'leads' => lang('leads'),
				'client' => lang('client'),
			);
			echo form_select_group(
				'module',
				lang('related_to'),
				$related_options,
				$related_module,
				true,
				array(
					'id' => 'check_related',
					'class' => 'select_box',
				),
				'',
				'',
				'horizontal'
			);
			?>

			<div id="proposal_clone_related_fields" class="space-y-4">
				<div data-related-module="leads" class="<?= $related_module === 'leads' ? '' : 'hidden' ?>">
					<?= form_lookup_group('leads_id', lang('select') . ' ' . lang('leads'), $leads_id, $lead_label, false, array('placeholder' => lang('search') . ' ' . lang('leads')), '', array('type' => 'lead', 'min_length' => 0), 'horizontal'); ?>
					<?= form_select_group('currency', lang('currency'), $currency_options, $currency_selected, true, array('data-live-search' => 'true'), '', '', 'horizontal'); ?>
				</div>
				<div data-related-module="client" class="<?= $related_module === 'client' ? '' : 'hidden' ?>">
					<?= form_lookup_group('client_id', lang('select') . ' ' . lang('client'), $client_id, $client_label, false, array('placeholder' => lang('search') . ' ' . lang('client')), '', array('type' => 'client', 'min_length' => 0), 'horizontal'); ?>
				</div>
			</div>

		<?= form_datepicker('proposal_date', lang('proposal_date'), !empty($proposals_info->proposal_date) ? $proposals_info->proposal_date : date('Y-m-d'), false, ['id' => 'start-date', 'data-format' => 'Y-m-d', 'data-max' => '#end-date', 'data-auto-close' => 'true'], '', '', 'horizontal'); ?>

		<?= form_datepicker('due_date', lang('due_date'), !empty($proposals_info->due_date) ? $proposals_info->due_date : date('Y-m-d'), false, ['id' => 'end-date', 'data-format' => 'Y-m-d', 'data-min' => '#start-date', 'data-auto-close' => 'true'], '', '', 'horizontal'); ?>

			
			<div class="flex items-center justify-end gap-2 border-t border-slate-200 pt-4 dark:border-slate-700">
				<button type="button" data-dismiss="modal"
					class="inline-flex items-center rounded-lg border border-slate-300 bg-white px-3 py-2 text-sm font-semibold text-slate-700 transition hover:bg-slate-50 dark:border-slate-600 dark:bg-slate-800 dark:text-slate-200 dark:hover:bg-slate-700"><?= lang('close') ?></button>
				<button type="submit"
					class="inline-flex items-center rounded-lg bg-slate-900 px-4 py-2 text-sm font-semibold text-white transition hover:bg-slate-800 dark:bg-slate-100 dark:text-slate-900 dark:hover:bg-white"><?= lang('clone') ?></button>
			</div>
		</form>
	</div>
</div>

<script>
(function() {
	var moduleSelect = document.getElementById('check_related');
	var relatedFields = document.getElementById('proposal_clone_related_fields');
	function toggleRelatedFields(value) {
		if (!relatedFields) return;
		relatedFields.querySelectorAll('[data-related-module]').forEach(function(block) {
			var isActive = block.getAttribute('data-related-module') === value;
			block.classList.toggle('hidden', !isActive);
			block.querySelectorAll('select, input').forEach(function(el) {
				if (isActive) {
					el.removeAttribute('disabled');
				} else {
					el.setAttribute('disabled', 'disabled');
				}
			});
		});
	}
	if (moduleSelect) {
		moduleSelect.addEventListener('change', function() {
			toggleRelatedFields(moduleSelect.value);
		});
		toggleRelatedFields(moduleSelect.value);
	}
	if (window.initLookup) {
		window.initLookup(relatedFields || document);
	}
})();
</script>
