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

<?php
$module = isset($module) ? $module : '';
$module_id = isset($id) ? $id : null;

$config_map = array(
	'leads' => array(
		'perm' => '55',
		'field' => 'leads_id',
		'base' => 'admin/leads/leads_details',
		'save' => 'admin/leads/saved_call',
		'delete' => 'admin/leads/delete_leads_call',
		'where' => array(),
	),
	'opportunities' => array(
		'perm' => '56',
		'field' => 'opportunities_id',
		'base' => 'admin/opportunities/opportunity_details',
		'save' => 'admin/opportunities/saved_call',
		'delete' => 'admin/opportunities/delete_opportunity_call',
		'where' => array(),
	),
	'deals' => array(
		'perm' => '55',
		'field' => 'module_field_id',
		'base' => 'admin/deals/details',
		'save' => 'admin/deals/saved_call',
		'delete' => 'admin/deals/delete_deals_call',
		'where' => array('module' => 'deals'),
	),
);

if (empty($module) || empty($module_id) || !isset($config_map[$module])) {
	echo '<div class="text-sm text-muted-foreground">' . lang('there_in_no_value') . '</div>';
	return;
}

$config = $config_map[$module];
$edited = can_action($config['perm'], 'edited');
$deleted = can_action($config['perm'], 'deleted');

$where = array_merge(array($config['field'] => $module_id), !empty($config['where']) ? $config['where'] : array());
$calls = get_result('tbl_calls', $where);

$client_map = array();
$user_map = array();
if (!empty($calls)) {
	$client_ids = array();
	$user_ids = array();
	foreach ($calls as $call) {
		if (!empty($call->client_id)) {
			$client_ids[] = $call->client_id;
		}
		if (!empty($call->user_id)) {
			$user_ids[] = $call->user_id;
		}
	}
	$client_ids = array_values(array_unique($client_ids));
	$user_ids = array_values(array_unique($user_ids));

	if (!empty($client_ids)) {
		$clients = $this->db->where_in('client_id', $client_ids)->get('tbl_client')->result();
		foreach ($clients as $client) {
			$client_map[$client->client_id] = $client->name;
		}
	}
	if (!empty($user_ids)) {
		$user_rows = $this->db
			->select('tbl_users.user_id, tbl_users.username, tbl_account_details.fullname')
			->from('tbl_users')
			->join('tbl_account_details', 'tbl_account_details.user_id = tbl_users.user_id', 'left')
			->where_in('tbl_users.user_id', $user_ids)
			->get()->result();
		foreach ($user_rows as $user) {
			$user_map[$user->user_id] = !empty($user->fullname) ? $user->fullname : $user->username;
		}
	}
}

?>

<div class="space-y-6" data-call-log-panel="<?= html_escape($module . '-' . $module_id) ?>">
	<div class="rounded-2xl border border-border bg-card p-5 shadow-sm md:p-6">
		<div class="flex flex-wrap items-center justify-between gap-3 border-b border-border pb-4">
			<div>
				<p class="text-[11px] uppercase tracking-wider text-muted-foreground"><?= lang('all_call') ?></p>
				<h3 class="mt-1 text-base font-semibold text-foreground"><?= lang('call') ?></h3>
			</div>
			<div class="flex items-center gap-2.5">
				<span class="inline-flex items-center rounded-md border border-border bg-muted/40 px-2.5 py-1 text-xs font-medium text-muted-foreground"><?= !empty($calls) ? count($calls) : 0; ?> <?= lang('records') ?></span>
				<?php if (!empty($edited)) { ?>
					<?= btn_modal(
						'admin/common/new_call/' . $module . '/' . $module_id,
						lang('new') . ' ' . lang('call'),
						lang('new') . ' ' . lang('call'),
						'lg',
						'plus',
						array('class' => 'inline-flex items-center gap-2 rounded-md bg-amber-600 px-3 py-2 text-xs font-semibold text-white hover:bg-amber-500 dark:bg-amber-700 dark:hover:bg-amber-600')
					); ?>
				<?php } ?>
			</div>
		</div>

		<div class="mt-6 grid grid-cols-1 gap-4 xl:grid-cols-2">
			<?php if (!empty($calls)) { ?>
				<?php foreach ($calls as $call) { ?>
					<?php
					$contact_label = !empty($call->client_id) && isset($client_map[$call->client_id]) ? $client_map[$call->client_id] : '';
					$responsible_label = !empty($call->user_id) && isset($user_map[$call->user_id]) ? $user_map[$call->user_id] : '-';
					$title = $contact_label !== '' ? $contact_label : lang('call');
					?>
					<div class="overflow-hidden rounded-2xl border border-border bg-card shadow-sm transition hover:shadow-md" id="call-row-<?= $call->calls_id; ?>">
						<div class="grid grid-cols-[6px_1fr]">
							<div class="bg-slate-300 dark:bg-slate-700"></div>
							<div class="space-y-3 p-4">
								<div class="flex flex-wrap items-start justify-between gap-3">
									<div class="space-y-2">
										<p class="text-[10px] font-semibold uppercase tracking-wider text-muted-foreground"><?= lang('call'); ?></p>
										<h4 class="text-sm font-semibold text-foreground md:text-base"><?= html_escape($title); ?></h4>
										<div class="flex flex-wrap items-center gap-2 text-xs text-muted-foreground">
											<span class="inline-flex items-center gap-1 rounded-full bg-muted px-2.5 py-1">
												<i data-lucide="calendar" class="h-3.5 w-3.5"></i>
												<?= display_date($call->date); ?>
											</span>
											<span class="inline-flex items-center gap-1 rounded-full bg-muted px-2.5 py-1">
												<i data-lucide="user-check" class="h-3.5 w-3.5"></i>
												<?= $responsible_label; ?>
											</span>
										</div>
									</div>
									<?php if (!empty($edited) || !empty($deleted)) { ?>
										<div class="flex items-center">
											<?php if (!empty($edited)) { ?>
												<?= btn_modal(
													'admin/common/new_call/' . $module . '/' . $module_id . '/' . $call->calls_id,
													'',
													lang('edit') . ' ' . lang('call'),
													'lg',
													'pencil',
													array('class' => 'btn h-7 btn-xs btn-primary mb-1')
												); ?>
											<?php } ?>
											<?php if (!empty($deleted)) { ?>


												 <?= ajax_anchor(base_url($config['delete'] . '/' . $module_id . '/' . $call->calls_id), "", array("class" => "inline-flex items-center justify-center rounded-lg border border-rose-200 bg-rose-50 p-2 text-rose-600 hover:bg-rose-100", "title" => lang('delete'), "data-fade-out-on-success" => "#call-row-" . $call->calls_id)); ?>
											
											<?php } ?>
										</div>
									<?php } ?>
								</div>
								<div class="rounded-xl border border-border bg-muted/25 p-3">
									<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('notes'); ?></p>
									<p class="mt-1 text-sm leading-relaxed text-foreground"><?= !empty($call->call_summary) ? $call->call_summary : '-'; ?></p>
								</div>
							</div>
						</div>
					</div>
				<?php } ?>
			<?php } else { ?>
				<div class="rounded-xl border border-dashed border-border bg-muted/30 px-4 py-10 text-center text-sm text-muted-foreground xl:col-span-2"><?= lang('nothing_to_display') ?></div>
			<?php } ?>
		</div>
	</div>
</div>
