<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php
$can_edit = isset($can_edit) ? $can_edit : false;
$edited = isset($edited) ? $edited : false;

$state = !empty($state_info->opportunities_state) ? $state_info->opportunities_state : '';
$state_reason = !empty($state_info->opportunities_state_reason) ? $state_info->opportunities_state_reason : '';
$state_tones = array(
	'open' => 'border-sky-200 bg-sky-50 text-sky-700 dark:border-sky-900/50 dark:bg-sky-950/25 dark:text-sky-300',
	'won' => 'border-emerald-200 bg-emerald-50 text-emerald-700 dark:border-emerald-900/50 dark:bg-emerald-950/25 dark:text-emerald-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',
	'suspended' => 'border-amber-200 bg-amber-50 text-amber-700 dark:border-amber-900/50 dark:bg-amber-950/25 dark:text-amber-300',
);
$state_tone = isset($state_tones[$state]) ? $state_tones[$state] : 'border-slate-200 bg-slate-50 text-slate-700 dark:border-slate-700 dark:bg-slate-800/60 dark:text-slate-200';

$close_overdue = false;
if (!empty($opportunity_details->close_date)) {
	$close_overdue = (safe_strtotime($opportunity_details->close_date) < safe_strtotime(date('Y-m-d')) && (float) $opportunity_details->probability < 100);
}

$expected_revenue = !empty($opportunity_details->expected_revenue)
	? display_money($opportunity_details->expected_revenue, default_currency())
	: '-';
$close_date = !empty($opportunity_details->close_date) ? display_date($opportunity_details->close_date) : '-';
$next_action_date = !empty($opportunity_details->next_action_date) ? display_date($opportunity_details->next_action_date) : '-';

$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,
		);
	}
}
?>

<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="target" class="h-3.5 w-3.5"></i>
						<?= lang('opportunity') ?>
					</span>
					<?php if (!empty($state)) { ?>
						<span class="inline-flex items-center rounded-full border px-3 py-1 text-xs font-semibold <?= $state_tone ?>">
							<?= lang($state) ?>
						</span>
					<?php } ?>
				</div>
				<h2 class="text-2xl font-semibold text-foreground"><?= html_escape($opportunity_details->opportunity_name) ?></h2>
				<p class="text-sm text-muted-foreground">
					<?= lang('stages') ?>: <span class="font-medium text-foreground"><?= !empty($opportunity_details->stages) ? lang($opportunity_details->stages) : '-' ?></span>
					<span class="mx-2 text-border">|</span>
					<?= lang('probability') ?>: <span class="font-medium text-foreground"><?= !empty($opportunity_details->probability) ? (float) $opportunity_details->probability . '%' : '0%' ?></span>
				</p>
				<?php if (!empty($state_reason)) { ?>
					<p class="text-sm text-muted-foreground">
						<?= lang('state') ?>: <span class="font-medium text-foreground"><?= html_escape($state_reason) ?></span>
					</p>
				<?php } ?>
			</div>
			<div class="flex flex-wrap items-center gap-2">
				<?php if (!empty($can_edit) && !empty($edited)) { ?>
					<a href="<?= base_url('admin/opportunities/create/' . $opportunity_details->opportunities_id) ?>" class="inline-flex h-9 items-center gap-2 rounded-lg bg-primary px-3 text-xs font-semibold text-primary-foreground hover:bg-primary/90">
						<i data-lucide="square-pen" class="h-4 w-4"></i>
						<?= lang('edit') ?>
					</a>
				<?php } ?>
			</div>
		</div>

		<div class="mt-4 grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
			<div class="rounded-xl border border-border bg-card p-3">
				<p class="text-[11px] font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('expected_revenue') ?></p>
				<p class="mt-2 text-base font-semibold text-foreground"><?= $expected_revenue ?></p>
			</div>
			<div class="rounded-xl border border-border bg-card p-3">
				<p class="text-[11px] font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('close_date') ?></p>
				<p class="mt-2 text-sm font-medium <?= $close_overdue ? 'text-rose-600 dark:text-rose-400' : 'text-foreground' ?>"><?= $close_date ?></p>
				<?php if ($close_overdue) { ?>
					<span class="mt-1 inline-flex items-center rounded-full border border-rose-200 bg-rose-50 px-2 py-0.5 text-[11px] font-semibold text-rose-700 dark:border-rose-900/50 dark:bg-rose-950/25 dark:text-rose-300"><?= lang('overdue') ?></span>
				<?php } ?>
			</div>
			<div class="rounded-xl border border-border bg-card p-3">
				<p class="text-[11px] font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('next_action') ?></p>
				<p class="mt-2 text-sm font-medium text-foreground"><?= !empty($opportunity_details->next_action) ? html_escape($opportunity_details->next_action) : '-' ?></p>
			</div>
			<div class="rounded-xl border border-border bg-card p-3">
				<p class="text-[11px] font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('next_action_date') ?></p>
				<p class="mt-2 text-sm font-medium text-foreground"><?= $next_action_date ?></p>
			</div>
		</div>
	</div>

	<div class="grid gap-5 xl:grid-cols-3">
		<div class="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 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('opportunity_name') ?></p>
						<p class="mt-1 text-sm text-foreground"><?= html_escape($opportunity_details->opportunity_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('stages') ?></p>
						<p class="mt-1 text-sm text-foreground"><?= !empty($opportunity_details->stages) ? lang($opportunity_details->stages) : '-' ?></p>
					</div>
					<div class="rounded-lg border border-border bg-background p-3">
						<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('probability') ?></p>
						<p class="mt-1 text-sm text-foreground"><?= !empty($opportunity_details->probability) ? (float) $opportunity_details->probability . '%' : '0%' ?></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($state) ? lang($state) : '-' ?></p>
					</div>
					<?php if (!empty($opportunity_details->new_link)) { ?>
						<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('new_link') ?></p>
							<a href="<?= prep_url($opportunity_details->new_link) ?>" target="_blank" class="mt-1 inline-flex break-all text-sm text-primary hover:underline"><?= html_escape($opportunity_details->new_link) ?></a>
						</div>
					<?php } ?>
				</div>
			</div>

			<?php if (!empty($opportunity_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($opportunity_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="space-y-5">
			<div class="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)) { ?>
						<?= btn_modal(
							'admin/opportunities/update_users/' . $opportunity_details->opportunities_id,
							'',
							'',
							'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>
