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

<?php
$can_edit = isset($can_edit) ? $can_edit : false;
$edited = isset($edited) ? $edited : false;

$status_name = !empty($lead_status->lead_status) ? $lead_status->lead_status : '';
$status_type = !empty($lead_status->lead_type) ? $lead_status->lead_type : '';
$source_name = !empty($lead_source->lead_source) ? $lead_source->lead_source : '';

$status_tones = array(
	'open' => 'border-emerald-200 bg-emerald-50 text-emerald-700 dark:border-emerald-900/50 dark:bg-emerald-950/25 dark:text-emerald-300',
	'contacted' => 'border-sky-200 bg-sky-50 text-sky-700 dark:border-sky-900/50 dark:bg-sky-950/25 dark:text-sky-300',
	'qualified' => 'border-indigo-200 bg-indigo-50 text-indigo-700 dark:border-indigo-900/50 dark:bg-indigo-950/25 dark:text-indigo-300',
	'lost' => 'border-rose-200 bg-rose-50 text-rose-700 dark:border-rose-900/50 dark:bg-rose-950/25 dark:text-rose-300',
);
$status_tone = isset($status_tones[$status_type]) ? $status_tones[$status_type] : 'border-amber-200 bg-amber-50 text-amber-700 dark:border-amber-900/50 dark:bg-amber-950/25 dark:text-amber-300';

$custom_field_items = array();
if (!empty($custom_fields)) {
	foreach ($custom_fields as $c_label => $v_fields) {
		if (empty($v_fields)) {
			continue;
		}
		$decoded = json_decode($v_fields, true);
		$custom_field_items[] = array(
			'label' => $c_label,
			'value' => is_array($decoded) ? implode(', ', $decoded) : $v_fields,
		);
	}
}

$created_text = !empty($leads_details->created_time) ? time_ago($leads_details->created_time) : '-';
$last_contact_text = !empty($leads_details->last_contact) ? time_ago($leads_details->last_contact) : '-';
$language_text = !empty($leads_details->language) ? $leads_details->language : '-';
$country_text = !empty($leads_details->country) ? $leads_details->country : '-';
?>

<div class="space-y-5">
	<div class="rounded-2xl border border-border bg-gradient-to-r from-card to-muted/20 p-5 shadow-sm md:p-6">
		<div class="flex flex-col gap-4 xl:flex-row xl:items-start xl:justify-between">
			<div class="space-y-2">
				<div class="flex flex-wrap items-center gap-2">
					<span class="inline-flex items-center gap-1 rounded-full border border-border bg-background px-3 py-1 text-[11px] font-semibold uppercase tracking-[0.14em] text-muted-foreground">
						<i data-lucide="user-round-search" class="h-3.5 w-3.5"></i>
						<?= lang('lead') ?>
					</span>
					<?php if (!empty($status_name)) { ?>
						<span class="inline-flex items-center rounded-full border px-3 py-1 text-xs font-semibold <?= $status_tone ?>">
							<?= html_escape($status_name) ?>
						</span>
					<?php } ?>
					<?php if (!empty($source_name)) { ?>
						<span class="inline-flex items-center rounded-full border border-border bg-background px-3 py-1 text-xs font-medium text-foreground">
							<?= html_escape($source_name) ?>
						</span>
					<?php } ?>
				</div>
				<h2 class="text-2xl font-semibold text-foreground"><?= html_escape($leads_details->lead_name) ?></h2>
				<p class="text-sm text-muted-foreground">
					<?= lang('contact_name') ?>: <span class="font-medium text-foreground"><?= !empty($leads_details->contact_name) ? html_escape($leads_details->contact_name) : '-' ?></span>
					<span class="mx-2 text-border">|</span>
					<?= lang('organization') ?>: <span class="font-medium text-foreground"><?= !empty($leads_details->organization) ? html_escape($leads_details->organization) : '-' ?></span>
				</p>
			</div>
			<div class="flex flex-wrap items-center gap-2">
				<?php if ($leads_details->converted_client_id == 0 && !empty($can_edit) && !empty($edited) && !empty($lead_statuses)) { ?>
					<details class="relative">
						<summary class="inline-flex h-9 cursor-pointer list-none items-center gap-2 rounded-lg border border-border bg-background px-3 text-xs font-semibold text-foreground hover:bg-muted">
							<i data-lucide="refresh-cw" class="h-4 w-4"></i>
							<?= lang('change') ?>
							<i data-lucide="chevron-down" class="h-4 w-4"></i>
						</summary>
						<div class="absolute right-0 z-30 mt-1 min-w-[220px] overflow-hidden rounded-xl border border-border bg-card p-1 shadow-lg">
							<?php foreach ($lead_statuses as $status) { ?>
								<?php $is_active = !empty($lead_status->lead_status_id) && (int) $lead_status->lead_status_id === (int) $status->lead_status_id; ?>
								<a href="<?= base_url('admin/leads/change_status/' . $leads_details->leads_id . '/' . $status->lead_status_id) ?>" class="block rounded-lg px-3 py-2 text-xs <?= $is_active ? 'bg-muted font-medium text-foreground' : 'text-foreground hover:bg-muted' ?>">
									<?= lang($status->lead_type) ?> - <?= html_escape($status->lead_status) ?>
								</a>
							<?php } ?>
						</div>
					</details>
				<?php } ?>
			</div>
		</div>

		<div class="mt-4 grid grid-cols-1 gap-3 sm:grid-cols-2 xl:grid-cols-4">
			<div class="min-w-0 rounded-xl border border-border bg-card p-3">
				<p class="text-[11px] font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('email') ?></p>
				<p class="mt-2 truncate text-sm font-medium text-foreground"><?= !empty($leads_details->email) ? html_escape($leads_details->email) : '-' ?></p>
			</div>
			<div class="min-w-0 rounded-xl border border-border bg-card p-3">
				<p class="text-[11px] font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('phone') ?></p>
				<p class="mt-2 text-sm font-medium text-foreground"><?= !empty($leads_details->phone) ? html_escape($leads_details->phone) : '-' ?></p>
			</div>
			<div class="min-w-0 rounded-xl border border-border bg-card p-3">
				<p class="text-[11px] font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('created') ?></p>
				<p class="mt-2 text-sm font-medium text-foreground"><?= $created_text ?></p>
			</div>
			<div class="min-w-0 rounded-xl border border-border bg-card p-3">
				<p class="text-[11px] font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('last_contact') ?></p>
				<p class="mt-2 text-sm font-medium text-foreground"><?= $last_contact_text ?></p>
			</div>
		</div>
	</div>

	<div class="grid gap-5 xl:grid-cols-3">
		<div class="min-w-0 xl:col-span-2 space-y-5">
			<div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
				<h3 class="mb-4 text-sm font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('details') ?></h3>
				<div class="grid grid-cols-1 gap-3 sm:grid-cols-2">
					<div class="rounded-lg border border-border bg-background p-3">
						<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('lead_name') ?></p>
						<p class="mt-1 text-sm text-foreground"><?= html_escape($leads_details->lead_name) ?></p>
					</div>
					<div class="rounded-lg border border-border bg-background p-3">
						<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('contact_name') ?></p>
						<p class="mt-1 text-sm text-foreground"><?= !empty($leads_details->contact_name) ? html_escape($leads_details->contact_name) : '-' ?></p>
					</div>
					<div class="rounded-lg border border-border bg-background p-3">
						<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('organization') ?></p>
						<p class="mt-1 text-sm text-foreground"><?= !empty($leads_details->organization) ? html_escape($leads_details->organization) : '-' ?></p>
					</div>
					<div class="rounded-lg border border-border bg-background p-3">
						<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('mobile') ?></p>
						<p class="mt-1 text-sm text-foreground"><?= !empty($leads_details->mobile) ? html_escape($leads_details->mobile) : '-' ?></p>
					</div>
					<div class="rounded-lg border border-border bg-background p-3">
						<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('city') ?></p>
						<p class="mt-1 text-sm text-foreground"><?= !empty($leads_details->city) ? html_escape($leads_details->city) : '-' ?></p>
					</div>
					<div class="rounded-lg border border-border bg-background p-3">
						<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('state') ?></p>
						<p class="mt-1 text-sm text-foreground"><?= !empty($leads_details->state) ? html_escape($leads_details->state) : '-' ?></p>
					</div>
					<div class="rounded-lg border border-border bg-background p-3">
						<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('country') ?></p>
						<p class="mt-1 text-sm text-foreground"><?= html_escape($country_text) ?></p>
					</div>
					<div class="rounded-lg border border-border bg-background p-3">
						<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('language') ?></p>
						<p class="mt-1 text-sm text-foreground"><?= html_escape($language_text) ?></p>
					</div>
					<div class="sm:col-span-2 rounded-lg border border-border bg-background p-3">
						<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('address') ?></p>
						<p class="mt-1 text-sm text-foreground"><?= !empty($leads_details->address) ? html_escape($leads_details->address) : '-' ?></p>
					</div>
					<?php if (!empty($leads_details->tags)) { ?>
						<div class="sm:col-span-2 rounded-lg border border-border bg-background p-3">
							<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('tags') ?></p>
							<div class="mt-2"><?= get_tags($leads_details->tags, true) ?></div>
						</div>
					<?php } ?>
					<?php if (!empty($leads_details->facebook)) { ?>
						<div class="sm:col-span-2 rounded-lg border border-border bg-background p-3">
							<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('facebook_profile_link') ?></p>
							<a class="mt-1 inline-flex break-all text-sm text-primary hover:underline" target="_blank" href="<?= prep_url($leads_details->facebook) ?>"><?= html_escape($leads_details->facebook) ?></a>
						</div>
					<?php } ?>
					<?php if (!empty($leads_details->twitter)) { ?>
						<div class="sm:col-span-2 rounded-lg border border-border bg-background p-3">
							<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('twitter_profile_link') ?></p>
							<a class="mt-1 inline-flex break-all text-sm text-primary hover:underline" target="_blank" href="<?= prep_url($leads_details->twitter) ?>"><?= html_escape($leads_details->twitter) ?></a>
						</div>
					<?php } ?>
					<?php if (!empty($leads_details->skype)) { ?>
						<div class="rounded-lg border border-border bg-background p-3">
							<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('skype_id') ?></p>
							<p class="mt-1 text-sm text-foreground"><?= html_escape($leads_details->skype) ?></p>
						</div>
					<?php } ?>
				</div>
			</div>

			<?php if (!empty($leads_details->notes)) { ?>
				<div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
					<h3 class="text-sm font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('short_note') ?></h3>
					<div class="mt-3 whitespace-pre-line text-sm leading-6 text-foreground"><?= nl2br(html_escape($leads_details->notes)) ?></div>
				</div>
			<?php } ?>

			<?php if (!empty($custom_field_items)) { ?>
				<div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
					<h3 class="text-sm font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('custom_field') ?></h3>
					<div class="mt-3 grid gap-3 sm:grid-cols-2">
						<?php foreach ($custom_field_items as $field) { ?>
							<div class="rounded-lg border border-border bg-background p-3">
								<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= html_escape($field['label']) ?></p>
								<p class="mt-1 text-sm text-foreground"><?= nl2br(html_escape($field['value'])) ?></p>
							</div>
						<?php } ?>
					</div>
				</div>
			<?php } ?>
		</div>

		<div class="min-w-0 space-y-5">
			<div class="min-w-0 rounded-2xl border border-border bg-card p-5 shadow-sm">
				<div class="mb-3 flex items-center justify-between gap-2">
					<h3 class="text-sm font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('participants') ?></h3>
					<?php if (!empty($can_edit) && !empty($edited) && (int) $leads_details->converted_client_id === 0) { ?>
						<?= btn_modal(
							'admin/leads/update_users/' . $leads_details->leads_id,
							'',
							lang('add_more'),
							'md',
							'plus',
							array('class' => 'inline-flex h-8 w-8 items-center justify-center rounded-lg border border-border bg-background text-foreground hover:bg-muted')
						) ?>
					<?php } ?>
				</div>

				<?php if (!empty($participants_all)) { ?>
					<div class="rounded-lg border border-border bg-background p-3">
						<p class="text-sm font-medium text-foreground"><?= lang('everyone') ?></p>
						<p class="mt-1 text-xs text-muted-foreground"><?= lang('permission_for_all') ?></p>
					</div>
				<?php } elseif (!empty($participants)) { ?>
					<div class="space-y-2">
						<?php foreach ($participants as $participant) { ?>
							<?php
							$avatar = !empty($participant['avatar']) ? $participant['avatar'] : staffImage($participant['user_id']);
							if (!preg_match('/^https?:\/\//', $avatar)) {
								$avatar = base_url(ltrim($avatar, '/'));
							}
							$role_badge = ((int) $participant['role_id'] === 1) ? 'Admin' : 'Staff';
							?>
							<div class="flex items-center gap-3 rounded-lg border border-border bg-background p-2.5">
								<img src="<?= $avatar ?>" alt="<?= html_escape($participant['fullname']) ?>" class="h-9 w-9 rounded-full object-cover">
								<div class="min-w-0 flex-1">
									<p class="truncate text-sm font-medium text-foreground"><?= html_escape($participant['fullname']) ?></p>
									<p class="text-xs text-muted-foreground"><?= $role_badge ?></p>
								</div>
							</div>
						<?php } ?>
					</div>
				<?php } else { ?>
					<p class="text-sm text-muted-foreground"><?= lang('nothing_to_display') ?></p>
				<?php } ?>
			</div>
		</div>
	</div>
</div>
