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

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

    $sql = "SELECT * FROM grifos WHERE id = ?";
    $stmt = $connMySQL->prepare($sql);
    $stmt->bind_param("i", $id_grifo);
    $stmt->execute();
    $grifo = $stmt->get_result()->fetch_assoc();
    $stmt->close();

    $connMySQL->closeConnection();

    if (!$grifo) {
        header("Location: Adm_Gestion_Grifos.php");
        exit;
    }
} else {
    header("Location: Adm_Gestion_Grifos.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 Grifo</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_Grifos.php" class="btn btn-outline-primary mb-3">
      <i class="bi bi-arrow-left"></i> Volver a gestión de Grifos
    </a>

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

      <div class="card-body">
        <form id="Editar_Grifo" action="../../Controlador/Ctrl_Administrador/MySQL_Editar_Grifo.php" method="post" novalidate>
          <input type="hidden" name="id_grifo" value="<?= $grifo['id']; ?>">

          <div class="row g-3">
            <div class="col-md-6">
              <label for="nombre" class="form-label">Nombre</label>
              <input type="text" class="form-control text-uppercase" id="nombre" name="nombre"
                     value="<?= htmlspecialchars($grifo['nombre']); ?>"
                     oninput="this.value = this.value.toUpperCase();" required>
              <div class="invalid-feedback"></div>
            </div>

            <div class="col-md-6">
              <label for="ruc" class="form-label">RUC</label>
              <input type="text" class="form-control" id="ruc" name="ruc"
                     maxlength="11" value="<?= htmlspecialchars($grifo['ruc']); ?>"
                     oninput="this.value = this.value.replace(/\D/g, '');"
                     pattern="\d{11}" title="Debe contener exactamente 11 números" required>
              <div class="invalid-feedback"></div>
            </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($grifo['telefono']); ?>"
                     oninput="this.value = this.value.replace(/\D/g, '');"
                     pattern="\d{9}" title="Debe contener exactamente 9 números" required>
              <div class="invalid-feedback"></div>
            </div>

            <div class="col-md-6">
              <label for="lugar" class="form-label">Lugar</label>
              <input type="text" class="form-control text-uppercase" id="lugar" name="lugar"
                     value="<?= htmlspecialchars($grifo['lugar']); ?>"
                     oninput="this.value = this.value.toUpperCase();" required>
              <div class="invalid-feedback"></div>
            </div>

            <div class="col-12 text-center mt-4">
              <button type="submit" class="btn btn-success px-4">Guardar Cambios</button>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>

  <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_Grifo.js"></script>
  

  <?php if (isset($_SESSION['error_ruc'])): ?>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <script>
      Swal.fire({
        icon: 'error',
        title: 'Error al actualizar',
        text: '<?= $_SESSION['error_ruc'] ?>',
        confirmButtonText: 'Aceptar'
      });
    </script>
    <?php unset($_SESSION['error_ruc']); ?>
  <?php endif; ?>

</body>

</html>
