<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<?php $rules = isset($rules) && is_array($rules) ? $rules : array(); ?>
<style>
	.management-page {
		display: grid;
		gap: 16px;
	}
	.management-shell {
		display: grid;
		gap: 14px;
		padding: 18px;
		border-radius: 22px;
		background: var(--panel);
		box-shadow: var(--shadow);
	}
	.management-head {
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
		gap: 12px;
	}
	.management-head h2 {
		margin: 0;
	}
	.management-head p {
		margin: 6px 0 0;
		color: var(--muted);
	}
	.management-list {
		display: grid;
		gap: 10px;
	}
	.management-row {
		display: grid;
		grid-template-columns: minmax(0, 1fr) auto;
		gap: 12px;
		align-items: center;
		padding: 14px;
		border-radius: 18px;
		background: var(--card);
		box-shadow: inset 0 0 0 1px rgba(20, 41, 64, 0.08);
	}
	.management-row strong,
	.management-row span {
		display: block;
	}
	.management-row strong {
		color: var(--ink);
	}
	.management-row span {
		margin-top: 4px;
		color: var(--muted);
		font-size: 0.92rem;
	}
	.management-empty {
		padding: 18px;
		border-radius: 18px;
		background: var(--card);
		box-shadow: inset 0 0 0 1px rgba(20, 41, 64, 0.08);
		color: var(--muted);
		text-align: center;
	}
	.management-actions {
		display: flex;
		align-items: center;
		gap: 10px;
		flex-wrap: wrap;
		justify-content: flex-end;
	}
	@media (max-width: 760px) {
		.management-row {
			grid-template-columns: 1fr;
		}
		.management-head {
			grid-template-columns: 1fr;
			display: grid;
		}
	}
</style>

<div class="management-page">
	<div class="actions" style="margin-bottom:0;">
		<a class="btn btn-outline" href="<?php echo site_url('tareas'); ?>">Volver</a>
		<a class="btn" href="<?php echo site_url('tareas/nueva'); ?>">Agregar</a>
	</div>

	<section class="management-shell">
		<div class="management-head">
			<div>
				<h2>Tareas configuradas</h2>
				<p>Aqui ves la lista completa de reglas para editar destino, recurrencia y configuracion.</p>
			</div>
		</div>

		<?php if (empty($rules)) { ?>
			<div class="management-empty">Todavia no hay tareas configuradas.</div>
		<?php } else { ?>
			<div class="management-list">
				<?php foreach ($rules as $rule) { ?>
					<div class="management-row">
						<div>
							<strong><?php echo htmlspecialchars(!empty($rule['display_name']) ? (string) $rule['display_name'] : (!empty($rule['title']) ? (string) $rule['title'] : 'Tarea'), ENT_QUOTES, 'UTF-8'); ?></strong>
							<span><?php echo htmlspecialchars((string) $rule['target_label'].' - '.(string) $rule['recurrence_label'].' - Proxima: '.(string) $rule['next_due_date_label'], ENT_QUOTES, 'UTF-8'); ?></span>
						</div>
						<div class="management-actions">
							<a class="btn btn-outline" href="<?php echo site_url('tareas/reglas/'.(int) $rule['id']); ?>">Editar</a>
							<form method="post" action="<?php echo site_url('tareas/reglas/'.(int) $rule['id'].'/eliminar'); ?>" onsubmit="return confirm('Estas seguro de eliminar esta tarea?');">
								<button class="btn" type="submit">Eliminar</button>
							</form>
						</div>
					</div>
				<?php } ?>
			</div>
		<?php } ?>
	</section>
</div>
