<?php
// paneladm.jatpluss.com/admin/admin_asignar_contrasena.php
session_start();

// 1) Sólo administradores
if (empty($_SESSION['admin_id'])) {
    header('Location: login_admin.php');
    exit;
}

// 2) Conexión y carga de plataformas
require_once __DIR__ . '/conexion.php';       // $pdo disponible

$stmt = $pdo->query("
    SELECT DISTINCT platform
    FROM email_subjects
    WHERE active = 1
    ORDER BY platform
");
$plataformasDisponibles = $stmt->fetchAll(PDO::FETCH_COLUMN);

// 3) Mensajes flash
$exito_message = $_SESSION['exito_message'] ?? '';
$error_message = $_SESSION['error_message'] ?? '';
unset($_SESSION['exito_message'], $_SESSION['error_message']);

// 4) Para “recordar” el valor elegido si hubo error:
$oldEmail       = $_SESSION['old_email']       ?? '';
$oldPassword    = $_SESSION['old_password']    ?? '';
$oldPlataforma  = $_SESSION['old_plataforma']  ?? '';
unset($_SESSION['old_email'], $_SESSION['old_password'], $_SESSION['old_plataforma']);
?>
<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="UTF-8">
  <title>Panel Admin – Asignar Contraseña</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- Bootstrap & FontAwesome -->
  <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">
  <link rel="icon" href="/images/logo/favicon.ico" type="image/x-icon">

  <style>
    body  { background:#343a40; }
    .card { background:rgba(0,0,0,.7); border:none; }
    .card .card-title, .card label { color:#fff; }
    .form-control, .form-select { background:#212529; border:1px solid #666; color:#fff; }
    .btn-primary{ background:#e50914;border:none; }
    .btn-primary:hover{ background:#f6121d; }
  </style>
</head>
<body>

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

<div class="container d-flex justify-content-center align-items-center min-vh-100">
  <div class="col-12 col-md-8 col-lg-6">
    <div class="card p-4 shadow">
      <div class="card-body">
        <h1 class="card-title text-center mb-4">
          <i class="fas fa-key"></i> Asignar/Actualizar Contraseña
        </h1>

        <!-- Mensajes -->
        <?php if ($exito_message): ?>
          <div class="alert alert-success d-flex align-items-center">
            <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">
            <i class="fas fa-exclamation-triangle me-2"></i> <?= htmlspecialchars($error_message) ?>
          </div>
        <?php endif; ?>

        <!-- Formulario -->
        <form action="admin_guardar.php" method="POST" class="mt-2">
          <!-- Email -->
          <div class="mb-3">
            <label class="form-label"><i class="fas fa-envelope"></i> Correo</label>
            <input type="email" name="email" class="form-control" placeholder="correo@ejemplo.com"
                   value="<?= htmlspecialchars($oldEmail) ?>" required>
          </div>

          <!-- Contraseña -->
          <div class="mb-3">
            <label class="form-label"><i class="fas fa-lock"></i> Contraseña</label>
            <input type="text" name="password" class="form-control" placeholder="Clave para el buzón"
                   value="<?= htmlspecialchars($oldPassword) ?>" required>
          </div>

          <!-- Plataforma -->
          <div class="mb-3">
            <label class="form-label"><i class="fas fa-tv"></i> Plataforma</label>
            <select name="plataforma" class="form-select" required>
              <option value="" disabled <?= $oldPlataforma === '' ? 'selected' : '' ?>>
                Seleccione una plataforma
              </option>
              <?php foreach ($plataformasDisponibles as $plat): ?>
                <option value="<?= htmlspecialchars($plat) ?>"
                        <?= $plat === $oldPlataforma ? 'selected' : '' ?>>
                  <?= htmlspecialchars($plat) ?>
                </option>
              <?php endforeach; ?>
            </select>
          </div>

          <!-- Guardar -->
          <button class="btn btn-primary w-100">
            <i class="fas fa-save"></i> Guardar
          </button>
        </form>
      </div>
    </div>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<?php include __DIR__ . '/footer.php'; ?>

</body>
</html>
