<form method="post" action="<?php echo base_url() ?>admin/mailbox/delete_mail/sent">
	<div class="rounded-2xl border border-border bg-card shadow-sm">
		<div class="flex flex-wrap items-center justify-between gap-3 border-b border-border px-4 py-3">
			<div class="flex flex-wrap items-center gap-2 text-xs font-semibold">
				<label class="inline-flex h-9 w-9 items-center justify-center rounded-md border border-border bg-card text-foreground hover:bg-muted">

					<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">

					<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">

					<i data-lucide="refresh-cw" class="h-4 w-4"></i>
					<span class="hidden"><?= lang('refresh') ?></span>
				</a>
			</div>
			<a href="<?php echo base_url() ?>admin/mailbox/index/compose"
				class="inline-flex items-center gap-2 rounded-lg bg-indigo-600 px-4 py-2 text-xs font-semibold text-white shadow-sm hover:bg-indigo-500 dark:bg-indigo-500 dark:hover:bg-indigo-400">

				<i data-lucide="square-pen" class="h-4 w-4"></i>
				<span class="hidden"><?= lang('compose') ?></span>
			</a>
		</div>
		<div class="divide-y divide-border">
			<?php if (!empty($get_sent_message)): foreach ($get_sent_message as $v_sent_msg): ?>
					<?php
					$subject = (strlen($v_sent_msg->subject) > 50) ? strip_html_tags(mb_substr($v_sent_msg->subject, 0, 50)) . '...' : $v_sent_msg->subject;
					$string = (strlen($v_sent_msg->to) > 50) ? strip_html_tags(mb_substr($v_sent_msg->to, 0, 50)) . '...' : $v_sent_msg->to;
					$body = (strlen($v_sent_msg->message_body) > 100) ? strip_html_tags(mb_substr($v_sent_msg->message_body, 0, 100)) . '...' : $v_sent_msg->message_body;
					$uploaded_file = !empty($v_sent_msg->attach_file) ? json_decode($v_sent_msg->attach_file) : array();
					?>
					<div class="flex flex-col gap-3 px-4 py-4 md:flex-row md:items-start md:justify-between hover:bg-muted/40">
						<div class="flex 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_sent_msg->sent_id; ?>" />
							<div class="min-w-0">
								<a class="text-sm font-semibold text-foreground"
									href="<?php echo base_url() ?>admin/mailbox/index/read_send_mail/<?php echo $v_sent_msg->sent_id ?>">
									<?= $subject ?>
								</a>
								<div class="text-xs text-muted-foreground"><?= $string ?></div>
								<div class="flex items-center gap-2 text-xs text-muted-foreground">
									<span class="truncate max-w-[520px]"><?= $body ?></span>
									<?php if (!empty($uploaded_file)): ?>
										<i data-lucide="paperclip"></i>
									<?php endif; ?>
								</div>
							</div>
						</div>
						<div class="text-xs text-muted-foreground"><?= time_ago($v_sent_msg->message_time); ?></div>
					</div>
				<?php endforeach; ?>
			<?php else: ?>
				<div class="px-4 py-6 text-sm text-muted-foreground"><?= 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>

