<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<style>
	.attendance-shell {
		display: grid;
		gap: 18px;
	}
	.attendance-toolbar {
		display: flex;
		flex-wrap: wrap;
		gap: 12px;
		align-items: center;
		justify-content: space-between;
	}
	.attendance-toolbar form {
		display: flex;
		flex-wrap: wrap;
		gap: 12px;
		align-items: center;
	}
	.attendance-toolbar input[type="month"] {
		border: 0;
		border-radius: 14px;
		padding: 12px 14px;
		background: var(--field-bg);
		color: var(--field-ink);
		box-shadow: inset 0 0 0 1px rgba(20, 41, 64, 0.12);
		font-family: inherit;
	}
	.attendance-summary {
		display: grid;
		grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
		gap: 12px;
	}
	.attendance-table-wrap {
		background: var(--panel);
		border-radius: 22px;
		box-shadow: var(--shadow);
		overflow: hidden;
	}
	.attendance-table {
		width: 100%;
		border-collapse: collapse;
	}
	.attendance-table th,
	.attendance-table td {
		padding: 14px;
		text-align: left;
		border-bottom: 1px solid var(--border);
		vertical-align: middle;
	}
	.attendance-table th {
		background: var(--card);
		font-size: 0.83rem;
		text-transform: uppercase;
		letter-spacing: 0.04em;
		color: var(--muted);
	}
	.attendance-table tr:last-child td {
		border-bottom: 0;
	}
	.attendance-table a {
		color: inherit;
		text-decoration: none;
	}
	.attendance-table tr:hover {
		background: rgba(77, 116, 195, 0.04);
	}
	.employee-cell strong,
	.employee-cell span {
		display: block;
	}
	.employee-cell span {
		margin-top: 4px;
		color: var(--muted);
		font-size: 0.88rem;
	}
	.count-pill {
		display: inline-flex;
		align-items: center;
		justify-content: center;
		min-width: 36px;
		padding: 6px 10px;
		border-radius: 999px;
		font-weight: 700;
	}
	.count-pill.present {
		background: rgba(46, 204, 113, 0.18);
		color: #17663a;
	}
	.count-pill.absent {
		background: rgba(231, 76, 60, 0.16);
		color: #8c2f24;
	}
	.count-pill.free {
		background: rgba(113, 128, 150, 0.16);
		color: #46576a;
	}
	.count-pill.notes {
		background: rgba(95, 143, 168, 0.16);
		color: #24485a;
	}
	.attendance-empty {
		padding: 18px;
		text-align: center;
		color: var(--muted);
	}
</style>

<?php
	$selected_month = isset($selected_month) ? (string)$selected_month : date('Y-m');
	$selected_month_label = isset($selected_month_label) ? (string)$selected_month_label : '';
	$attendance_summary_rows = isset($attendance_summary_rows) && is_array($attendance_summary_rows) ? $attendance_summary_rows : array();
	$attendance_summary = isset($attendance_summary) && is_array($attendance_summary) ? $attendance_summary : array(
		'total_employees' => 0,
		'total_present' => 0,
		'total_absent' => 0,
		'total_free' => 0,
		'total_notes' => 0,
	);
	$can_view_attendance_history = !empty($can_view_attendance_history);
?>

<div class="attendance-shell">
	<div class="attendance-toolbar panel">
		<div>
			<h2>Asistencia</h2>
			<p>Resumen mensual por empleado para detectar faltas, dias libres y notas registradas.</p>
		</div>
		<?php if ($can_view_attendance_history) { ?>
		<form method="get" action="<?php echo site_url('asistencia'); ?>">
			<input type="month" name="month" value="<?php echo htmlspecialchars($selected_month, ENT_QUOTES, 'UTF-8'); ?>" />
			<button class="btn" type="submit">Ver mes</button>
		</form>
		<?php } ?>

	</div>
	<?php if (!empty($flash_message)) { ?>
		<div class="panel" style="background:#e9f7ef; color:#1f5c3a;">
			<?php echo htmlspecialchars($flash_message, ENT_QUOTES, 'UTF-8'); ?>
		</div>
	<?php } ?>
	<?php if (!empty($flash_error)) { ?>
		<div class="panel" style="background:#fdecea; color:#7a2d2d;">
			<?php echo htmlspecialchars($flash_error, ENT_QUOTES, 'UTF-8'); ?>
		</div>
	<?php } ?>

	<section class="attendance-summary">
		<div class="stat">
			<label>Mes</label>
			<strong><?php echo htmlspecialchars($selected_month_label, ENT_QUOTES, 'UTF-8'); ?></strong>
		</div>
		<div class="stat">
			<label>Empleados</label>
			<strong><?php echo (int)$attendance_summary['total_employees']; ?></strong>
		</div>
		<div class="stat">
			<label>Asistencias</label>
			<strong><?php echo (int)$attendance_summary['total_present']; ?></strong>
		</div>
		<div class="stat">
			<label>Faltas</label>
			<strong><?php echo (int)$attendance_summary['total_absent']; ?></strong>
		</div>
		<div class="stat">
			<label>Libres</label>
			<strong><?php echo (int)$attendance_summary['total_free']; ?></strong>
		</div>
		<div class="stat">
			<label>Notas</label>
			<strong><?php echo (int)$attendance_summary['total_notes']; ?></strong>
		</div>
	</section>

	<div class="attendance-table-wrap">
		<?php if (empty($attendance_summary_rows)) { ?>
			<div class="attendance-empty">No hay empleados activos para mostrar en asistencia.</div>
		<?php } else { ?>
			<table class="attendance-table">
				<thead>
					<tr>
						<th>Empleado</th>
						<th>Asistencias</th>
						<th>Faltas</th>
						<th>Libres</th>
						<th>Notas</th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ($attendance_summary_rows as $row) { ?>
						<tr>
							<td class="employee-cell">
								<a href="<?php echo site_url('asistencia/empleado/'.(int)$row['employee_id']).'?month='.rawurlencode($selected_month); ?>">
									<strong><?php echo htmlspecialchars($row['employee_name'], ENT_QUOTES, 'UTF-8'); ?></strong>
									<?php if (!empty($row['employee_username'])) { ?>
										<span><?php echo htmlspecialchars($row['employee_username'], ENT_QUOTES, 'UTF-8'); ?></span>
									<?php } else { ?>
										<span>Ver detalle del mes</span>
									<?php } ?>
								</a>
							</td>
							<td><span class="count-pill present"><?php echo (int)$row['present_count']; ?></span></td>
							<td><span class="count-pill absent"><?php echo (int)$row['absent_count']; ?></span></td>
							<td><span class="count-pill free"><?php echo (int)$row['free_count']; ?></span></td>
							<td><span class="count-pill notes"><?php echo (int)$row['notes_count']; ?></span></td>
						</tr>
					<?php } ?>
				</tbody>
			</table>
		<?php } ?>
	</div>
</div>


