<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<section class="panel">
	<form method="post" action="<?php echo site_url('configuracion/empleados/'.(int)$employee['person_id']); ?>" 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/empleados'); ?>">Regresar</a>
		</div>

		<h2 style="margin-bottom:0;"><?php echo htmlspecialchars($employee['full_name'], ENT_QUOTES, 'UTF-8'); ?></h2>

		<div style="display:grid; gap:16px;">
			<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:10px;">
				<h3 style="margin:0;">Activo</h3>
				<label style="display:flex; align-items:center; gap:10px; font-weight:700; color:var(--ink);">
					<input type="checkbox" name="is_enabled" value="1" <?php echo !empty($is_enabled) ? 'checked' : ''; ?> <?php echo !empty($is_self) ? 'disabled' : ''; ?> />
					<span>Empleado activo en Visor</span>
				</label>
				<?php if (!empty($is_self)) { ?>
					<input type="hidden" name="is_enabled" value="1" />
					<p style="font-size:calc(0.86rem * var(--app-text-size)); color:var(--muted);">Tu propio usuario debe mantenerse activo.</p>
				<?php } ?>
			</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;">Configuracion de permisos</h3>
				<div style="display:grid; gap:16px;">
					<?php foreach ($permission_groups as $group_label => $group_keys) { ?>
						<div style="display:grid; gap:10px; padding:14px; border-radius:18px; background:var(--card);">
							<div style="font-size:calc(0.92rem * var(--app-text-size)); color:var(--ink); font-weight:900;">
								<?php echo htmlspecialchars($group_label, ENT_QUOTES, 'UTF-8'); ?>
							</div>
							<?php foreach ($group_keys as $key) { ?>
								<?php if (!isset($permission_labels[$key])) { continue; } ?>
								<?php $encoded_key = str_replace('.', '__dot__', $key); ?>
								<label style="display:flex; align-items:center; gap:10px; font-weight:700; color:var(--ink);">
									<input
										type="checkbox"
										name="permissions[<?php echo htmlspecialchars($encoded_key, ENT_QUOTES, 'UTF-8'); ?>]"
										value="1"
										<?php echo !empty($permissions[$key]) ? 'checked' : ''; ?>
										<?php echo (!empty($is_self) && $key === 'configuracion.view') ? 'disabled' : ''; ?>
									/>
									<span><?php echo htmlspecialchars($permission_labels[$key], ENT_QUOTES, 'UTF-8'); ?></span>
								</label>
								<?php if (!empty($is_self) && $key === 'configuracion.view') { ?>
									<input type="hidden" name="permissions[<?php echo htmlspecialchars($encoded_key, ENT_QUOTES, 'UTF-8'); ?>]" value="1" />
								<?php } ?>
							<?php } ?>
						</div>
					<?php } ?>

					<?php
					$grouped_keys = array();
					foreach ($permission_groups as $group_keys)
					{
						foreach ((array) $group_keys as $group_key)
						{
							$grouped_keys[$group_key] = TRUE;
						}
					}
					?>
					<?php foreach ($permission_labels as $key => $label) { ?>
						<?php if (isset($grouped_keys[$key])) { continue; } ?>
						<?php $encoded_key = str_replace('.', '__dot__', $key); ?>
						<label style="display:flex; align-items:center; gap:10px; font-weight:700; color:var(--ink);">
							<input
								type="checkbox"
								name="permissions[<?php echo htmlspecialchars($encoded_key, ENT_QUOTES, 'UTF-8'); ?>]"
								value="1"
								<?php echo !empty($permissions[$key]) ? 'checked' : ''; ?>
								<?php echo (!empty($is_self) && $key === 'configuracion.view') ? 'disabled' : ''; ?>
							/>
							<span><?php echo htmlspecialchars($label, ENT_QUOTES, 'UTF-8'); ?></span>
						</label>
						<?php if (!empty($is_self) && $key === 'configuracion.view') { ?>
							<input type="hidden" name="permissions[<?php echo htmlspecialchars($encoded_key, ENT_QUOTES, 'UTF-8'); ?>]" value="1" />
						<?php } ?>
					<?php } ?>
				</div>
			</div>
		</div>

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