<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<?php $group = is_array($group) ? $group : array(); ?>
<section class="panel">
	<form method="post" action="" autocomplete="off" style="display:grid; gap:18px;">
		<div class="actions" style="justify-content:flex-start; margin-top:0; margin-bottom:4px;">
			<a class="btn btn-outline" href="<?php echo site_url('configuracion/grupos'); ?>">Regresar</a>
		</div>

		<h2 style="margin-bottom:0;"><?php echo !empty($group['id']) ? 'Editar grupo' : 'Nuevo grupo'; ?></h2>

		<div style="display:grid; gap:16px;">
			<div class="field" style="margin-top:0;">
				<label for="name">Nombre</label>
				<input id="name" type="text" name="name" value="<?php echo htmlspecialchars(set_value('name', isset($group['name']) ? $group['name'] : ''), ENT_QUOTES, 'UTF-8'); ?>" />
			</div>

			<label style="display:flex; align-items:center; gap:10px; font-weight:700; color:var(--ink);">
				<input type="checkbox" name="is_active" value="1" <?php echo set_checkbox('is_active', '1', !isset($group['is_active']) || !empty($group['is_active'])); ?> />
				<span>Grupo activo</span>
			</label>

			<div class="field" style="margin-top:0;">
				<label for="sort_order">Orden</label>
				<input id="sort_order" type="number" step="1" min="0" name="sort_order" value="<?php echo htmlspecialchars(set_value('sort_order', isset($group['sort_order']) ? (int)$group['sort_order'] : 0), ENT_QUOTES, 'UTF-8'); ?>" />
			</div>

			<div class="field" style="margin-top:0;">
				<label for="color">Color del grupo</label>
				<input id="color" type="color" name="color" value="<?php echo htmlspecialchars(set_value('color', isset($group['color']) && $group['color'] !== '' ? $group['color'] : '#4d74c3'), ENT_QUOTES, 'UTF-8'); ?>" style="height:52px; padding:6px;" />
			</div>

			<div style="padding:16px 18px; border-radius:20px; background:var(--panel); box-shadow:0 12px 24px rgba(20,41,64,0.08); display:grid; gap:14px;">
				<h3 style="margin:0;">Miembros del grupo</h3>
				<div style="display:grid; gap:10px;">
					<?php foreach ($employees as $employee) { ?>
						<label style="display:flex; align-items:center; gap:10px; font-weight:700; color:var(--ink);">
							<input type="checkbox" name="members[]" value="<?php echo (int)$employee['person_id']; ?>" <?php echo in_array((int)$employee['person_id'], $selected_members, TRUE) ? 'checked' : ''; ?> />
							<span><?php echo htmlspecialchars($employee['full_name'], ENT_QUOTES, 'UTF-8'); ?></span>
						</label>
					<?php } ?>
				</div>
			</div>
		</div>

		<div class="actions" style="justify-content:flex-end;">
			<button class="btn" type="submit">Guardar</button>
		</div>
	</form>
</section>
