<?php
// ======================================================
// KONEKSI DATABASE & KONFIGURASI API
// ======================================================
include '../function/connect.php';
include '../play/game/config.php';

// Ambil 10 notifikasi terbaru
$queryNotif = mysqli_query($koneksi, "SELECT judul, deskripsi, tanggal FROM tb_pemberitahuan ORDER BY tanggal DESC LIMIT 10");

$notifikasi = [];
while ($row = mysqli_fetch_assoc($queryNotif)) {
    $notifikasi[] = $row;
}

// Ambil 10 notifikasi terbaru
$queryNotif = mysqli_query($koneksi, "SELECT judul, deskripsi, tanggal FROM tb_pemberitahuan ORDER BY tanggal DESC LIMIT 10");

$notifikasi = [];
while ($row = mysqli_fetch_assoc($queryNotif)) {
    $notifikasi[] = $row;
}

$notifCount = count($notifikasi);

// ======================================================
// AMBIL DATA SESSION USER
// ======================================================
$id_login  = $_SESSION['id'];        // ID user yang sedang login
$extplayer = $_SESSION['extplayer']; // ID player external

// ======================================================
// CEK STATUS SALDO USER
// ======================================================
$query1 = mysqli_query(
    $koneksi,
    "SELECT active FROM tb_saldo WHERE id_user = '$extplayer'"
);
$liat = mysqli_fetch_array($query1);

// ======================================================
// AMBIL DATA USER LOGIN
// ======================================================
$query2 = mysqli_query(
    $koneksi,
    "SELECT * FROM tb_user WHERE id = '$id_login'"
);
$punya_user = mysqli_fetch_array($query2);

// ======================================================
// CEK STATUS USER (SUSPEND)
// ======================================================
if ($punya_user['status'] == "Suspend") {
    session_destroy();                    // Hapus session user
    header("Location:index.php?pesan=29"); // Redirect ke index
}


// ======================================================
// AMBIL DATA BANK USER
// ======================================================
$query3 = mysqli_query(
    $koneksi,
    "SELECT * FROM tb_bank WHERE id_user = '$extplayer'"
);
$bank_user = mysqli_fetch_array($query3);

// ======================================================
// AMBIL DATA BANK USER (DUPLIKAT SESUAI STRUKTUR ASLI)
// ======================================================
$query3 = mysqli_query(
    $koneksi,
    "SELECT * FROM tb_bank WHERE id_user = '$extplayer'"
);
$b = mysqli_fetch_array($query3);

// ======================================================
// DATA KONTAK (WHATSAPP & LIVECHAT)
// ======================================================
$query1010 = mysqli_query($koneksi, "SELECT * FROM tb_contact");
$ssa = mysqli_fetch_array($query1010);

$whatsapp    = $ssa['no_whatsapp']; // Nomor WhatsApp CS
$id_livechat = $ssa['id_livechat']; // ID livechat
$facebok = $ssa['facebok']; // ID livechat
$telegram = $ssa['telegram']; // ID livechat

// ======================================================
// DATA WEBSITE
// ======================================================
$cuk = mysqli_query($koneksi, "SELECT * FROM tb_web");
$cek_web = mysqli_fetch_array($cuk);

$urlweb   = $cek_web['url'];        // URL website
$logo     = $cek_web['logo'];       // Logo website
$warna    = $cek_web['warna'];      // Warna tema
$min_depo = $cek_web['min_depo'];   // Minimal deposit
$min_wd   = $cek_web['min_wd'];     // Minimal withdraw
$icon     = $cek_web['icon_web'];   // Icon web
$title    = $cek_web['title'];      // Title website
$deskripsi = $cek_web['deskripsi']; // Meta description
$keyword   = $cek_web['keyword'];   // Meta keyword

// ======================================================
// PISAH JUDUL WEBSITE (SEBELUM TANDA "|")
// ======================================================
$pisah = explode('|', $title);
$judul = trim($pisah[0]);

/* =========================
   MAINTENANCE MODE
========================= */
// Wajib ada kolom: maintenance (0 / 1)
$maintenance = $web['maintenance'] ?? 0;

if ($maintenance == 1) {
    http_response_code(503); // SEO Friendly
    include 'error_page/maintenance.php'; // halaman luxury
    exit;
}

?>