<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php
$customer_group_options = array('' => lang('customer_group'));
$all_customer_group = $this->db->order_by('customer_group_id', 'DESC')->get('tbl_customer_group')->result();
if (!empty($all_customer_group)) {
	foreach ($all_customer_group as $customer_group) {
		$customer_group_options[$customer_group->customer_group_id] = $customer_group->customer_group;
	}
}

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

$currency_options = array();
if (!empty($currencies)) {
	foreach ($currencies as $currency) {
		$currency_options[$currency->code] = $currency->name;
	}
}

$country_options = array('' => lang('company_country'));
if (!empty($this->config->item('company_country'))) {
	$country_options[$this->config->item('company_country')] = $this->config->item('company_country');
}
if (!empty($countries)) {
	foreach ($countries as $country) {
		$country_options[$country->value] = $country->value;
	}
}

$lead_name = !empty($leads_info->lead_name) ? $leads_info->lead_name : '';
$lead_email = !empty($leads_info->email) ? $leads_info->email : '';
$lead_notes = !empty($leads_info->notes) ? $leads_info->notes : '';
$lead_phone = !empty($leads_info->phone) ? $leads_info->phone : '';
$lead_mobile = !empty($leads_info->mobile) ? $leads_info->mobile : '';
$lead_city = !empty($leads_info->city) ? $leads_info->city : '';
$lead_country = !empty($leads_info->country) ? $leads_info->country : '';
$lead_address = !empty($leads_info->address) ? $leads_info->address : '';
$lead_language = !empty($leads_info->language) ? $leads_info->language : config_item('language');
$lead_skype = !empty($leads_info->skype) ? $leads_info->skype : '';
$lead_facebook = !empty($leads_info->facebook) ? $leads_info->facebook : '';
$lead_twitter = !empty($leads_info->twitter) ? $leads_info->twitter : '';
?>

<?= form_open_multipart(
	base_url('admin/leads/converted/' . (!empty($leads_info->leads_id) ? $leads_info->leads_id : '')),
	array(
		'data-parsley-validate' => '',
		'novalidate' => '',
		'class' => 'space-y-5'
	)
); ?>

<div class="rounded-2xl border border-border bg-card shadow-sm">
	<div class="border-b border-border px-5 py-4">
		<div class="flex items-start gap-3">
			<span class="inline-flex h-10 w-10 items-center justify-center rounded-xl bg-primary/10 text-primary">
				<i data-lucide="arrow-right-left" class="h-5 w-5"></i>
			</span>
			<div>
				<h3 class="text-base font-semibold text-foreground"><?= lang('convert_to_client') ?></h3>
				<p class="text-sm text-muted-foreground">Move this lead into the client directory with company, contact, and hosting details.</p>
			</div>
		</div>
	</div>

	<div class="p-5 space-y-5">
		<div class="grid gap-5 xl:grid-cols-2">
			<div class="rounded-2xl border border-border bg-background p-4 space-y-4">
				<div class="flex items-center gap-2 border-b border-border pb-3">
					<i data-lucide="building-2" class="h-4 w-4 text-muted-foreground"></i>
					<h4 class="text-sm font-semibold text-foreground"><?= lang('general') ?></h4>
				</div>

				<div class="grid gap-4 md:grid-cols-2">
					<div class="md:col-span-2">
						<?= form_input_group('name', lang('company_name'), $lead_name, 'text', true); ?>
					</div>
					<div class="md:col-span-2">
						<?= form_input_group('email', lang('company_email'), $lead_email, 'email', true); ?>
					</div>
					<?= form_input_group('vat', lang('company_vat'), !empty($client_info->vat) ? $client_info->vat : '', 'text'); ?>
					<?= form_select_group('customer_group_id', lang('customer_group'), $customer_group_options, !empty($client_info->customer_group_id) ? $client_info->customer_group_id : '', false, array('class' => 'select_box', 'data-live-search' => 'true')); ?>
					<?= form_select_group('language', lang('language'), $language_options, $lead_language, false, array('class' => 'select_box')); ?>
					<?= form_select_group('currency', lang('currency'), $currency_options, !empty($client_info->currency) ? $client_info->currency : config_item('default_currency'), false, array('class' => 'select_box')); ?>
					<div class="md:col-span-2">
						<?= form_textarea_group('short_note', lang('short_note'), $lead_notes, 3); ?>
					</div>
				</div>
			</div>

			<div class="rounded-2xl border border-border bg-background p-4 space-y-4">
				<div class="flex items-center gap-2 border-b border-border pb-3">
					<i data-lucide="contact-round" class="h-4 w-4 text-muted-foreground"></i>
					<h4 class="text-sm font-semibold text-foreground"><?= lang('client_contact') . ' ' . lang('details') ?></h4>
				</div>

				<div class="grid gap-4 md:grid-cols-2">
					<?= form_input_group('phone', lang('company_phone'), $lead_phone, 'text'); ?>
					<?= form_input_group('mobile', lang('company_mobile'), $lead_mobile, 'text', true); ?>
					<?= form_input_group('fax', lang('company_fax'), !empty($client_info->fax) ? $client_info->fax : '', 'text'); ?>
					<?= form_input_group('city', lang('company_city'), $lead_city, 'text'); ?>
					<div class="md:col-span-2">
						<?= form_select_group('country', lang('company_country'), $country_options, $lead_country, false, array('class' => 'select_box', 'data-live-search' => 'true')); ?>
					</div>
					<div class="md:col-span-2">
						<?= form_textarea_group('address', lang('company_address'), $lead_address, 3); ?>
					</div>
					<div>
						<?= form_input_group('latitude', lang('latitude') . ' (' . lang('google_map') . ')', !empty($client_info->latitude) ? $client_info->latitude : '', 'text'); ?>
					</div>
					<div>
						<?= form_input_group('longitude', lang('longitude') . ' (' . lang('google_map') . ')', !empty($client_info->longitude) ? $client_info->longitude : '', 'text'); ?>
					</div>
				</div>
			</div>
		</div>

		<div class="grid gap-5 xl:grid-cols-2">
			<div class="rounded-2xl border border-border bg-background p-4 space-y-4">
				<div class="flex items-center gap-2 border-b border-border pb-3">
					<i data-lucide="globe" class="h-4 w-4 text-muted-foreground"></i>
					<h4 class="text-sm font-semibold text-foreground"><?= lang('web') ?></h4>
				</div>
				<div class="grid gap-4 md:grid-cols-2">
					<div class="md:col-span-2">
						<?= form_input_group('website', lang('company_domain'), !empty($client_info->website) ? $client_info->website : '', 'text'); ?>
					</div>
					<?= form_input_group('skype_id', lang('skype_id'), $lead_skype, 'text'); ?>
					<?= form_input_group('linkedin', lang('linkedin_profile_link'), !empty($client_info->linkedin) ? $client_info->linkedin : '', 'text'); ?>
					<?= form_input_group('facebook', lang('facebook_profile_link'), $lead_facebook, 'text'); ?>
					<?= form_input_group('twitter', lang('twitter_profile_link'), $lead_twitter, 'text'); ?>
				</div>
			</div>

			<div class="rounded-2xl border border-border bg-background p-4 space-y-4">
				<div class="flex items-center gap-2 border-b border-border pb-3">
					<i data-lucide="server" class="h-4 w-4 text-muted-foreground"></i>
					<h4 class="text-sm font-semibold text-foreground"><?= lang('hosting') ?></h4>
				</div>
				<div class="grid gap-4 md:grid-cols-2">
					<div class="md:col-span-2">
						<?= form_input_group('hosting_company', lang('hosting_company'), !empty($client_info->hosting_company) ? $client_info->hosting_company : '', 'text'); ?>
					</div>
					<?= form_input_group('hostname', lang('hostname'), !empty($client_info->hostname) ? $client_info->hostname : '', 'text'); ?>
					<?= form_input_group('port', lang('port'), !empty($client_info->port) ? $client_info->port : '', 'text'); ?>
					<?= form_input_group('username', lang('username'), !empty($client_info->username) ? $client_info->username : '', 'text'); ?>
					<?= form_input_group('password', lang('password'), !empty($client_info->password) ? $client_info->password : '', 'password'); ?>
				</div>
			</div>
		</div>
	</div>
</div>

<div class="flex flex-col-reverse gap-2 border-t border-border pt-4 sm:flex-row sm:justify-end">
	<div class="w-full sm:w-auto">
		<?= btn_modal_close('close', 'sm'); ?>
	</div>
	<div class="w-full sm:w-auto">
		<?= btn_update('save', 'sm'); ?>
	</div>
</div>

<?= form_close(); ?>

<script type="text/javascript">
	function fetch_lat_long_from_google_cprofile() {
		var data = {
			address: $('textarea[name="address"]').val(),
			city: $('input[name="city"]').val(),
			country: $('select[name="country"] option:selected').text()
		};

		var $icon = $('#gmaps-search-icon');
		$icon.addClass('animate-spin');

		$.post('<?= base_url() ?>admin/common/fetch_address_info_gmaps', data).done(function(response) {
			response = JSON.parse(response);
			$icon.removeClass('animate-spin');
			if (response.response.status == 'OK') {
				$('input[name="latitude"]').val(response.lat);
				$('input[name="longitude"]').val(response.lng);
			} else if (response.response.status == 'ZERO_RESULTS') {
				toastr.warning("<?php echo lang('g_search_address_not_found'); ?>");
			} else {
				toastr.warning(response.response.status);
			}
		}).fail(function() {
			$icon.removeClass('animate-spin');
		});
	}
</script>
