<?php
session_start();
require_once '../../Controlador/Utilidades/Conexion_BD.php';

if (isset($_GET['IC']) && !empty($_GET['IC'])) {
    $id_chofer = intval($_GET['IC']);
    $connMySQL = new Conexion_BD();

    $sql = "SELECT * FROM choferes WHERE id=?";
    $stmt = $connMySQL->prepare($sql);
    $stmt->bind_param("i", $id_chofer);
    $stmt->execute();
    $chofer = $stmt->get_result()->fetch_assoc();
    $stmt->close();
    $connMySQL->closeConnection();

    if (!$chofer) {
        header("Location: Adm_Gestion_Choferes.php");
        exit;
    }
} else {
    header("Location: Adm_Gestion_Choferes.php");
    exit;
}
?>

<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>TSGJ - Editar Conductores</title>
  <link rel="icon" href="../../Modelo/Archivos_Media/Color_LogoTipo.png" type="image/png" />
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" />
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" />
</head>
<body>
  <div class="container my-5">
    <a href="Adm_Gestion_Choferes.php" class="btn btn-outline-primary mb-3">
      <i class="bi bi-arrow-left"></i> Volver a gestión de Conductores
    </a>

    <div class="card shadow">
      <div class="card-header bg-primary text-white text-center">
        <h4>Editar Datos del Conductor</h4>
      </div>

      <div class="card-body">
        <form id="Editar_Chofer" action="../../Controlador/Ctrl_Administrador/MySQL_Editar_Chofer.php" method="post" novalidate>
          <input type="hidden" name="id_chofer" value="<?= $chofer['id']; ?>">

          <div class="row g-3">
            <div class="col-md-6">
              <label for="nombres" class="form-label">Nombres</label>
              <input type="text" class="form-control text-uppercase" id="nombres" name="nombres"
                value="<?= htmlspecialchars($chofer['nombres']); ?>"
                oninput="this.value = this.value.replace(/[^A-ZÁÉÍÓÚÑ ]/gi, '').toUpperCase();">
            </div>

            <div class="col-md-6">
              <label for="apellidos" class="form-label">Apellidos</label>
              <input type="text" class="form-control text-uppercase" id="apellidos" name="apellidos"
                value="<?= htmlspecialchars($chofer['apellidos']); ?>"
                oninput="this.value = this.value.replace(/[^A-ZÁÉÍÓÚÑ ]/gi, '').toUpperCase();">
            </div>

            <div class="col-md-6">
              <label for="dni" class="form-label">DNI</label>
              <input type="text" class="form-control" id="dni" name="dni" maxlength="8"
                value="<?= htmlspecialchars($chofer['dni']); ?>"
                oninput="this.value = this.value.replace(/\D/g,'');">
            </div>

            <div class="col-md-6">
              <label for="categoria_licencia" class="form-label">Categoría Licencia</label>
              <input type="text" class="form-control text-uppercase" id="categoria_licencia" name="categoria_licencia"
                value="<?= htmlspecialchars($chofer['categoria_licencia']); ?>"
                oninput="this.value = this.value.toUpperCase();">
            </div>

            <div class="col-md-6">
              <label for="telefono" class="form-label">Teléfono</label>
              <input type="text" class="form-control" id="telefono" name="telefono" maxlength="9"
                value="<?= htmlspecialchars($chofer['telefono']); ?>"
                oninput="this.value = this.value.replace(/\D/g,'');">
            </div>

            <div class="col-12 text-center mt-4">
              <div class="d-flex justify-content-center gap-3 flex-wrap">
                <button type="submit" class="btn btn-success px-4">Guardar Cambios</button>
              </div>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>

  <!-- Scripts -->
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
  <script src="../../Modelo/Archivos_JS/Validacion_Editar_Chofer.js"></script>

  <?php if (isset($_SESSION['error_dni'])): ?>
    <script>
      Swal.fire({
        icon: 'error',
        title: 'Error al actualizar',
        text: <?= json_encode($_SESSION['error_dni']) ?>,
        confirmButtonText: 'Aceptar'
      });
    </script>
    <?php unset($_SESSION['error_dni']); ?>
  <?php endif; ?>
</body>
</html>
