<?php
// paneladm.jatpluss.com/admin/admin_edit_correo.php
session_start();
if (empty($_SESSION['admin_id'])) { header('Location: login_admin.php'); exit; }

require_once __DIR__.'/conexion.php';      // $pdo
require_once __DIR__.'/config.php';        // $plataformasDisponibles

/* --- ID & registro --- */
if (empty($_GET['id']) || !is_numeric($_GET['id'])) {
    $_SESSION['error_message'] = 'ID inválido.';        header('Location: admin_list_correo.php'); exit;
}
$id = (int)$_GET['id'];
$stmt = $pdo->prepare("SELECT id,email,password,plataforma FROM usuarios_correos WHERE id = ? LIMIT 1");
$stmt->execute([$id]);
$correo = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$correo){
    $_SESSION['error_message']='Registro no encontrado.'; header('Location: admin_list_correo.php'); exit;
}

/* --- CSRF --- */
if (empty($_SESSION['csrf_token'])) {
    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}

/* --- mensajes flash --- */
$exito_message = $_SESSION['exito_message'] ?? '';
$error_message = $_SESSION['error_message'] ?? '';
unset($_SESSION['exito_message'], $_SESSION['error_message']);
?>
<!doctype html>
<html lang="es">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Editar correo</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" rel="stylesheet">
</head>
<body class="bg-dark text-white">

<?php include 'navbar.php'; ?>

<div class="container py-5">
  <div class="row justify-content-center">
    <div class="col-12 col-md-8 col-lg-6 col-xl-5">
      <div class="card bg-black bg-opacity-75 border-0 shadow">
        <div class="card-body p-4">

          <h1 class="h4 text-center mb-4"><i class="fas fa-edit me-2"></i>Editar correo</h1>

          <?php if($exito_message): ?>
            <div class="alert alert-success d-flex align-items-center" role="alert">
              <i class="fas fa-check-circle me-2"></i><?= htmlspecialchars($exito_message) ?>
            </div>
          <?php elseif($error_message): ?>
            <div class="alert alert-danger d-flex align-items-center" role="alert">
              <i class="fas fa-exclamation-triangle me-2"></i><?= htmlspecialchars($error_message) ?>
            </div>
          <?php endif; ?>

          <form action="admin_update_correo.php" method="POST" class="needs-validation" novalidate>
            <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
            <input type="hidden" name="id" value="<?= $correo['id'] ?>">

            <div class="mb-3">
              <label class="form-label"><i class="fas fa-envelope me-1"></i>Correo</label>
              <input type="email" name="email" class="form-control bg-dark text-white" 
                     value="<?= htmlspecialchars($correo['email']) ?>" required>
            </div>

            <div class="mb-3">
              <label class="form-label"><i class="fas fa-lock me-1"></i>Acceso</label>
              <input type="text" name="password" class="form-control bg-dark text-white"
                     value="<?= htmlspecialchars($correo['password']) ?>" required>
            </div>

            <div class="mb-4">
              <label class="form-label"><i class="fas fa-tv me-1"></i>Plataforma</label>
              <select name="plataforma" class="form-select bg-dark text-white" required>
                <option value="" disabled>Seleccione…</option>
                <?php foreach ($plataformasDisponibles as $plat): ?>
                  <option value="<?= htmlspecialchars($plat) ?>"
                    <?= $plat === $correo['plataforma'] ? 'selected' : '' ?>>
                    <?= htmlspecialchars($plat) ?>
                  </option>
                <?php endforeach; ?>
              </select>
            </div>

            <div class="d-flex justify-content-between">
              <a href="admin_list_correo.php" class="btn btn-secondary">
                <i class="fas fa-arrow-left me-1"></i>Volver
              </a>
              <button class="btn btn-danger">
                <i class="fas fa-save me-1"></i>Guardar cambios
              </button>
            </div>
          </form>

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

<?php include __DIR__.'/footer.php'; ?>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
