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

<?= message_box('success'); ?>
<?= message_box('error'); ?>

<?php
$lead_kpis = isset($lead_kpis) && is_array($lead_kpis) ? $lead_kpis : array();
$status_label = !empty($lead_status->lead_status) ? $lead_status->lead_status : '';
$status_tone = (!empty($lead_status->lead_type) && $lead_status->lead_type === 'open') ? 'emerald' : 'amber';
$source_label = !empty($lead_source->lead_source) ? $lead_source->lead_source : '';

$kpi_cards = array(
	array(
		'label' => lang('call'),
		'count' => $lead_kpis['calls'] ?? 0,
		'url' => base_url('admin/leads/leads_details/' . $leads_details->leads_id . '/call'),
		'icon' => 'phone',
		'tone' => 'indigo',
	),
	array(
		'label' => lang('mettings'),
		'count' => $lead_kpis['meetings'] ?? 0,
		'url' => base_url('admin/leads/leads_details/' . $leads_details->leads_id . '/mettings'),
		'icon' => 'calendar',
		'tone' => 'sky',
	),
	array(
		'label' => lang('tasks'),
		'count' => $lead_kpis['tasks'] ?? 0,
		'url' => base_url('admin/leads/leads_details/' . $leads_details->leads_id . '/tasks'),
		'icon' => 'check-square',
		'tone' => 'emerald',
	),
	array(
		'label' => lang('proposals'),
		'count' => $lead_kpis['proposals'] ?? 0,
		'url' => base_url('admin/leads/leads_details/' . $leads_details->leads_id . '/proposals'),
		'icon' => 'file-text',
		'tone' => 'amber',
	),
	array(
		'label' => lang('reminder'),
		'count' => $lead_kpis['reminders'] ?? 0,
		'url' => base_url('admin/leads/leads_details/' . $leads_details->leads_id . '/reminder'),
		'icon' => 'bell',
		'tone' => 'rose',
	),
	array(
		'label' => lang('attachments'),
		'count' => $lead_kpis['attachments'] ?? 0,
		'url' => base_url('admin/leads/leads_details/' . $leads_details->leads_id . '/attachments'),
		'icon' => 'paperclip',
		'tone' => 'slate',
	),
);
?>

<div class="space-y-6">
	<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_profile') ?>
					</span>
					<?php if (!empty($status_label)) { ?>
						<span class="inline-flex items-center rounded-full bg-<?= $status_tone; ?>-50 px-2.5 py-1 text-xs font-semibold text-<?= $status_tone; ?>-700 dark:bg-<?= $status_tone; ?>-900/30 dark:text-<?= $status_tone; ?>-300">
							<?= html_escape($status_label); ?>
						</span>
					<?php } ?>
					<?php if (!empty($source_label)) { ?>
						<span class="inline-flex items-center rounded-full border border-border bg-background px-2.5 py-1 text-xs font-medium text-foreground">
							<?= html_escape($source_label); ?>
						</span>
					<?php } ?>
				</div>
				<h2 class="text-2xl font-semibold text-foreground"><?= html_escape($leads_details->lead_name); ?></h2>
				<div class="flex flex-wrap items-center gap-x-4 gap-y-1 text-sm text-muted-foreground">
					<?php if (!empty($leads_details->contact_name)) { ?>
						<span><?= html_escape($leads_details->contact_name); ?></span>
					<?php } ?>
					<?php if (!empty($leads_details->email)) { ?>
						<span><?= html_escape($leads_details->email); ?></span>
					<?php } ?>
					<?php if (!empty($leads_details->phone)) { ?>
						<span><?= html_escape($leads_details->phone); ?></span>
					<?php } ?>
					<?php if (!empty($leads_details->organization)) { ?>
						<span><?= html_escape($leads_details->organization); ?></span>
					<?php } ?>
				</div>
			</div>
			<div class="flex flex-wrap items-center gap-2">
				<?php if (!empty($pin_url)) { ?>
					<a href="<?= base_url('admin/projects/' . $pin_url); ?>" class="inline-flex h-9 items-center gap-2 rounded-lg border border-border bg-background px-3 text-xs font-semibold text-foreground hover:bg-muted" title="<?= $pin_title; ?>">
						<i data-lucide="pin" class="h-4 w-4"></i>
						<?= $pin_title; ?>
					</a>
				<?php } ?>
				<?php if ($leads_details->converted_client_id == 0 && !empty($can_edit) && !empty($edited)) { ?>
					<a href="<?= base_url() ?>admin/leads/create/<?= $leads_details->leads_id ?>"
						class="inline-flex h-9 items-center gap-2 rounded-lg bg-primary px-3 text-xs font-semibold text-white shadow-sm hover:bg-primary/90"
						title="<?= lang('edit'); ?>">
						<i data-lucide="square-pen" class="h-4 w-4"></i>
						<?= lang('edit'); ?>
					</a>
				<?php } ?>
				<?php if ($leads_details->converted_client_id == 0 && !empty($can_edit) && !empty($edited)) { ?>
					<?= btn_modal(
						'admin/leads/convert/' . $leads_details->leads_id,
						lang('convert_to_client'),
						lang('convert_to_client'),
						'2xl',
							'arrow-right-left',
							array(
								'class' => 'inline-flex h-9 items-center gap-2 rounded-lg border border-border bg-background px-3 text-xs font-semibold text-foreground hover:bg-muted',
								'data-confirm-action' => 'convert',
								'data-confirm-title' => lang('convert_to_client'),
								'data-confirm-accept-label' => lang('convert'),
								'data-confirm-message' => sprintf(lang('convert_record_confirm'), lang('convert'), $leads_details->lead_name),
							)
						); ?>
				<?php } ?>
			</div>
		</div>

		<div class="mt-5 grid grid-cols-1 gap-3 sm:grid-cols-2 xl:grid-cols-6">
			<?php foreach ($kpi_cards as $card) { ?>
				<a href="<?= $card['url']; ?>" class="min-w-0 rounded-xl border border-border bg-card p-4 shadow-sm transition hover:bg-muted/20">
					<div class="flex items-center justify-between">
						<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= $card['label']; ?></p>
						<span class="inline-flex h-9 w-9 items-center justify-center rounded-xl bg-<?= $card['tone']; ?>-50 text-<?= $card['tone']; ?>-600 dark:bg-<?= $card['tone']; ?>-900/30 dark:text-<?= $card['tone']; ?>-300">
							<i data-lucide="<?= $card['icon']; ?>" class="h-4 w-4"></i>
						</span>
					</div>
					<p class="mt-3 text-2xl font-semibold text-foreground"><?= (int) $card['count']; ?></p>
					<p class="mt-1 text-xs text-muted-foreground"><?= lang('view_details'); ?></p>
				</a>
			<?php } ?>
		</div>
	</div>

	<?php $this->load->view('admin/common/tabs'); ?>
</div>
