<?php require __DIR__ . '/../../includes/navbar.php'; ?>

<div class="container my-5">

    <div class="row justify-content-center">

        <div class="col-xl-8 col-lg-9 col-md-10">

            <div class="card card-rounded card-shadow border-0">

                <!-- HEADER -->

                <div class="card-header header-dark px-4 py-3">

                    <div class="d-flex justify-content-between align-items-center flex-wrap gap-3">
                        <h4 class="mb-0 title-shadow">
                            <i class="fas fa-user-plus me-2"></i>
                            Crear Usuario
                        </h4>

                        <a href="?opt=usuarios" class="btn btn-soft-secondary text-white">
                            <i class="fas fa-arrow-left me-1"></i> Volver
                        </a>
                    </div>
                </div>

                <!-- BODY -->

                <div class="card-body p-4 bg-white">

                    <form id="form-crear-usuario" method="POST" action="?opt=crear_usuario" novalidate>

                        <!-- CAMPO OCULTO (para verificación de token) -->

                        <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">

                        <div class="row g-3">

                            <!-- NOMBRE -->

                            <div class="col-md-6">
                                <label class="form-label">Nombre</label>
                                <input type="text" name="nombre" class="form-control">
                            </div>

                            <!-- APELLIDO -->

                            <div class="col-md-6">
                                <label class="form-label">Apellido</label>
                                <input type="text" name="apellido" class="form-control">
                            </div>

                            <!-- EMAIL -->

                            <div class="col-md-6">
                                <label class="form-label">Email</label>
                                <input type="email" name="email" class="form-control">
                            </div>

                            <!-- TELÉFONO -->

                            <div class="col-md-6">
                                <label class="form-label">Teléfono</label>
                                <input type="text" name="telefono" class="form-control">
                            </div>

                            <!-- USUARIO -->

                            <div class="col-md-6">
                                <label class="form-label">Usuario</label>
                                <input type="text" name="usuario" class="form-control">
                            </div>

                            <!-- PASSWORD -->

                            <div class="col-md-6">
                                <label class="form-label">Contraseña</label>
                                <input type="password" name="password" class="form-control">
                            </div>

                            <!-- ROL -->

                            <div class="col-md-6">
                                <label class="form-label">Rol</label>
                                <select name="rol" class="form-select">
                                    <option value="operador">Operador</option>
                                    <option value="admin">Administrador</option>
                                </select>
                            </div>
                        </div>

                        <!-- BOTON SUBMIT -->

                        <div class="d-flex justify-content-between mt-4 pt-3 border-top">

                            <button type="submit" class="btn btn-success">
                                <i class="fas fa-user-plus me-1"></i> Crear Usuario
                            </button>
                        </div>
                    </form>

                    <!-- Errores provinientes de usuarioController -->

                    <!-- ERROR -->

                    <script>
                        const serverErrors = <?php 
                            if (!empty($_SESSION['flash_error'])) {
                                $errorsArray = explode('<br>', $_SESSION['flash_error']);
                                unset($_SESSION['flash_error']);
                                echo json_encode($errorsArray);
                            } else {
                                echo '[]';
                            }
                        ?>;
                    </script>

                    <!-- SUCCES -->

                    <script>
                        const serverSuccess = <?php
                            if (!empty($_SESSION['flash_success'])) {
                                $successArray = explode('<br>', $_SESSION['flash_success']);
                                unset($_SESSION['flash_success']);
                                echo json_encode($successArray);
                            } else {
                                echo '[]';
                            }
                        ?>;
                    </script>

                </div>
            </div>
        </div>
    </div>
</div>

<!-- Validar mediante js -->

<script src="js/usuarios/usuarioValidator.js"></script>