<?php
$datatable = true;
echo message_box('success');
echo message_box('error');
$customer_groups = $this->db->where('type', 'client')->order_by('customer_group_id', 'DESC')->get('tbl_customer_group')->result();
$created = can_action('4', 'created');
$deleted = can_action('4', 'deleted');

// Lightweight stats
$total_clients = $this->db->count_all('tbl_client');
$new_last7 = $this->db->where('date_added >=', date('Y-m-d', safe_strtotime('-7 days')))->count_all_results('tbl_client');
$group_count = $this->db->where('type', 'client')->count_all_results('tbl_customer_group');
$contact_count = $this->db->count_all('tbl_account_details');
?>
<div class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-4 mb-4">
	<div class="rounded-2xl border border-border bg-gradient-to-br from-white via-white to-slate-50 p-4 shadow-sm dark:from-slate-900 dark:via-slate-900 dark:to-slate-800">
		<div class="flex items-center justify-between">
			<p class="text-[11px] uppercase tracking-widest text-muted-foreground"><?= lang('total_clients'); ?></p>
			<span class="inline-flex h-9 w-9 items-center justify-center rounded-xl bg-slate-900 text-white dark:bg-slate-700">
				<i data-lucide="users" class="h-4 w-4"></i>
			</span>
		</div>
		<p class="text-3xl font-bold text-foreground mt-3"><?= $total_clients; ?></p>

	</div>
	<div class="rounded-2xl border border-border bg-gradient-to-br from-white via-white to-emerald-50 p-4 shadow-sm dark:from-slate-900 dark:via-slate-900 dark:to-emerald-900/20">
		<div class="flex items-center justify-between">
			<p class="text-[11px] uppercase tracking-widest text-muted-foreground"><?= lang('new'); ?> (7d)</p>
			<span class="inline-flex h-9 w-9 items-center justify-center rounded-xl bg-emerald-600 text-white">
				<i data-lucide="trending-up" class="h-4 w-4"></i>
			</span>
		</div>
		<p class="text-3xl font-bold text-foreground mt-3"><?= $new_last7; ?></p>
	</div>
	<div class="rounded-2xl border border-border bg-gradient-to-br from-white via-white to-indigo-50 p-4 shadow-sm dark:from-slate-900 dark:via-slate-900 dark:to-indigo-900/20">
		<div class="flex items-center justify-between">
			<p class="text-[11px] uppercase tracking-widest text-muted-foreground"><?= lang('customer_group'); ?></p>
			<span class="inline-flex h-9 w-9 items-center justify-center rounded-xl bg-indigo-600 text-white">
				<i data-lucide="layers" class="h-4 w-4"></i>
			</span>
		</div>
		<p class="text-3xl font-bold text-foreground mt-3"><?= $group_count; ?></p>
	</div>
	<div class="rounded-2xl border border-border bg-gradient-to-br from-white via-white to-sky-50 p-4 shadow-sm dark:from-slate-900 dark:via-slate-900 dark:to-sky-900/20">
		<div class="flex items-center justify-between">
			<p class="text-[11px] uppercase tracking-widest text-muted-foreground"><?= lang('contacts'); ?></p>
			<span class="inline-flex h-9 w-9 items-center justify-center rounded-xl bg-sky-600 text-white">
				<i data-lucide="contact" class="h-4 w-4"></i>
			</span>
		</div>
		<p class="text-3xl font-bold text-foreground mt-3"><?= $contact_count; ?></p>
	</div>
</div>

<div class="rounded-2xl border border-border bg-card shadow-sm">
	<div class="p-6 space-y-4">
		<div class="overflow-x-auto">
			<table class="w-full text-left text-sm DataTables" id="DataTables" width="100%">
				<thead class="text-xs uppercase text-muted-foreground">
					<tr>
						<?php if (!empty($deleted)) : ?>
							<th class="w-10"><input type="checkbox" id="select_all"></th>
						<?php endif; ?>
						<th class="py-2 px-3"><?= lang('name'); ?></th>
						<th class="py-2 px-3"><?= lang('contacts'); ?></th>
						<th class="py-2 px-3"><?= lang('primary_contact'); ?></th>
						<th class="py-2 px-3"><?= lang('projects'); ?></th>
						<th class="py-2 px-3"><?= lang('outstanding'); ?></th>
						<th class="py-2 px-3"><?= lang('paid_amount'); ?></th>
						<th class="py-2 px-3"><?= lang('expenses'); ?></th>
						<th class="py-2 px-3"><?= lang('customer_group'); ?></th>
						<th class="py-2 px-3 text-right"><?= lang('action'); ?></th>
					</tr>
				</thead>
				<tbody>
				</tbody>
			</table>
		</div>
	</div>
</div>
<script type="text/javascript">
	list = base_url + "admin/client/clientList";
	window.erpDatatablePreload = window.erpDatatablePreload || {};
	window.erpDatatablePreload.DataTables = window.erpDatatablePreload.DataTables || {};
	<?php if (!empty($datatable_preload)) { ?>
	window.erpDatatablePreload.DataTables["list"] = <?= json_encode($datatable_preload, JSON_UNESCAPED_SLASHES) ?>;
	<?php } ?>
</script>
