<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php
$is_income = (!empty($expense_info) && $expense_info->type == 'Income');
$perm_id = $is_income ? '30' : '31';
$edited = can_action($perm_id, 'edited');
$reminder_module = 'expense';
$all_reminder = get_result('tbl_reminders', array('module' => $reminder_module, 'module_id' => $expense_info->transactions_id));
$reminder_total = !empty($all_reminder) ? count($all_reminder) : 0;
$reminder_pending = 0;
$reminder_notified = 0;
if (!empty($all_reminder)) {
	foreach ($all_reminder as $reminder_item) {
		if ($reminder_item->notified == 'Yes') {
			$reminder_notified++;
		} else {
			$reminder_pending++;
		}
	}
}
$user_options = array();
	$all_user = $this->db->where('role_id !=', 2)->order_by('sort_user', 'ASC')->get('tbl_users')->result();
if (!empty($all_user)) {
	foreach ($all_user as $v_users) {
		$profile = $this->db->where('user_id', $v_users->user_id)->get('tbl_account_details')->row();
		if (!empty($profile)) {
			$user_options[$v_users->user_id] = $profile->fullname;
		}
	}
}
?>

<div class="space-y-6">
	<div class="flex flex-col gap-4 md:flex-row md:items-start md:justify-between">
		<div>
			<h3 class="text-lg font-semibold text-foreground"><?= lang('reminder') ?></h3>
			<p class="text-sm text-muted-foreground"><?= lang('reminder') ?> <?= lang('list') ?> and follow-up schedule for this <?= $is_income ? lang('deposit') : lang('expense') ?>.</p>
		</div>
	</div>

	<div class="grid gap-3 md:grid-cols-3">
		<div class="rounded-2xl border border-border bg-card p-4 shadow-sm">
			<div class="text-xs font-semibold uppercase tracking-[0.2em] text-muted-foreground"><?= lang('all') ?></div>
			<div class="mt-2 text-2xl font-semibold text-foreground"><?= $reminder_total ?></div>
		</div>
		<div class="rounded-2xl border border-border bg-card p-4 shadow-sm">
			<div class="text-xs font-semibold uppercase tracking-[0.2em] text-muted-foreground"><?= lang('pending') ?></div>
			<div class="mt-2 text-2xl font-semibold text-amber-600"><?= $reminder_pending ?></div>
		</div>
		<div class="rounded-2xl border border-border bg-card p-4 shadow-sm">
			<div class="text-xs font-semibold uppercase tracking-[0.2em] text-muted-foreground"><?= lang('notified') ?></div>
			<div class="mt-2 text-2xl font-semibold text-emerald-600"><?= $reminder_notified ?></div>
		</div>
	</div>

	<div class="grid gap-6 xl:grid-cols-[1.1fr_0.9fr]">
		<div class="rounded-2xl border border-border bg-card shadow-sm">
			<div class="border-b border-border px-5 py-4">
				<h4 class="text-sm font-semibold text-foreground"><?= lang('reminder') ?> <?= lang('list') ?></h4>
			</div>
			<?php if (!empty($all_reminder)) { ?>
				<div class="divide-y divide-border">
					<?php foreach ($all_reminder as $v_reminder) : ?>
						<?php $remind_user_info = $this->db->where('user_id', $v_reminder->user_id)->get('tbl_account_details')->row(); ?>
						<div id="leads_reminder_<?= $v_reminder->reminder_id ?>" class="flex flex-col gap-4 p-5 lg:flex-row lg:items-start lg:justify-between">
							<div class="space-y-3">
								<div class="flex flex-wrap items-center gap-2">
									<span class="inline-flex items-center rounded-full px-2.5 py-1 text-xs font-semibold <?= $v_reminder->notified == 'Yes' ? 'bg-emerald-100 text-emerald-700' : 'bg-amber-100 text-amber-700' ?>">
										<?= $v_reminder->notified == 'Yes' ? lang('notified') : lang('pending') ?>
									</span>
									<span class="inline-flex items-center rounded-full border border-border bg-background px-2.5 py-1 text-xs font-medium text-muted-foreground">
										<i data-lucide="calendar-clock" class="mr-1 h-3.5 w-3.5"></i>
										<?= display_date($v_reminder->date) . ' ' . display_time($v_reminder->date) ?>
									</span>
								</div>
								<div class="text-sm text-foreground"><?= !empty($v_reminder->description) ? nl2br(html_escape($v_reminder->description)) : '-' ?></div>
								<div class="text-xs text-muted-foreground">
									<?= lang('remind') ?>:
									<a href="<?= base_url() ?>admin/user/user_details/<?= $v_reminder->user_id ?>" class="font-medium text-primary hover:underline">
										<?= !empty($remind_user_info->fullname) ? html_escape($remind_user_info->fullname) : '-' ?>
									</a>
								</div>
							</div>
							<div class="flex items-center gap-2">
								<?= ajax_anchor(base_url("admin/invoice/delete_reminder/" . $v_reminder->module . '/' . $v_reminder->module_id . '/' . $v_reminder->reminder_id), lang('delete'), array("class" => "inline-flex items-center rounded-lg border border-rose-200 bg-rose-50 px-3 py-2 text-xs font-semibold text-rose-700 hover:bg-rose-100", "title" => lang('delete'), "data-fade-out-on-success" => "#leads_reminder_" . $v_reminder->reminder_id)); ?>
							</div>
						</div>
					<?php endforeach; ?>
				</div>
			<?php } else { ?>
				<div class="p-10 text-center">
					<div class="mx-auto inline-flex h-14 w-14 items-center justify-center rounded-2xl border border-border bg-background/70">
						<i data-lucide="bell-ring" class="h-6 w-6 text-muted-foreground"></i>
					</div>
					<h4 class="mt-4 text-sm font-semibold text-foreground"><?= lang('nothing_to_display') ?></h4>
					<p class="mt-1 text-sm text-muted-foreground"><?= lang('set') . ' ' . lang('reminder') ?> <?= lang('to') ?> <?= lang('follow_up') ?>.</p>
				</div>
			<?php } ?>
		</div>

		<?php if (!empty($edited)) { ?>
			<div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
				<div class="mb-4">
					<h4 class="text-sm font-semibold text-foreground"><?= lang('set') . ' ' . lang('reminder') ?></h4>
					<p class="mt-1 text-xs text-muted-foreground"><?= lang('schedule') ?> <?= lang('reminder') ?> <?= lang('for') ?> <?= $is_income ? lang('deposit') : lang('expense') ?>.</p>
				</div>
				<form role="form" data-parsley-validate="" novalidate="" enctype="multipart/form-data" id="form" action="<?= base_url(); ?>admin/invoice/reminder/<?= $reminder_module ?>/<?= $expense_info->transactions_id ?>/<?php if (!empty($reminder_info)) { echo $reminder_info->reminder_id; } ?>" method="post" class="space-y-4">
					<?= form_datepicker(
						'date',
						lang('date_to_notified'),
						!empty($reminder_info->date) ? $reminder_info->date : date('Y-m-d H:i'),
						true,
						array(
							'data-type' => 'datetime',
							'data-format' => 'Y-m-d H:i',
							'data-min-date' => date('Y-m-d H:i'),
							'data-auto-close' => 'false',
						)
					); ?>

					<?= form_textarea(
						'description',
						lang('description'),
						!empty($reminder_info) ? $reminder_info->description : '',
						5,
						false,
						array('class' => 'min-h-[120px]'),
						'stacked'
					); ?>

					<?= form_select_group(
						'user_id',
						lang('set_reminder_to'),
						$user_options,
						!empty($reminder_info) ? $reminder_info->user_id : '',
						true,
						array('data-search' => 'true')
					); ?>

					<div class="rounded-xl border border-border bg-background/50 px-4 py-3">
						<?= form_checkbox(
							'notify_by_email',
							lang('send_also_email_this_reminder'),
							(!empty($reminder_info) && $reminder_info->notify_by_email == 'Yes'),
							false,
							array('value' => 'Yes')
						); ?>
					</div>

					<div class="flex items-center justify-end gap-2 pt-2">
						<?= btn_update() ?>
					</div>
				</form>
			</div>
		<?php } ?>
	</div>
</div>
