<?php
/**
 * ⚠️ EMERGENCY ADMIN LOGIN - BYPASS OTP ⚠️
 * HAPUS FILE INI SETELAH BERHASIL MASUK!
 * DELETE THIS FILE AFTER LOGIN!
 */
require 'RGShenn.php';

// ==========================================
// KUNCI AKSES DARURAT - GANTI JIKA PERLU
// ==========================================
$EMERGENCY_KEY = 'gclhos-emergency-2026';
// ==========================================

$error = '';
$users = [];
$logged_in = false;

// Ambil semua admin dari database untuk dipilih
if ($call) {
    $res = $call->query("SELECT id, name, username, phone, email, level FROM users WHERE level IN ('Admin','Super Admin','Owner') ORDER BY id ASC");
    if ($res && $res->num_rows > 0) {
        while ($row = $res->fetch_assoc()) {
            $users[] = $row;
        }
    }
    // Kalau tidak ada admin, ambil semua user
    if (empty($users)) {
        $res2 = $call->query("SELECT id, name, username, phone, email, level FROM users ORDER BY id ASC LIMIT 20");
        if ($res2) {
            while ($row = $res2->fetch_assoc()) {
                $users[] = $row;
            }
        }
    }
}

// Proses login darurat
if (isset($_POST['do_login'])) {
    $input_key = $_POST['emergency_key'] ?? '';
    $input_user = $_POST['username'] ?? '';

    if ($input_key !== $EMERGENCY_KEY) {
        $error = '❌ Kunci Akses Darurat salah!';
    } else if (!$input_user) {
        $error = '❌ Pilih akun terlebih dahulu!';
    } else {
        $data_user = $call->query("SELECT * FROM users WHERE username = '" . $call->real_escape_string($input_user) . "'")->fetch_assoc();
        if (!$data_user) {
            $error = '❌ User tidak ditemukan!';
        } else {
            // Set session langsung - bypass OTP dan PIN
            $_SESSION['user'] = $data_user;
            $_SESSION['last_login_time'] = time();

            // Set cookie juga
            $ShennCookie = random(86);
            $cookie_time = time() + (86400 * 7);
            setcookie('ssid', 'SHENN-A' . $data_user['id'] . 'AIY', $cookie_time, '/', $_SERVER['HTTP_HOST']);
            setcookie('token', $ShennCookie, $cookie_time, '/', $_SERVER['HTTP_HOST']);
            $call->query("INSERT INTO users_cookie VALUES ('$ShennCookie', '" . $data_user['username'] . "', '" . date('Y-m-d H:i:s') . "')");

            $logged_in = true;
        }
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>⚠️ Emergency Login</title>
    <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { font-family: Arial, sans-serif; background: #1a1a2e; min-height: 100vh; display: flex; align-items: center; justify-content: center; }
        .card { background: #fff; border-radius: 12px; padding: 30px; max-width: 460px; width: 95%; box-shadow: 0 10px 40px rgba(0,0,0,0.4); }
        .warning { background: #fff3cd; border: 2px solid #ffc107; border-radius: 8px; padding: 14px; margin-bottom: 20px; font-size: 13px; color: #856404; text-align: center; }
        h2 { color: #c0392b; text-align: center; margin-bottom: 6px; font-size: 20px; }
        .subtitle { text-align: center; color: #666; font-size: 13px; margin-bottom: 20px; }
        label { display: block; font-weight: bold; margin-bottom: 4px; font-size: 14px; color: #333; }
        input, select { width: 100%; padding: 10px 12px; border: 2px solid #ddd; border-radius: 8px; font-size: 14px; margin-bottom: 16px; outline: none; }
        input:focus, select:focus { border-color: #5A24A8; }
        button { width: 100%; padding: 12px; background: #5A24A8; color: white; border: none; border-radius: 8px; font-size: 16px; font-weight: bold; cursor: pointer; margin-top: 4px; }
        button:hover { background: #4a1a98; }
        .error { background: #f8d7da; border: 1px solid #f5c6cb; color: #721c24; padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; font-size: 14px; }
        .success-box { text-align: center; }
        .success-box .icon { font-size: 60px; margin-bottom: 10px; }
        .success-box h3 { color: #155724; margin-bottom: 10px; }
        .success-box .user-info { background: #d4edda; border-radius: 8px; padding: 14px; margin-bottom: 16px; text-align: left; font-size: 13px; }
        .success-box .user-info b { color: #155724; }
        .btn-dashboard { display: block; padding: 12px; background: #28a745; color: white; text-decoration: none; border-radius: 8px; font-size: 16px; font-weight: bold; text-align: center; margin-bottom: 10px; }
        .btn-dashboard:hover { background: #218838; }
        .btn-admin { display: block; padding: 12px; background: #5A24A8; color: white; text-decoration: none; border-radius: 8px; font-size: 16px; font-weight: bold; text-align: center; }
        .btn-admin:hover { background: #4a1a98; }
        .delete-notice { background: #f8d7da; border-radius: 8px; padding: 10px; margin-top: 14px; font-size: 12px; color: #721c24; text-align: center; }
        .user-option { font-size: 13px; }
    </style>
</head>
<body>
<div class="card">
    <?php if ($logged_in): ?>
        <div class="success-box">
            <div class="icon">✅</div>
            <h3>Login Berhasil!</h3>
            <div class="user-info">
                <b>Nama:</b> <?= htmlspecialchars($_SESSION['user']['name']) ?><br>
                <b>Username:</b> <?= htmlspecialchars($_SESSION['user']['username']) ?><br>
                <b>Level:</b> <?= htmlspecialchars($_SESSION['user']['level']) ?><br>
                <b>HP:</b> <?= htmlspecialchars($_SESSION['user']['phone']) ?><br>
                <b>Email:</b> <?= htmlspecialchars($_SESSION['user']['email']) ?>
            </div>
            <a href="<?= base_url() ?>" class="btn-dashboard">🏠 Ke Dashboard User</a>
            <br>
            <a href="<?= base_url('admin') ?>" class="btn-admin">⚙️ Ke Panel Admin</a>
            <div class="delete-notice">
                ⚠️ <b>PENTING:</b> Setelah masuk, segera <b>HAPUS file</b> <code>emergency-login.php</code> dari hosting untuk keamanan!
            </div>
        </div>
    <?php else: ?>
        <div class="warning">
            ⚠️ <b>HALAMAN DARURAT</b> — Hanya untuk admin. Hapus file ini setelah digunakan!
        </div>
        <h2>🔑 Emergency Login</h2>
        <p class="subtitle">Bypass OTP — Khusus saat API WA/Email belum dikonfigurasi</p>

        <?php if ($error): ?>
            <div class="error"><?= $error ?></div>
        <?php endif; ?>

        <form method="POST">
            <label>Pilih Akun (<?= count($users) ?> ditemukan):</label>
            <select name="username" required>
                <option value="">-- Pilih Akun --</option>
                <?php foreach ($users as $u): ?>
                    <option value="<?= htmlspecialchars($u['username']) ?>" class="user-option">
                        [<?= htmlspecialchars($u['level']) ?>] <?= htmlspecialchars($u['name']) ?> — <?= htmlspecialchars($u['phone']) ?>
                    </option>
                <?php endforeach; ?>
            </select>

            <label>Kunci Akses Darurat:</label>
            <input type="password" name="emergency_key" placeholder="Masukkan kunci akses darurat" required>

            <button type="submit" name="do_login">🔓 Login Darurat</button>
        </form>

        <?php if (empty($users)): ?>
            <div class="error" style="margin-top:12px;">
                ⚠️ Tidak ada user ditemukan di database. Pastikan database sudah diimport dengan benar.
            </div>
        <?php endif; ?>
    <?php endif; ?>
</div>
</body>
</html>
