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

<?php
$created = can_action('56', 'created');
$edited = can_action('56', 'edited');
$deleted = can_action('56', 'deleted');

$opportunity_id = !empty($opportunity_info) ? $opportunity_info->opportunities_id : null;
$close_date = !empty($opportunity_info) ? date('Y-m-d', safe_strtotime($opportunity_info->close_date)) : date('Y-m-d');
$next_action_date = !empty($opportunity_info) ? date('Y-m-d', safe_strtotime($opportunity_info->next_action_date)) : date('Y-m-d');
$probability_val = !empty($opportunity_info->probability) ? $opportunity_info->probability : 0;
$probability_slider_id = 'opportunity_probability';

$stage_options = array(
	'new' => lang('new'),
	'qualification' => lang('qualification'),
	'proposition' => lang('proposition'),
	'won' => lang('won'),
	'lost' => lang('lost'),
	'dead' => lang('dead'),
);

$permissionL = !empty($opportunity_info->permission) ? $opportunity_info->permission : null;

// $state_options_html = '';
// if (!empty($all_state)) {
// 	foreach ($all_state as $state => $opportunities_state) {
// 		if (empty($state)) {
// 			continue;
// 		}
// 		$state_options_html .= '<optgroup label="' . lang($state) . '">';
// 		foreach ($opportunities_state as $v_state) {
// 			$selected = (!empty($opportunity_info->opportunities_state_reason_id) && $v_state->opportunities_state_reason_id == $opportunity_info->opportunities_state_reason_id)
// 				? ' selected'
// 				: '';
// 			$state_options_html .= '<option value="' . $v_state->opportunities_state_reason_id . '"' . $selected . '>' . $v_state->opportunities_state_reason . '</option>';
// 		}
// 		$state_options_html .= '</optgroup>';
// 	}
// }

$state_options_html = form_lookup_add_link(
	'opportunities_state_reason_id',
	base_url('admin/opportunities/opportunities_state_reason'),
	lang('opportunities_state_reason'),
);

$selected_state = !empty($opportunity_info->opportunities_state_reason_id) ? $opportunity_info->opportunities_state_reason_id : (!empty($employee_id) ? $employee_id : '');
$selected_state_label = null;
if (!empty($opportunity_info->opportunities_state_reason_id)) {
	$state_reason = $this->db->where('opportunities_state_reason_id', $opportunity_info->opportunities_state_reason_id)->get('tbl_opportunities_state_reason')->row();
	if (!empty($state_reason)) {
		$selected_state_label = $state_reason->opportunities_state_reason;
	}
}

if (!empty($created) || !empty($edited)) {
?>
	<div class="space-y-6">
		<?= message_box('success'); ?>
		<?= message_box('error'); ?>

		<div class="rounded-2xl border border-border bg-card shadow-sm">
			<div class="px-6 py-4 border-b border-border flex flex-col gap-1 md:flex-row md:items-center md:justify-between">
				<div>
					<h2 class="text-lg font-semibold text-foreground"><?= !empty($opportunity_id) ? lang('updates') : lang('new_opportunities'); ?></h2>
					<p class="text-sm text-muted-foreground"><?= lang('opportunities'); ?></p>
				</div>
			</div>
			<div class="p-6">
				<form role="form" data-parsley-validate enctype="multipart/form-data" id="form" action="<?= base_url('admin/opportunities/saved_opportunity/' . $opportunity_id); ?>" method="post" class="space-y-6">
					<div class="grid grid-cols-1 gap-6 xl:grid-cols-12">
						<div class="space-y-6 xl:col-span-8">
							<div class="rounded-2xl border border-border bg-card shadow-sm">
								<div class="px-5 py-4 border-b border-border">
									<h3 class="text-sm font-semibold text-foreground"><?= lang('opportunity'); ?></h3>
								</div>
								<div class="p-5">
									<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
										<?= form_input_group('opportunity_name', lang('opportunity_name'), (!empty($opportunity_info) ? $opportunity_info->opportunity_name : ''), 'text', true); ?>
										<?= form_select_group('stages', lang('stages'), $stage_options, (!empty($opportunity_info) ? $opportunity_info->stages : 'new'), true); ?>
										<?= form_datepicker('close_date', lang('close_date'), $close_date); ?>
										<?= form_progress_slider('probability', lang('probability') . ' %', $probability_val, 0, 100, array('id' => $probability_slider_id)); ?>
									</div>
								</div>
							</div>

							<div class="rounded-2xl border border-border bg-card shadow-sm">
								<div class="px-5 py-4 border-b border-border">
									<h3 class="text-sm font-semibold text-foreground"><?= lang('status'); ?></h3>
								</div>
								<div class="p-5">
									<div class="grid grid-cols-1 gap-4 md:grid-cols-2">

										<?= form_lookup_group('opportunities_state_reason_id', lang('current_state'), $selected_state, $selected_state_label, true, array(), '', array('type' => 'state_reason', 'append' => $state_options_html)); ?>

										<?= form_input_group('expected_revenue', lang('expected_revenue'), (!empty($opportunity_info) ? $opportunity_info->expected_revenue : ''), 'text', false, array('data-parsley-type' => 'number', 'min' => 0)); ?>
										<?= form_input_group('new_link', lang('new_link'), (!empty($opportunity_info) ? $opportunity_info->new_link : ''), 'text'); ?>
									</div>
								</div>
							</div>

							<div class="rounded-2xl border border-border bg-card shadow-sm">
								<div class="px-5 py-4 border-b border-border">
									<h3 class="text-sm font-semibold text-foreground"><?= lang('next_action'); ?></h3>
								</div>
								<div class="p-5">
									<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
										<?= form_input_group('next_action', lang('next_action'), (!empty($opportunity_info) ? $opportunity_info->next_action : ''), 'text'); ?>
										<?= form_datepicker('next_action_date', lang('next_action_date'), $next_action_date); ?>
									</div>
								</div>
							</div>

							<div class="rounded-2xl border border-border bg-card shadow-sm">
								<div class="px-5 py-4 border-b border-border">
									<h3 class="text-sm font-semibold text-foreground"><?= lang('short_note'); ?></h3>
								</div>
								<div class="p-5">
									<?= form_textarea_group('notes', lang('short_note'), (!empty($opportunity_info) ? $opportunity_info->notes : ''), 4, false, array('class' => 'textarea')); ?>
								</div>
							</div>
						</div>

						<div class="space-y-6 xl:col-span-4">
							<div class="rounded-2xl border border-border bg-card shadow-sm">
								<div class="px-5 py-4 border-b border-border">
									<h3 class="text-sm font-semibold text-foreground"><?= lang('custom_field'); ?></h3>
								</div>
								<div class="p-5">
									<?= custom_form_Fields(8, $opportunity_id, true); ?>
								</div>
							</div>

							<div class="rounded-2xl border border-border bg-card shadow-sm">
								<div class="px-5 py-4 border-b border-border">
									<h3 class="text-sm font-semibold text-foreground"><?= lang('who_responsible'); ?></h3>
								</div>
								<div class="p-5">
									<?= form_permission_modern($assign_user, $permissionL, lang('who_responsible'), true); ?>
								</div>
							</div>
						</div>
					</div>

					<div class="flex items-center justify-end gap-2">
						<?php if (!empty($opportunity_info)) { ?>
							<?= btn_update(lang('updates'), 'md'); ?>
							<button type="button" onclick="goBack()" class="btn h-9 btn-md mb-1 btn-danger"><?= lang('cancel') ?></button>
						<?php } else { ?>
							<?= btn_update(lang('save'), 'md'); ?>
						<?php } ?>
					</div>
				</form>
			</div>
		</div>
	</div>
<?php } ?>

<script>
	function goBack() {
		window.history.back();
	}
	(function() {
		const range = document.getElementById('<?= $probability_slider_id; ?>');
		const hidden = document.getElementById('<?= $probability_slider_id; ?>_hidden');

		const sync = () => {
			if (hidden && range) {
				hidden.value = range.value;
			}
		};

		if (range) {
			range.addEventListener('input', sync);
			sync();
		}
	})();
</script>
