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

$config_map = array(
	'tasks' => array(
		'perm' => '55',
		'table' => 'tbl_leads_notes',
		'field' => 'module_field_id',
		'save' => 'admin/tasks/save_tasks_notes',
		'delete' => 'admin/leads/delete_notes',
	),
);

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');

$notes = get_result($config['table'], array($config['field'] => $module_id));

$user_map = array();
if (!empty($notes)) {
	$user_ids = array();
	foreach ($notes as $note) {
		if (!empty($note->user_id)) {
			$user_ids[] = $note->user_id;
		}
	}
	$user_ids = array_values(array_unique($user_ids));
	if (!empty($user_ids)) {
		$user_rows = $this->db
			->select('tbl_users.user_id, tbl_users.username, tbl_account_details.fullname, tbl_account_details.avatar')
			->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] = array(
				'name' => !empty($user->fullname) ? $user->fullname : $user->username,
				'avatar' => !empty($user->avatar) ? $user->avatar : 'uploads/default_avatar.jpg',
			);
		}
	}
}

$lead_info = $this->db->select('last_contact')->where('task_id', $module_id)->get('tbl_tasks')->row();
$last_contact_value = !empty($lead_info->last_contact) ? $lead_info->last_contact : date('Y-m-d H:i');

$form_action = base_url($config['save'] . '/' . $module_id);
?>

<div class="space-y-6">
	<div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
		<div class="flex items-center justify-between border-b border-border pb-3">
			<h3 class="text-sm font-semibold text-foreground"><?= lang('notes') ?></h3>
			<span class="text-xs text-muted-foreground"><?= !empty($notes) ? count($notes) : 0; ?> <?= lang('records') ?></span>
		</div>

		<?php if (!empty($edited)) { ?>
			<form action="<?= $form_action; ?>" method="post" class="mt-4 space-y-4">
				<?= form_editor('notes', lang('notes'), '', false, array('data-height' => '150px')); ?>
				<div class="grid grid-cols-1 gap-3 md:grid-cols-2">
					<?= form_radio('contacted_indicator', lang('i_got_touch_with_leads'), 'touch_with_leads', false, false, array('data-contacted' => 'show')); ?>
					<?= form_radio('contacted_indicator', lang('i_have_not_contacted'), 'not_contacted', true, false, array('data-contacted' => 'hide')); ?>
				</div>
				<div id="notes_contacted_date" class="hidden">
					<?= form_datepicker('last_contact', lang('date_contacted'), $last_contact_value, false, array('data-type' => 'datetime', 'data-format' => 'Y-m-d H:i')); ?>
				</div>
				<div class="flex justify-end">
					
					<button type="submit" class="rounded-lg bg-slate-900 px-4 py-2 text-xs font-semibold text-white hover:bg-slate-800 dark:bg-slate-700 dark:hover:bg-slate-600">
						<?= lang('updates'); ?>
					</button>
				</div>
			</form>
		<?php } ?>

		<div class="mt-6 space-y-3">
			<?php if (!empty($notes)) { ?>
				<?php foreach ($notes as $note) { ?>
					<div class="flex gap-3 rounded-xl border border-slate-200 bg-white p-4" id="note-row-<?= $note->notes_id; ?>">
						<?php
						$avatar = isset($user_map[$note->user_id]) ? $user_map[$note->user_id]['avatar'] : 'uploads/default_avatar.jpg';
						$name = isset($user_map[$note->user_id]) ? $user_map[$note->user_id]['name'] : lang('unknown');
						?>
						<img alt="Avatar" src="<?= base_url($avatar); ?>" class="h-10 w-10 rounded-full border border-slate-200 object-cover">
						<div class="flex-1">
							<div class="flex items-start justify-between gap-3">
								<div>
									<a href="<?= base_url('admin/user/user_details/' . $note->user_id); ?>" class="text-sm font-semibold text-slate-900 hover:underline">
										<?= html_escape($name); ?>
									</a>
									<div class="text-xs text-slate-500"><?= time_ago($note->created_time); ?></div>
								</div>
								<div class="flex items-center gap-2">
									<?php if (!empty($note->last_contact)) { ?>
										<span class="inline-flex items-center gap-1 rounded-full bg-emerald-50 px-2 py-1 text-xs text-emerald-600" data-tooltip="<?= $note->last_contact; ?>">
											<i data-lucide="phone-call"></i>
											<?= lang('contacted_today'); ?>
										</span>
									<?php } ?>
									<?php if (!empty($deleted)) { ?>
										<?= ajax_anchor(base_url($config['delete'] . '/' . $note->notes_id . '/' . $module_id), "<i data-lucide='trash-2' class='h-4 w-4 text-rose-600'></i>", 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" => "#note-row-" . $note->notes_id)); ?>
									<?php } ?>
								</div>
							</div>
							<div class="mt-2 text-sm text-slate-700">
								<?php if (!empty($note->notes)) echo $note->notes; ?>
							</div>
						</div>
					</div>
				<?php } ?>
			<?php } else { ?>
				<div class="text-center text-sm text-muted-foreground"><?= lang('nothing_to_display') ?></div>
			<?php } ?>
		</div>
	</div>
</div>

<script>
	(function() {
		const wrap = document.currentScript && document.currentScript.parentElement;
		if (!wrap) return;
		const radios = wrap.querySelectorAll('input[name="contacted_indicator"]');
		const dateWrap = wrap.querySelector('#notes_contacted_date');
		if (!radios.length || !dateWrap) return;

		function sync() {
			let show = false;
			radios.forEach((radio) => {
				if (radio.checked && radio.value === 'touch_with_leads') {
					show = true;
				}
			});
			dateWrap.classList.toggle('hidden', !show);
		}

		radios.forEach((radio) => radio.addEventListener('change', sync));
		sync();
	})();
</script>

