<?php
$no_of_inv = $this->admin_model->get_permission('tbl_invoices', null, 'count(tbl_invoices.invoices_id) as num_of_inv')[0]->num_of_inv;
$not_paid = $this->admin_model->get_permission('tbl_invoices', array('tbl_invoices.status = ' => 'unpaid'), 'count(tbl_invoices.invoices_id) as num_of_inv')[0]->num_of_inv;
$fully_paid = $this->admin_model->get_permission('tbl_invoices', array('tbl_invoices.status = ' => 'paid'), 'count(tbl_invoices.invoices_id) as num_of_inv')[0]->num_of_inv;
$draft = $this->admin_model->get_permission('tbl_invoices', array('tbl_invoices.status = ' => 'Draft'), 'count(tbl_invoices.invoices_id) as num_of_inv')[0]->num_of_inv;
$partially_paid = $this->admin_model->get_permission('tbl_invoices', array('tbl_invoices.status = ' => 'partially_paid'), 'count(tbl_invoices.invoices_id) as num_of_inv')[0]->num_of_inv;
$overdue = $this->admin_model->get_permission('tbl_invoices', array('tbl_invoices.due_date < ' => safe_strtotime(date("Y-m-d")), 'tbl_invoices.status != ' => 'Paid'), 'count(tbl_invoices.invoices_id) as num_of_inv')[0]->num_of_inv;

$status_items = array(
	array(
		'id' => 'not_paid',
		'label' => lang('unpaid'),
		'count' => (int) $not_paid,
		'total' => (int) $no_of_inv,
		'tone' => 'danger',
		'width' => $no_of_inv ? ($not_paid / $no_of_inv) * 100 : 0,
	),
	array(
		'id' => 'paid',
		'label' => lang('paid'),
		'count' => (int) $fully_paid,
		'total' => (int) $no_of_inv,
		'tone' => 'success',
		'width' => $no_of_inv ? ($fully_paid / $no_of_inv) * 100 : 0,
	),
	array(
		'id' => 'partially_paid',
		'label' => lang('partially_paid'),
		'count' => (int) $partially_paid,
		'total' => (int) $no_of_inv,
		'tone' => 'warning',
		'width' => $no_of_inv ? ($partially_paid / $no_of_inv) * 100 : 0,
	),
	array(
		'id' => 'overdue',
		'label' => lang('overdue'),
		'count' => (int) $overdue,
		'total' => (int) $no_of_inv,
		'tone' => 'danger',
		'width' => $no_of_inv ? ($overdue / $no_of_inv) * 100 : 0,
	),
	array(
		'id' => 'draft',
		'label' => lang('draft'),
		'count' => (int) $draft,
		'total' => (int) $no_of_inv,
		'tone' => 'neutral',
		'width' => $no_of_inv ? ($draft / $no_of_inv) * 100 : 0,
	),
);
?>

<?php if ($this->session->userdata('user_type') == 1) { ?>
	<section id="state_report" class="rounded-xl border border-border bg-white p-4 shadow-sm dark:border-slate-700 dark:bg-slate-900">
		<div class="mb-4 flex flex-wrap justify-end gap-2">
			<span class="inline-flex items-center gap-2 rounded-full border border-emerald-200 bg-emerald-50 px-3 py-1 text-xs font-semibold text-emerald-700 dark:border-emerald-500/30 dark:bg-emerald-500/10 dark:text-emerald-300">
				<span><?= lang('paid') . ' ' . lang('invoice'); ?></span>
				<strong id="paid_invo_amount">0</strong>
			</span>
			<span class="inline-flex items-center gap-2 rounded-full border border-rose-200 bg-rose-50 px-3 py-1 text-xs font-semibold text-rose-600 dark:border-rose-500/30 dark:bg-rose-500/10 dark:text-rose-300">
				<span><?= lang('past') . ' ' . lang('due') . ' ' . lang('invoice'); ?></span>
				<strong id="past_overdue">0</strong>
			</span>
			<span class="inline-flex items-center gap-2 rounded-full border border-amber-200 bg-amber-50 px-3 py-1 text-xs font-semibold text-amber-700 dark:border-amber-500/30 dark:bg-amber-500/10 dark:text-amber-300">
				<span><?= lang('outstanding') . ' ' . lang('invoice'); ?></span>
				<strong id="client_outstanding">0</strong>
			</span>
			<div class="hidden" aria-hidden="true" id="total_invo_amount">0</div>
		</div>

		<div class="grid gap-3 sm:grid-cols-2 lg:grid-cols-5">
			<?php
			foreach ($status_items as $item) {
				$tone = !empty($item['tone']) ? $item['tone'] : 'neutral';
				$tone_class = 'text-slate-500';
				if ($tone === 'danger') {
					$tone_class = 'text-rose-500';
				} elseif ($tone === 'success') {
					$tone_class = 'text-emerald-500';
				} elseif ($tone === 'warning') {
					$tone_class = 'text-amber-500';
				}
			?>
				<a href="#" class="block rounded-lg border border-border bg-white p-3 text-sm font-semibold text-slate-700 shadow-sm transition hover:-translate-y-0.5 hover:shadow-md dark:border-slate-700 dark:bg-slate-900 dark:text-slate-200" data-filter="<?= html_escape($item['id']); ?>" data-label="<?= html_escape($item['label']); ?>">
					<div class="flex items-center justify-between text-xs text-slate-500 dark:text-slate-400">
						<span class="<?= $tone_class ?>"><?= html_escape($item['label']); ?></span>
						<span>(<?= number_format($item['width'], 2); ?>%)</span>
					</div>
					<div class="mt-2 text-sm font-semibold text-slate-700 dark:text-slate-200"><?= (int) $item['count']; ?> / <?= (int) $item['total']; ?></div>
				</a>
			<?php } ?>
		</div>
	</section>
<?php } ?>

<script>
	$(document).off('click.invoiceStatusFilter', '.invoice-status-card')
		.on('click.invoiceStatusFilter', '.invoice-status-card', function(e) {
			e.preventDefault();
			var filterBy = $(this).data('filter') || '';
			var label = $(this).data('label') || '';
			if (typeof window.applyInvoiceFilter === 'function') {
				window.applyInvoiceFilter(filterBy, '', label);
			}
		});
</script>
