<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php
$entity = null;
if (!empty($client_details)) {
	$entity = $client_details;
} elseif (!empty($lead_info)) {
	$entity = $lead_info;
}

$name = !empty($entity->name) ? $entity->name : (!empty($entity->company) ? $entity->company : lang('location'));
$address_parts = array_filter(array(
	!empty($entity->address) ? $entity->address : '',
	!empty($entity->city) ? $entity->city : '',
	!empty($entity->state) ? $entity->state : '',
	!empty($entity->zipcode) ? $entity->zipcode : '',
	!empty($entity->country) ? $entity->country : '',
));
$full_address = trim(implode(', ', $address_parts));

$latitude = !empty($entity->latitude) ? trim((string) $entity->latitude) : '';
$longitude = !empty($entity->longitude) ? trim((string) $entity->longitude) : '';
$has_coordinates = ($latitude !== '' && $longitude !== '');

$map_query = $has_coordinates ? ($latitude . ',' . $longitude) : $full_address;
$map_src = '';
if (!empty($map_query)) {
	$map_src = 'https://www.google.com/maps?q=' . rawurlencode($map_query) . '&z=15&output=embed';
}

$update_url = '';
if (!empty($client_details->client_id)) {
	$update_url = base_url('admin/client/update_latitude/' . $client_details->client_id);
}
?>

<div class="space-y-5">
	<div class="grid grid-cols-1 gap-4 lg:grid-cols-[minmax(0,1.4fr)_minmax(320px,0.8fr)]">
		<div class="overflow-hidden rounded-2xl border border-border bg-card shadow-sm">
			<?php if (!empty($map_src)) { ?>
				<iframe
					title="<?= html_escape($name) ?> map"
					src="<?= html_escape($map_src) ?>"
					loading="lazy"
					referrerpolicy="no-referrer-when-downgrade"
					class="h-[420px] w-full border-0"></iframe>
			<?php } else { ?>
				<div class="flex h-[420px] items-center justify-center bg-muted/40 p-6 text-center">
					<div>
						<div class="mx-auto flex h-14 w-14 items-center justify-center rounded-2xl border border-border bg-card">
							<i data-lucide="map-pin-off" class="h-6 w-6 text-muted-foreground"></i>
						</div>
						<p class="mt-4 text-sm font-semibold text-foreground"><?= lang('nothing_to_display') ?></p>
						<p class="mt-1 text-sm text-muted-foreground"><?= lang('address') ?> <?= lang('or') ?> coordinates are not available.</p>
					</div>
				</div>
			<?php } ?>
		</div>

		<div class="space-y-4">
			<div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
				<h4 class="text-sm font-semibold text-foreground"><?= lang('location') ?></h4>
				<div class="mt-4 space-y-3 text-sm">
					<div>
						<p class="text-xs font-semibold uppercase tracking-[0.18em] text-muted-foreground"><?= lang('name') ?></p>
						<p class="mt-1 text-foreground"><?= html_escape($name) ?></p>
					</div>
					<div>
						<p class="text-xs font-semibold uppercase tracking-[0.18em] text-muted-foreground"><?= lang('address') ?></p>
						<p class="mt-1 text-foreground"><?= !empty($full_address) ? html_escape($full_address) : '-' ?></p>
					</div>
					<div class="grid grid-cols-2 gap-3">
						<div>
							<p class="text-xs font-semibold uppercase tracking-[0.18em] text-muted-foreground"><?= lang('latitude') ?></p>
							<p class="mt-1 text-foreground"><?= $latitude !== '' ? html_escape($latitude) : '-' ?></p>
						</div>
						<div>
							<p class="text-xs font-semibold uppercase tracking-[0.18em] text-muted-foreground"><?= lang('longitude') ?></p>
							<p class="mt-1 text-foreground"><?= $longitude !== '' ? html_escape($longitude) : '-' ?></p>
						</div>
					</div>
				</div>
			</div>

			<?php if (!empty($update_url)) { ?>
				<div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
					<h4 class="text-sm font-semibold text-foreground"><?= lang('google_map') ?></h4>
					<p class="mt-1 text-xs text-muted-foreground"><?= lang('update') ?> <?= lang('latitude') ?> / <?= lang('longitude') ?></p>
					<?= form_open($update_url, array('class' => 'mt-4 space-y-4')); ?>
						<?= form_input_group('latitude', lang('latitude'), $latitude, 'text', false, array('placeholder' => lang('latitude'))); ?>
						<?= form_input_group('longitude', lang('longitude'), $longitude, 'text', false, array('placeholder' => lang('longitude'))); ?>
						<div class="flex justify-end">
							<?= btn_update(lang('update'), 'md'); ?>
						</div>
					<?= form_close(); ?>
				</div>
			<?php } ?>
		</div>
	</div>
</div>
