<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<section class="panel">
	<div class="actions" style="justify-content:flex-start; margin-top:0; margin-bottom:18px;">
		<a class="btn btn-outline" href="<?php echo site_url('configuracion/tareas'); ?>">Regresar</a>
	</div>

	<h2 style="margin-bottom:16px;">Seguimiento de tareas</h2>

	<?php if (!empty($flash_message)) { ?>
		<div style="margin-bottom:14px; padding:12px 14px; border-radius:16px; background:#e8f6ed; color:#1f6a35; font-weight:700;">
			<?php echo htmlspecialchars($flash_message, ENT_QUOTES, 'UTF-8'); ?>
		</div>
	<?php } ?>

	<?php if (!empty($flash_error)) { ?>
		<div style="margin-bottom:14px; padding:12px 14px; border-radius:16px; background:#fdecec; color:#8a1f1f; font-weight:700;">
			<?php echo htmlspecialchars($flash_error, ENT_QUOTES, 'UTF-8'); ?>
		</div>
	<?php } ?>

	<div class="card-grid" style="margin-bottom:18px;">
		<div class="stat">
			<label>Total</label>
			<strong><?php echo (int) $summary['total']; ?></strong>
		</div>
		<div class="stat">
			<label>Pendientes</label>
			<strong><?php echo (int) $summary['pending']; ?></strong>
		</div>
		<div class="stat">
			<label>En progreso</label>
			<strong><?php echo (int) $summary['in_progress']; ?></strong>
		</div>
		<div class="stat">
			<label>Vencidas</label>
			<strong><?php echo (int) $summary['overdue']; ?></strong>
		</div>
		<div class="stat">
			<label>Completadas</label>
			<strong><?php echo (int) $summary['completed']; ?></strong>
		</div>
	</div>

	<form method="get" action="<?php echo site_url('configuracion/tareas/seguimiento'); ?>" style="display:grid; gap:14px; margin-bottom:18px;">
		<div class="field" style="margin-top:0;">
			<label for="q">Buscar</label>
			<input id="q" type="text" name="q" value="<?php echo htmlspecialchars(isset($filters['q']) ? $filters['q'] : '', ENT_QUOTES, 'UTF-8'); ?>" placeholder="Empleado, tarea o grupo" />
		</div>

		<div style="display:grid; grid-template-columns:repeat(3, minmax(0, 1fr)); gap:12px;">
			<div class="field" style="margin-top:0;">
				<label for="employee_id">Empleado</label>
				<select id="employee_id" name="employee_id">
					<option value="0">Todos</option>
					<?php foreach ($employees as $employee) { ?>
						<option value="<?php echo (int) $employee['person_id']; ?>" <?php echo !empty($filters['employee_id']) && (int) $filters['employee_id'] === (int) $employee['person_id'] ? 'selected' : ''; ?>>
							<?php echo htmlspecialchars($employee['full_name'], ENT_QUOTES, 'UTF-8'); ?>
						</option>
					<?php } ?>
				</select>
			</div>

			<div class="field" style="margin-top:0;">
				<label for="group_id">Grupo</label>
				<select id="group_id" name="group_id">
					<option value="0">Todos</option>
					<?php foreach ($groups as $group) { ?>
						<option value="<?php echo (int) $group['id']; ?>" <?php echo !empty($filters['group_id']) && (int) $filters['group_id'] === (int) $group['id'] ? 'selected' : ''; ?>>
							<?php echo htmlspecialchars($group['name'], ENT_QUOTES, 'UTF-8'); ?>
						</option>
					<?php } ?>
				</select>
			</div>

			<div class="field" style="margin-top:0;">
				<label for="status">Estado</label>
				<select id="status" name="status">
					<option value="all" <?php echo isset($filters['status']) && $filters['status'] === 'all' ? 'selected' : ''; ?>>Todos</option>
					<option value="pending" <?php echo isset($filters['status']) && $filters['status'] === 'pending' ? 'selected' : ''; ?>>Pendientes</option>
					<option value="in_progress" <?php echo isset($filters['status']) && $filters['status'] === 'in_progress' ? 'selected' : ''; ?>>En progreso</option>
					<option value="overdue" <?php echo isset($filters['status']) && $filters['status'] === 'overdue' ? 'selected' : ''; ?>>Vencidas</option>
					<option value="completed" <?php echo isset($filters['status']) && $filters['status'] === 'completed' ? 'selected' : ''; ?>>Completadas</option>
				</select>
			</div>
		</div>

		<div class="actions" style="justify-content:flex-end;">
			<a class="btn btn-outline" href="<?php echo site_url('configuracion/tareas/seguimiento'); ?>">Limpiar</a>
			<button class="btn" type="submit">Filtrar</button>
		</div>
	</form>

	<?php if (empty($assignments)) { ?>
		<div class="empty">No hay tareas que coincidan con este filtro.</div>
	<?php } else { ?>
		<div style="display:grid; gap:16px;">
			<?php foreach ($assignments as $assignment) { ?>
				<?php
				$status = isset($assignment['status']) ? (string) $assignment['status'] : 'pending';
				$status_label = $status === 'completed' ? 'Completada' : ($status === 'overdue' ? 'Vencida' : ($status === 'in_progress' ? 'En progreso' : 'Pendiente'));
				$status_bg = $status === 'completed' ? '#e8f6ed' : ($status === 'overdue' ? '#fdecec' : '#edf3ff');
				$status_fg = $status === 'completed' ? '#1f6a35' : ($status === 'overdue' ? '#8a1f1f' : '#355ca8');
				?>
				<div style="padding:18px; border-radius:24px; background:var(--panel); box-shadow:0 12px 24px rgba(20,41,64,0.08); display:grid; gap:14px;">
					<div style="display:flex; align-items:flex-start; justify-content:space-between; gap:12px;">
						<div style="display:grid; gap:8px;">
							<div style="font-size:calc(1.02rem * var(--app-text-size)); color:var(--ink); font-weight:900;">
								<?php echo htmlspecialchars($assignment['title'], ENT_QUOTES, 'UTF-8'); ?>
							</div>
							<div style="display:flex; flex-wrap:wrap; gap:8px;">
								<span style="display:inline-flex; align-items:center; padding:6px 10px; border-radius:999px; background:#f0f4f8; color:#516277; font-size:calc(0.78rem * var(--app-text-size)); font-weight:800;">
									<?php echo htmlspecialchars($assignment['employee_name'], ENT_QUOTES, 'UTF-8'); ?>
								</span>
								<?php if (!empty($assignment['group_name'])) { ?>
									<span style="display:inline-flex; align-items:center; padding:6px 10px; border-radius:999px; background:#f8fafc; color:var(--ink); font-size:calc(0.78rem * var(--app-text-size)); font-weight:800;">
										Grupo: <?php echo htmlspecialchars($assignment['group_name'], ENT_QUOTES, 'UTF-8'); ?>
									</span>
								<?php } ?>
								<span style="display:inline-flex; align-items:center; padding:6px 10px; border-radius:999px; background:<?php echo $status_bg; ?>; color:<?php echo $status_fg; ?>; font-size:calc(0.78rem * var(--app-text-size)); font-weight:800;">
									<?php echo htmlspecialchars($status_label, ENT_QUOTES, 'UTF-8'); ?>
								</span>
							</div>
						</div>
						<div style="min-width:120px; text-align:right;">
							<div style="font-size:calc(1.2rem * var(--app-text-size)); color:var(--theme); font-weight:900;">
								<?php echo (int) $assignment['items_completed']; ?>/<?php echo (int) $assignment['items_total']; ?>
							</div>
							<div style="font-size:calc(0.82rem * var(--app-text-size)); color:var(--muted); font-weight:700;">
								subtareas
							</div>
						</div>
					</div>

					<div style="display:grid; gap:6px; font-size:calc(0.88rem * var(--app-text-size)); color:var(--muted);">
						<div><strong style="color:var(--ink);">Plantilla:</strong> <?php echo htmlspecialchars(isset($assignment['template_name']) ? $assignment['template_name'] : 'Plantilla', ENT_QUOTES, 'UTF-8'); ?></div>
						<div><strong style="color:var(--ink);">Fecha asignada:</strong> <?php echo htmlspecialchars(date('d/m/Y', strtotime($assignment['assigned_date'])), ENT_QUOTES, 'UTF-8'); ?></div>
						<?php if (!empty($assignment['completed_at'])) { ?>
							<div><strong style="color:var(--ink);">Completada:</strong> <?php echo htmlspecialchars(date('d/m/Y h:i A', strtotime($assignment['completed_at'])), ENT_QUOTES, 'UTF-8'); ?></div>
						<?php } ?>
						<div><strong style="color:var(--ink);">Notas:</strong> <?php echo (int) $assignment['notes_count']; ?></div>
					</div>

					<?php if (!empty($assignment['items'])) { ?>
						<div style="display:grid; gap:8px; padding:14px; border-radius:18px; background:var(--card);">
							<?php foreach ($assignment['items'] as $item) { ?>
								<div style="display:flex; align-items:center; gap:10px; color:<?php echo isset($item['status']) && $item['status'] === 'completed' ? 'var(--muted)' : 'var(--ink)'; ?>;">
									<span style="width:10px; height:10px; border-radius:999px; background:<?php echo isset($item['status']) && $item['status'] === 'completed' ? '#98a5b3' : 'var(--theme)'; ?>; display:inline-block;"></span>
									<span><?php echo htmlspecialchars($item['name'], ENT_QUOTES, 'UTF-8'); ?></span>
								</div>
							<?php } ?>
						</div>
					<?php } ?>

					<?php if (!empty($assignment['notes'])) { ?>
						<div style="display:grid; gap:8px; padding:14px; border-radius:18px; background:var(--card);">
							<div style="font-size:calc(0.85rem * var(--app-text-size)); color:var(--muted); font-weight:700;">Historial de notas</div>
							<?php foreach (array_slice($assignment['notes'], 0, 3) as $note_row) { ?>
								<div style="display:grid; gap:4px;">
									<div style="font-size:calc(0.8rem * var(--app-text-size)); color:var(--muted); font-weight:700;">
										<?php echo htmlspecialchars(isset($note_row['created_at']) ? $note_row['created_at'] : '', ENT_QUOTES, 'UTF-8'); ?>
									</div>
									<div style="font-size:calc(0.9rem * var(--app-text-size)); color:var(--ink);">
										<?php echo htmlspecialchars(isset($note_row['note_text']) ? $note_row['note_text'] : '', ENT_QUOTES, 'UTF-8'); ?>
									</div>
								</div>
							<?php } ?>
						</div>
					<?php } ?>

					<?php if (in_array($status, array('completed', 'overdue'), TRUE)) { ?>
						<div class="actions" style="justify-content:flex-end; margin-top:0;">
							<form method="post" action="<?php echo site_url('configuracion/tareas/seguimiento'); ?>" style="display:inline-flex;">
								<input type="hidden" name="action_type" value="reactivate_assignment" />
								<input type="hidden" name="assignment_id" value="<?php echo (int) $assignment['id']; ?>" />
								<input type="hidden" name="q" value="<?php echo htmlspecialchars(isset($filters['q']) ? $filters['q'] : '', ENT_QUOTES, 'UTF-8'); ?>" />
								<input type="hidden" name="employee_id" value="<?php echo (int) (isset($filters['employee_id']) ? $filters['employee_id'] : 0); ?>" />
								<input type="hidden" name="group_id" value="<?php echo (int) (isset($filters['group_id']) ? $filters['group_id'] : 0); ?>" />
								<input type="hidden" name="status" value="<?php echo htmlspecialchars(isset($filters['status']) ? $filters['status'] : 'all', ENT_QUOTES, 'UTF-8'); ?>" />
								<button class="btn btn-outline" type="submit">Reactivar tarea</button>
							</form>
						</div>
					<?php } ?>
				</div>
			<?php } ?>
		</div>
	<?php } ?>
</section>
