<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php
$title = isset($title) ? $title : '';
$fields = isset($fields) ? $fields : array();
$empty_label = isset($empty_label) ? $empty_label : '-';
$actions = isset($actions) ? $actions : '';
$label_col = isset($label_col) ? (int) $label_col : 4;
$value_col = isset($value_col) ? (int) $value_col : 8;

if ($label_col < 1 || $label_col > 11) {
	$label_col = 4;
}
if ($value_col < 1 || $value_col > 11) {
	$value_col = 8;
}
if (($label_col + $value_col) !== 12) {
	$label_col = 4;
	$value_col = 8;
}

$col_class_map = array(
	1 => 'md:col-span-1',
	2 => 'md:col-span-2',
	3 => 'md:col-span-3',
	4 => 'md:col-span-4',
	5 => 'md:col-span-5',
	6 => 'md:col-span-6',
	7 => 'md:col-span-7',
	8 => 'md:col-span-8',
	9 => 'md:col-span-9',
	10 => 'md:col-span-10',
	11 => 'md:col-span-11',
);
$label_col_class = isset($col_class_map[$label_col]) ? $col_class_map[$label_col] : 'md:col-span-4';
$value_col_class = isset($col_class_map[$value_col]) ? $col_class_map[$value_col] : 'md:col-span-8';
?>

<div class="rounded-2xl border border-border bg-card shadow-sm">
	<?php if (!empty($title) || !empty($actions)) { ?>
		<div class="flex items-center justify-between gap-3 border-b border-border px-5 py-4">
			<?php if (!empty($title)) { ?>
				<h3 class="text-sm font-semibold text-foreground"><?= html_escape($title); ?></h3>
			<?php } ?>
			<?php if (!empty($actions)) { ?>
				<div class="text-sm text-muted-foreground"><?= $actions; ?></div>
			<?php } ?>
		</div>
	<?php } ?>

	<div class="p-5">
		<?php
		$rendered = 0;
		foreach ($fields as $field) {
			$label = isset($field['label']) ? $field['label'] : '';
			$value = isset($field['value']) ? $field['value'] : null;
			$html = isset($field['html']) ? $field['html'] : null;
			$show_empty = !empty($field['show_empty']);

			if (
				!$show_empty
				&& ($value === null || $value === '' || (is_array($value) && empty($value)))
				&& empty($html)
			) {
				continue;
			}

			$rendered++;
			$display = $html !== null ? $html : html_escape((string) $value);
			if ($display === '') {
				$display = html_escape($empty_label);
			}
		?>
			<div class="grid gap-2 border-b border-border py-3 last:border-b-0 md:grid-cols-12 md:items-start">
				<div class="<?= $label_col_class ?>">
					<div class="mt-1 text-xs font-semibold uppercase tracking-wide text-muted-foreground">
						<?= html_escape($label); ?>
					</div>
				</div>
				<div class="<?= $value_col_class ?>">
					<div class="break-words text-sm text-foreground">
						<?= $display; ?>
					</div>
				</div>
			</div>
		<?php } ?>

		<?php if ($rendered === 0) { ?>
			<div class="text-sm text-muted-foreground"><?= html_escape($empty_label); ?></div>
		<?php } ?>
	</div>
</div>
