<?php defined('BASEPATH') or exit('No direct script access allowed');

if ($this->session->userdata('user_type') == 1) {
	$state_display = config_item('credit_note_state');
	$currency = $this->db->where(array('code' => config_item('default_currency')))->get('tbl_currencies')->row();
	$currency_symbol = !empty($currency->symbol) ? $currency->symbol : default_currency();

	$all_credit_notes = $this->credit_note_model->get_permission('tbl_credit_note');
	$total_count = !empty($all_credit_notes) ? count($all_credit_notes) : 0;
	$total_value = 0;
	$this_month_value = 0;
	$this_month_count = 0;
	$open_value = 0;
	$refund_value = 0;
	$closed_value = 0;
	$void_value = 0;
	$open_count = 0;
	$refund_count = 0;
	$closed_count = 0;
	$void_count = 0;
	$current_month = date('Y-m');

	if (!empty($all_credit_notes)) {
		foreach ($all_credit_notes as $credit_note) {
			$note_total = (float) $this->credit_note_model->credit_note_calculation('total', $credit_note->credit_note_id);
			$total_value += $note_total;

			if (!empty($credit_note->credit_note_date) && date('Y-m', safe_strtotime($credit_note->credit_note_date)) === $current_month) {
				$this_month_value += $note_total;
				$this_month_count++;
			}

			switch ((string) $credit_note->status) {
				case 'open':
					$open_count++;
					$open_value += $note_total;
					break;
				case 'refund':
					$refund_count++;
					$refund_value += $note_total;
					break;
				case 'closed':
					$closed_count++;
					$closed_value += $note_total;
					break;
				case 'void':
					$void_count++;
					$void_value += $note_total;
					break;
			}
		}
	}

	$summary_cards = array(
		array(
			'label' => 'Total Credit Notes',
			'value' => number_format($total_count),
			'meta' => display_money($total_value, $currency_symbol),
			'icon' => 'receipt-text',
			'class' => 'border-blue-200/70 bg-blue-50/70 text-blue-700 dark:border-blue-900/40 dark:bg-blue-950/20 dark:text-blue-300',
		),
		array(
			'label' => 'This Month',
			'value' => number_format($this_month_count),
			'meta' => display_money($this_month_value, $currency_symbol),
			'icon' => 'calendar-days',
			'class' => 'border-violet-200/70 bg-violet-50/70 text-violet-700 dark:border-violet-900/40 dark:bg-violet-950/20 dark:text-violet-300',
		),
		array(
			'label' => 'Open Exposure',
			'value' => display_money($open_value, $currency_symbol),
			'meta' => $open_count . ' ' . lang('open'),
			'icon' => 'folder-open',
			'class' => 'border-amber-200/70 bg-amber-50/70 text-amber-700 dark:border-amber-900/40 dark:bg-amber-950/20 dark:text-amber-300',
		),
	);

	$status_cards = array(
		array('label' => lang('open'), 'count' => $open_count, 'amount' => $open_value, 'tone' => 'amber', 'icon' => 'folder-open'),
		array('label' => lang('closed'), 'count' => $closed_count, 'amount' => $closed_value, 'tone' => 'emerald', 'icon' => 'badge-check'),
		array('label' => lang('refund'), 'count' => $refund_count, 'amount' => $refund_value, 'tone' => 'sky', 'icon' => 'rotate-ccw'),
		array('label' => lang('void'), 'count' => $void_count, 'amount' => $void_value, 'tone' => 'rose', 'icon' => 'ban'),
	);
	?>

	<div id="state_report" style="display: <?= html_escape($state_display) ?>">
		<div class="mb-4 grid gap-4 md:grid-cols-2 xl:grid-cols-3">
			<?php foreach ($summary_cards as $card) { ?>
				<div class="rounded-2xl border p-5 shadow-sm <?= $card['class'] ?>">
					<div class="flex items-start justify-between gap-3">
						<div>
							<p class="text-xs font-semibold uppercase tracking-[0.18em] opacity-80"><?= html_escape($card['label']) ?></p>
							<p class="mt-3 text-3xl font-semibold tracking-tight"><?= $card['value'] ?></p>
							<p class="mt-2 text-sm font-medium opacity-80"><?= $card['meta'] ?></p>
						</div>
						<div class="inline-flex h-11 w-11 items-center justify-center rounded-xl bg-white/70 dark:bg-slate-900/40">
							<i data-lucide="<?= html_escape($card['icon']) ?>" class="h-5 w-5"></i>
						</div>
					</div>
				</div>
			<?php } ?>
		</div>

		<div class="grid gap-4 md:grid-cols-2 xl:grid-cols-4">
			<?php foreach ($status_cards as $card) {
				$percent = $total_count > 0 ? round(($card['count'] / $total_count) * 100, 1) : 0;
				$bar_class = 'bg-' . $card['tone'] . '-500';
				$text_class = 'text-' . $card['tone'] . '-600 dark:text-' . $card['tone'] . '-300';
				?>
				<div class="rounded-2xl border border-border bg-card p-4 shadow-sm">
					<div class="flex items-start justify-between gap-3">
						<div>
							<p class="text-xs font-semibold uppercase tracking-[0.18em] text-muted-foreground"><?= html_escape($card['label']) ?></p>
							<p class="mt-2 text-2xl font-semibold text-foreground"><?= display_money($card['amount'], $currency_symbol) ?></p>
						</div>
						<div class="inline-flex h-10 w-10 items-center justify-center rounded-xl bg-muted/60 <?= $text_class ?>">
							<i data-lucide="<?= html_escape($card['icon']) ?>" class="h-4.5 w-4.5"></i>
						</div>
					</div>
					<div class="mt-4 flex items-center justify-between gap-2 text-sm">
						<span class="font-medium <?= $text_class ?>"><?= $card['count'] ?> <?= strtolower($card['label']) ?></span>
						<span class="text-muted-foreground"><?= $percent ?>%</span>
					</div>
					<div class="mt-3 h-2.5 w-full rounded-full bg-muted">
						<div class="h-2.5 rounded-full <?= $bar_class ?>" style="width: <?= $percent ?>%"></div>
					</div>
				</div>
			<?php } ?>
		</div>
	</div>
<?php } ?>
