<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/** SETTINGS — Expects: $groups (group => [rows of key/value/type/label]). */
$group_labels = array(
	'general'  => 'General',
	'contact'  => 'Contact &amp; Social',
	'seo'      => 'SEO &amp; Analytics',
	'homepage' => 'Homepage',
	'payment'  => 'Payment',
);
?>
<div class="page-head">
	<div><h2>Settings</h2><p>Manage global site configuration.</p></div>
</div>

<form action="<?= base_url('admin/settings/save') ?>" method="post">
	<?= form_hidden($this->security->get_csrf_token_name(), $this->security->get_csrf_hash()) ?>

	<?php foreach ($groups as $group => $rows): ?>
		<div class="card">
			<div class="card-head"><h3><?= $group_labels[$group] ?? ucfirst($group) ?></h3></div>
			<div class="card-body">
				<div class="form-grid cols-2">
					<?php foreach ($rows as $r):
						$key   = $r['key'];
						$val   = $r['value'];
						$type  = $r['type'];
						$label = $r['label'] ?: ucwords(str_replace('_', ' ', $key));
						$full  = in_array($type, array('textarea','html','wysiwyg')) ? 'fld-full' : '';
					?>
						<div class="fld <?= $full ?>">
							<label><?= html_escape($label) ?></label>
							<?php if ($type === 'textarea' || $type === 'html'): ?>
								<textarea name="settings[<?= html_escape($key) ?>]" <?= $type === 'html' ? 'style="font-family:monospace;font-size:.85rem;"' : '' ?>><?= html_escape($val) ?></textarea>
							<?php elseif ($type === 'boolean'): ?>
								<label class="check">
									<input type="hidden" name="settings[<?= html_escape($key) ?>]" value="0">
									<input type="checkbox" name="settings[<?= html_escape($key) ?>]" value="1" <?= $val ? 'checked' : '' ?>>
									<span>Enabled</span>
								</label>
							<?php elseif ($type === 'number'): ?>
								<input type="number" name="settings[<?= html_escape($key) ?>]" value="<?= html_escape($val) ?>">
							<?php elseif ($type === 'email'): ?>
								<input type="email" name="settings[<?= html_escape($key) ?>]" value="<?= html_escape($val) ?>">
							<?php else: ?>
								<input type="text" name="settings[<?= html_escape($key) ?>]" value="<?= html_escape($val) ?>">
							<?php endif; ?>
						</div>
					<?php endforeach; ?>
				</div>
			</div>
		</div>
	<?php endforeach; ?>

	<div class="card"><div class="card-body"><div class="form-actions">
		<button type="submit" class="btn btn-primary"><?= bloom_icon('check', 16) ?> Save All Settings</button>
	</div></div></div>
</form>
