<?php
session_start();
if (!isset($_SESSION["user_id"])) {
    header("Location: auth/login.php");
    exit();
}
require_once 'includes/header.php';

$pdo = db();

// Fetch all cities ordered by the 'orden' column
$stmt = $pdo->prepare("SELECT * FROM ciudades ORDER BY orden ASC");
$stmt->execute();
$ciudades = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="container-fluid">

    <!-- Page Heading -->
    <div class="d-sm-flex justify-content-between align-items-center mb-4">
        <h3 class="text-dark mb-0">Gestión de Ciudades</h3>
        <a class="btn btn-primary btn-sm" role="button" href="agregar_ciudad.php">
            <i class="fas fa-plus fa-sm text-white-50"></i>&nbsp;Agregar Ciudad
        </a>
    </div>

    <!-- Success/Error Messages -->
    <?php if (isset($_SESSION['success_message'])): ?>
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <?php echo $_SESSION['success_message']; ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        <?php unset($_SESSION['success_message']); ?>
    <?php endif; ?>
    <?php if (isset($_SESSION['error_message'])): ?>
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            <?php echo $_SESSION['error_message']; ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        <?php unset($_SESSION['error_message']); ?>
    <?php endif; ?>

    <div class="card shadow">
        <div class="card-header py-3 d-flex justify-content-between align-items-center">
            <p class="text-primary m-0 fw-bold">Listado y Orden de Ciudades</p>
        </div>
        <div class="card-body">
            <form action="handle_orden_ciudades.php" method="post">
                <div class="table-responsive">
                    <table class="table table-bordered" id="dataTable">
                        <thead>
                            <tr>
                                <th style="width: 100px;">Orden</th>
                                <th>Nombre</th>
                                <th>Acciones</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (count($ciudades) > 0): ?>
                                <?php foreach ($ciudades as $ciudad): ?>
                                    <tr>
                                        <td>
                                            <input type="number" name="orden[<?php echo $ciudad['id']; ?>]" value="<?php echo htmlspecialchars($ciudad['orden']); ?>" class="form-control form-control-sm text-center" style="width: 80px;">
                                        </td>
                                        <td><?php echo htmlspecialchars($ciudad['nombre']); ?></td>
                                        <td>
                                            <a href="editar_ciudad.php?id=<?php echo $ciudad['id']; ?>" class="btn btn-warning btn-sm"><i class="fas fa-edit"></i> Editar</a>
                                            <a href="eliminar_ciudad.php?id=<?php echo $ciudad['id']; ?>" class="btn btn-danger btn-sm" onclick="return confirm('¿Estás seguro de que quieres eliminar esta ciudad?');"><i class="fas fa-trash"></i> Eliminar</a>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="3" class="text-center">No hay ciudades registradas.</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
                <div class="text-end mt-3">
                    <button type="submit" class="btn btn-success"><i class="fas fa-save"></i> Guardar Orden</button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php require_once 'includes/footer.php'; ?>

