<form method="post" action="<?php echo base_url() ?>client/mailbox/delete_mail/inbox">
	<div class="rounded-2xl border border-border bg-card shadow-sm">
		<div class="border-b border-border px-5 py-4">
			<div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
				<div class="flex flex-wrap items-center gap-2">
					<label class="inline-flex h-9 w-9 items-center justify-center rounded-md border border-border bg-card text-foreground hover:bg-muted" title="<?= lang('select_all') ?>">
						<input type="checkbox" id="parent_present" class="h-4 w-4 rounded border-border">
						<span class="hidden"><?= lang('select_all') ?></span>
					</label>
					<button type="submit" class="inline-flex h-9 w-9 items-center justify-center rounded-md border border-border bg-card text-foreground hover:bg-muted" title="<?= lang('delete') ?>">
						<i data-lucide="trash" class="h-4 w-4"></i>
						<span class="hidden"><?= lang('delete') ?></span>
					</button>
					<a href="#" onClick="history.go(0)" class="inline-flex h-9 w-9 items-center justify-center rounded-md border border-border bg-card text-foreground hover:bg-muted" title="<?= lang('refresh') ?>">
						<i data-lucide="refresh-cw" class="h-4 w-4"></i>
						<span class="hidden"><?= lang('refresh') ?></span>
					</a>
				</div>
				<div class="shrink-0">
					<a href="<?php echo base_url() ?>client/mailbox/index/compose"
						class="inline-flex h-9 w-9 items-center justify-center rounded-md bg-indigo-600 text-white shadow-sm hover:bg-indigo-500" title="<?= lang('compose') ?>">
						<i data-lucide="square-pen" class="h-4 w-4"></i>
						<span class="hidden"><?= lang('compose') ?></span>
					</a>
				</div>
			</div>
		</div>
		<div class="divide-y divide-border">
			<?php if (!empty($get_inbox_message)): foreach ($get_inbox_message as $v_inbox_msg): ?>
					<?php
					$subject = (strlen($v_inbox_msg->subject) > 70) ? strip_html_tags(mb_substr($v_inbox_msg->subject, 0, 70)) . '...' : $v_inbox_msg->subject;
					$toDisplay = (strlen($v_inbox_msg->to) > 70) ? strip_html_tags(mb_substr($v_inbox_msg->to, 0, 70)) . '...' : $v_inbox_msg->to;
					$body = (strlen($v_inbox_msg->message_body) > 140) ? strip_html_tags(mb_substr($v_inbox_msg->message_body, 0, 140)) . '...' : $v_inbox_msg->message_body;
					$uploaded_file = !empty($v_inbox_msg->attach_file) ? json_decode($v_inbox_msg->attach_file) : array();
					$is_unread = $v_inbox_msg->view_status != 1;
					?>
					<div class="group px-5 py-4 transition hover:bg-muted/40">
						<div class="flex flex-col gap-3 lg:flex-row lg:items-start lg:justify-between">
							<div class="flex min-w-0 items-start gap-3">
								<input class="child_present mt-1 h-4 w-4 rounded border-border" type="checkbox" name="selected_id[]"
									value="<?php echo $v_inbox_msg->inbox_id; ?>" />
								<a class="mt-0.5 text-amber-400 transition hover:text-amber-500" href="<?php echo base_url() ?>client/mailbox/index/added_favourites/<?php echo $v_inbox_msg->inbox_id ?>/<?= $v_inbox_msg->favourites == 1 ? '0' : '1' ?>">
									<i data-lucide="<?= $v_inbox_msg->favourites == 1 ? 'star' : 'star-off' ?>" class="h-4 w-4"></i>
								</a>
								<div class="min-w-0">
									<div class="flex flex-wrap items-center gap-2">
										<a class="truncate text-sm <?= $is_unread ? 'font-bold text-foreground' : 'font-semibold text-foreground' ?>"
											href="<?php echo base_url() ?>client/mailbox/index/read_inbox_mail/<?php echo $v_inbox_msg->inbox_id ?>">
											<?= $subject ?>
										</a>
										<?php if ($is_unread) { ?>
											<span class="inline-flex items-center rounded-full bg-indigo-100 px-2 py-0.5 text-[11px] font-semibold text-indigo-700 dark:bg-indigo-900/40 dark:text-indigo-200">New</span>
										<?php } ?>
									</div>
									<div class="mt-0.5 text-xs text-muted-foreground <?= $is_unread ? 'font-semibold' : '' ?>"><?= $toDisplay ?></div>
									<div class="mt-1 flex items-center gap-2 text-xs text-muted-foreground">
										<span class="truncate max-w-[640px]">
											<?= $body ?>
										</span>
										<?php if (!empty($uploaded_file)): ?>
											<i data-lucide="paperclip" class="h-3.5 w-3.5"></i>
										<?php endif; ?>
									</div>
								</div>
							</div>
							<div class="shrink-0 text-xs font-medium text-muted-foreground"><?= time_ago($v_inbox_msg->message_time); ?></div>
						</div>
					</div>
				<?php endforeach; ?>
			<?php else: ?>
				<div class="px-5 py-10 text-center text-sm text-muted-foreground">
					<i data-lucide="inbox" class="mx-auto mb-2 h-5 w-5"></i>
					<?= lang('no_data') ?>
				</div>
			<?php endif; ?>
		</div>
	</div>
</form>

<script>
	$(function() {
		var $parent = $('#parent_present');
		var $children = $('.child_present');

		function syncParent() {
			var total = $children.length;
			var checked = $children.filter(':checked').length;
			$parent.prop('checked', total > 0 && checked === total);
			$parent.prop('indeterminate', checked > 0 && checked < total);
		}

		$parent.on('change', function() {
			$children.prop('checked', $(this).prop('checked'));
			$parent.prop('indeterminate', false);
		});

		$children.on('change', syncParent);
		syncParent();
	});
</script>
