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

<script src="<?php echo base_url(); ?>assets/plugins/bootstrap-tagsinput/fm.tagator.jquery.js"></script>

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

$lead_id = !empty($leads_info) ? $leads_info->leads_id : null;
$permissionL = !empty($leads_info->permission) ? $leads_info->permission : null;

$status_options = array('' => lang('select') . ' ' . lang('lead_status'));
if (!empty($status_info)) {
	foreach ($status_info as $type => $statuses) {
		if (empty($statuses)) {
			continue;
		}
		foreach ($statuses as $status) {
			$status_options[$status->lead_status_id] = lang($type) . ' - ' . $status->lead_status;
		}
	}
}

$source_options = array('' => lang('select') . ' ' . lang('lead_source'));
if (!empty($lead_sources)) {
	foreach ($lead_sources as $source) {
		$source_options[$source->lead_source_id] = $source->lead_source;
	}
}

$language_options = array();
if (!empty($languages)) {
	foreach ($languages as $lang_item) {
		$language_options[$lang_item->name] = ucfirst($lang_item->name);
	}
}

$country_options = array();
if (!empty($countries)) {
	foreach ($countries as $country) {
		$country_options[$country->value] = $country->value;
	}
}

$selected_country = !empty($leads_info->country) ? $leads_info->country : $this->config->item('company_country');

$lead_status_link = '';
if (can_action('127', 'created')) {
	$lead_status_link = form_select_add_link('lead_status_id', base_url('admin/leads/lead_status'), lang('new') . ' ' . lang('lead_status'), 'md');
}
$lead_source_link = '';
if (can_action('128', 'created')) {
	$lead_source_link = form_select_add_link('lead_source_id', base_url('admin/leads/lead_source'), lang('new') . ' ' . lang('lead_source'), 'sm');
}
?>

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

		<div class="">
			
			<div class="p-2">
				<form role="form" data-parsley-validate enctype="multipart/form-data"
					action="<?= base_url('admin/leads/saved_leads/' . $lead_id); ?>" method="post" class="space-y-6">
					<div class="grid grid-cols-1 gap-6 xl:grid-cols-12">
						<div class="min-w-0 space-y-6 xl:col-span-8">
							<div class="min-w-0 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('lead') . ' ' . lang('details'); ?></h3>
								</div>
								<div class="p-5">
									<div class="grid grid-cols-1 gap-4 sm:grid-cols-2">
										<?= form_input_group('lead_name', lang('lead_name'), (!empty($leads_info) ? $leads_info->lead_name : ''), 'text', true); ?>
										<?= form_select_group(
											'lead_status_id',
											lang('lead_status'),
											$status_options,
											(!empty($leads_info->lead_status_id) ? $leads_info->lead_status_id : ''),
											true,
											array('class' => 'tw-selectpicker'),
											'',
											$lead_status_link
										); ?>
										<?= form_input_group('organization', lang('organization'), (!empty($leads_info) ? $leads_info->organization : ''), 'text'); ?>
										<?= form_select_group(
											'lead_source_id',
											lang('lead_source'),
											$source_options,
											(!empty($leads_info->lead_source_id) ? $leads_info->lead_source_id : ''),
											true,
											array('class' => 'tw-selectpicker'),
											'',
											$lead_source_link
										); ?>
										<?= form_tags_group(!empty($leads_info) ? $leads_info->tags : ''); ?>
										<?= form_select_group('language', lang('language'), $language_options, (!empty($leads_info->language) ? $leads_info->language : $this->config->item('language'))); ?>
									</div>
								</div>
							</div>

							<div class="min-w-0 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('contact') . ' ' . lang('details'); ?></h3>
								</div>
								<div class="p-5">
									<div class="grid grid-cols-1 gap-4 sm:grid-cols-2">
										<?= form_input_group('contact_name', lang('contact_name'), (!empty($leads_info) ? $leads_info->contact_name : ''), 'text', true); ?>
										<?= form_input_group('email', lang('email'), (!empty($leads_info) ? $leads_info->email : ''), 'email', true); ?>
										<?= form_input_group('phone', lang('phone'), (!empty($leads_info) ? $leads_info->phone : ''), 'text', true); ?>
										<?= form_input_group('mobile', lang('mobile'), (!empty($leads_info) ? $leads_info->mobile : ''), 'text', true); ?>
										<?= form_input_group('address', lang('address'), (!empty($leads_info) ? $leads_info->address : ''), 'text'); ?>
										<?= form_input_group('city', lang('city'), (!empty($leads_info) ? $leads_info->city : ''), 'text'); ?>
										<?= form_input_group('state', lang('state'), (!empty($leads_info) ? $leads_info->state : ''), 'text'); ?>
										<?= form_select_group('country', lang('country'), $country_options, $selected_country); ?>
									</div>
								</div>
							</div>

							<div class="min-w-0 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('profile_details'); ?></h3>
								</div>
								<div class="p-5">
									<div class="grid grid-cols-1 gap-4 sm:grid-cols-2">
										<?= form_input_group('twitter', lang('twitter_profile_link'), (!empty($leads_info) ? $leads_info->twitter : ''), 'text'); ?>
										<?= form_input_group('facebook', lang('facebook_profile_link'), (!empty($leads_info) ? $leads_info->facebook : ''), 'text'); ?>
										<?= form_input_group('skype', lang('skype_id'), (!empty($leads_info) ? $leads_info->skype : ''), 'text'); ?>
									</div>
								</div>
							</div>

							<div class="min-w-0 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($leads_info) ? $leads_info->notes : ''), 4, false, array('class' => 'textarea')); ?>
								</div>
							</div>
						</div>

						<div class="min-w-0 space-y-6 xl:col-span-4">
							<div class="min-w-0 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 space-y-4">
									<?php if (empty($leads_info)) { ?>
										<div class="flex items-center justify-between rounded-lg border border-border bg-muted/40 px-3 py-2">
											<span class="text-sm text-foreground"><?= lang('contacted_today'); ?></span>
											<input data-toggle="toggle" name="contacted_today" value="on" checked data-on="<?= lang('yes') ?>" data-off="<?= lang('no') ?>" data-onstyle="success" data-offstyle="danger" type="checkbox" id="contacted_today_toggle">
										</div>
										<div id="contacted_date_wrap" class="hidden">
											<?= form_datepicker('last_contact', lang('date_contacted'), date('Y-m-d H:i'), false, array('data-type' => 'datetime', 'data-format' => 'Y-m-d H:i', 'data-auto-close' => 'false')); ?>
										</div>
									<?php } else { ?>
										<?= form_datepicker('last_contact', lang('last_contact'), (!empty($leads_info->last_contact) ? $leads_info->last_contact : ''), false, array('data-type' => 'datetime', 'data-format' => 'Y-m-d H:i', 'data-auto-close' => 'false')); ?>
									<?php } ?>
								</div>
							</div>

							<div class="min-w-0 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(5, $lead_id, true); ?>
								</div>
							</div>

							<div class="min-w-0 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('assigned_to'); ?></h3>
								</div>
								<div class="p-5">
									<?= form_permission_modern($assign_user, $permissionL, lang('assigned_to'), true); ?>
								</div>
							</div>
						</div>
					</div>

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

<script type="text/javascript">
	$(function() {
		var toggle = document.getElementById('contacted_today_toggle');
		var wrap = document.getElementById('contacted_date_wrap');
		if (toggle && wrap) {
			var sync = function() {
				wrap.classList.toggle('hidden', !toggle.checked);
			};
			toggle.addEventListener('change', sync);
			sync();
		}
	});
</script>
