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

if (empty($_SESSION['admin_id'])) {
    header('Location: login_admin.php');
    exit;
}

/* -------- BD y plataformas -------- */
require_once __DIR__ . '/conexion.php';

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

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

/* -------- Recordar datos si hubo error -------- */
$temp_emails     = $_SESSION['temp_emails']     ?? '';
$old_plataforma  = $_SESSION['old_plataforma']  ?? '';
unset($_SESSION['temp_emails'], $_SESSION['old_plataforma']);

/* -------- CSRF -------- */
if (empty($_SESSION['csrf_token'])) {
    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
$csrf = $_SESSION['csrf_token'];
?>
<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="UTF-8">
  <title>Panel Admin – Asignar Correos Masivamente</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <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;color:#fff}
    .card{background:rgba(0,0,0,.7);border:none}
    .card .card-title,.card label{color:#fff}
    .form-control,.form-select,textarea{background:#212529;border:1px solid #666;color:#fff}
    .btn-primary{background:#e50914;border:none}.btn-primary:hover{background:#f6121d}
    .btn-danger{background:#dc3545;border:none}.btn-danger:hover{background:#c82333}
    .btn-secondary{background:#6c757d;border:none}.btn-secondary:hover{background:#5a6268}
  </style>
</head>
<body>

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

<div class="container my-4">
  <div class="card p-4 shadow">
    <h1 class="card-title text-center mb-4">
      <i class="fas fa-users"></i> Asignar Correos Masivamente
    </h1>

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

    <form action="admin_guardar_masivo.php" method="POST">
      <input type="hidden" name="csrf_token" value="<?= $csrf ?>">

      <!-- Emails -->
      <div class="mb-3">
        <label class="form-label"><i class="fas fa-envelope"></i> Correos Electrónicos</label>
        <textarea name="emails" rows="10" class="form-control"
                  placeholder="Uno por línea" required><?= htmlspecialchars($temp_emails) ?></textarea>
      </div>

      <!-- Código -->
      <div class="mb-3">
        <label class="form-label"><i class="fas fa-lock"></i> Código de Acceso</label>
        <input type="text" name="access_code" class="form-control" 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 <?= $old_plataforma==='' ? 'selected':'' ?>>Seleccione…</option>
          <?php foreach ($plataformasDisponibles as $plat): ?>
            <option value="<?= htmlspecialchars($plat) ?>"
                    <?= $plat===$old_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"></i> Volver
        </a>
        <button class="btn btn-primary">
          <i class="fas fa-save"></i> Asignar Masivamente
        </button>
      </div>
    </form>
  </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>
