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

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

$perm_map = array(
	'leads' => '55',
	'opportunities' => '56',
);

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

$perm_id = isset($perm_map[$module]) ? $perm_map[$module] : null;
$edited = !empty($perm_id) ? can_action($perm_id, 'edited') : true;
$deleted = !empty($perm_id) ? can_action($perm_id, 'deleted') : true;

$reminders = get_result('tbl_reminders', array('module' => $module, 'module_id' => $module_id));

$user_map = array();
if (!empty($reminders)) {
	$user_ids = array();
	foreach ($reminders as $reminder) {
		if (!empty($reminder->user_id)) {
			$user_ids[] = $reminder->user_id;
		}
	}
	$user_ids = array_values(array_unique($user_ids));
	if (!empty($user_ids)) {
		$user_rows = $this->db->where_in('user_id', $user_ids)->get('tbl_account_details')->result();
		foreach ($user_rows as $user) {
			$user_map[$user->user_id] = $user->fullname;
		}
	}
}

?>

<div class="space-y-6">
	<div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
		<div class="flex flex-wrap items-center justify-between gap-3 border-b border-border pb-3">
			<h3 class="text-sm font-semibold text-foreground"><?= lang('reminder') ?></h3>
			<div class="flex items-center gap-2">
				<span class="text-xs text-muted-foreground"><?= !empty($reminders) ? count($reminders) : 0; ?> <?= lang('records') ?></span>
				<?php if (!empty($edited)) { ?>
					<?= btn_modal(
						'admin/common/new_reminder/' . $module . '/' . $module_id,
						lang('new') . ' ' . lang('reminder'),
						lang('new') . ' ' . lang('reminder'),
						'lg',
						'plus',
						array('class' => 'inline-flex items-center gap-2 rounded-lg bg-slate-900 px-3 py-2 text-xs font-semibold text-white hover:bg-slate-800 dark:hover:bg-slate-700')
					); ?>
				<?php } ?>
			</div>
		</div>

		<div class="mt-6 grid grid-cols-1 gap-4 lg:grid-cols-2">
			<?php if (!empty($reminders)) { ?>
				<?php foreach ($reminders as $reminder) { ?>
					<div class="rounded-xl border border-border bg-card shadow-sm" id="reminder-row-<?= $reminder->reminder_id; ?>">
						<div class="flex flex-wrap items-start justify-between gap-3 border-b border-border px-4 py-3">
							<div class="space-y-1">
								<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('reminder'); ?></p>
								<h4 class="text-sm font-semibold text-foreground">
									<?= lang('reminder'); ?> #<?= $reminder->reminder_id; ?>
								</h4>
								<div class="flex flex-wrap items-center gap-3 text-xs text-muted-foreground">
									<span class="inline-flex items-center gap-1">
										<i data-lucide="calendar" class="h-3.5 w-3.5"></i>
										<?= display_datetime($reminder->date); ?>
									</span>
									<span class="inline-flex items-center gap-1">
										<i data-lucide="user" class="h-3.5 w-3.5"></i>
										<?php if (!empty($reminder->user_id) && isset($user_map[$reminder->user_id])) { ?>
											<?= $user_map[$reminder->user_id]; ?>
										<?php } else { ?>
											-
										<?php } ?>
									</span>
									<span class="inline-flex items-center gap-1">
										<i data-lucide="bell" class="h-3.5 w-3.5"></i>
										<span class="inline-flex items-center rounded-full bg-slate-100 px-2 py-0.5 text-[11px] font-semibold text-slate-600 dark:bg-slate-800 dark:text-slate-300">
											<?= html_escape($reminder->notified); ?>
										</span>
									</span>
								</div>
							</div>
							<?php if (!empty($deleted)) { ?>
								<div class="flex items-center gap-2">
									<?= ajax_anchor(
										base_url("admin/invoice/delete_reminder/" . $reminder->module . '/' . $reminder->module_id . '/' . $reminder->reminder_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 dark:border-rose-900/50 dark:bg-rose-900/20 dark:text-rose-300 dark:hover:bg-rose-900/40",
											"title" => lang('delete'),
											"data-fade-out-on-success" => "#reminder-row-" . $reminder->reminder_id
										)
									); ?>
								</div>
							<?php } ?>
						</div>
						<div class="space-y-2 px-4 py-3">
							<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('notes'); ?></p>
							<p class="text-sm text-foreground"><?= !empty($reminder->description) ? html_escape($reminder->description) : '-'; ?></p>
						</div>
					</div>
				<?php } ?>
			<?php } else { ?>
				<div class="text-center text-sm text-muted-foreground lg:col-span-2"><?= lang('nothing_to_display') ?></div>
			<?php } ?>
		</div>
	</div>
</div>
