<?php

if (!isset($_SESSION['user_id'])) {
    $csrf_token = bin2hex(random_bytes(32));
    $_SESSION['csrf_token'] = $csrf_token;
}

$info_website = "SELECT * FROM configuration";
$stmt = $db->prepare($info_website);
$stmt->execute();
$config = $stmt->fetch(PDO::FETCH_ASSOC);

if ($config['maintenance'] == 0) {
    header("Location: /maintenance");
}

$gambar_slide = "SELECT * FROM banner WHERE status = 1 ORDER BY id ASC";
$stmt = $db->prepare($gambar_slide);
$stmt->execute();
$banner = $stmt->fetchAll(PDO::FETCH_ASSOC);

$promosi = "SELECT * FROM promotion WHERE status = 1 ORDER BY id ASC";
$stmt = $db->prepare($promosi);
$stmt->execute();
$promotion = $stmt->fetchAll(PDO::FETCH_ASSOC);

if (!empty($_SESSION['user_id'])) {
    $user_id = $_SESSION['user_id'];

    $info_pengguna = "SELECT u.*, b1.*, b2.* 
                      FROM user u 
                      LEFT JOIN bank_user b1 ON u.id = b1.user_id 
                      LEFT JOIN balance b2 ON u.id = b2.user_id 
                      WHERE u.id = :user_id 
                      LIMIT 1";
    $stmt = $db->prepare($info_pengguna);
    $stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
    $stmt->execute();
    $user = $stmt->fetch(PDO::FETCH_ASSOC);
    $extplayer = $user['extplayer'];
    $total_saldo = $user['total_balance'];
    $total_putaran = $user['total_turnover'];
    $total_win = $user['total_win'];

    $pemberitahuan = "SELECT COUNT(*) AS total FROM transaction WHERE type IN ('deposit', 'withdraw') AND user_id = :user_id AND is_read = 0";
    $stmt = $db->prepare($pemberitahuan);
    $stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
    $stmt->execute();
    $pesan = $stmt->fetchColumn();
}

$currentPage = basename($_SERVER['PHP_SELF']);
$PaymentType = '';
$type = '';

switch (strtolower($currentPage)) {
    case 'bank.php':
        $PaymentType = 'BANK';
        break;
    case 'ewallet.php':
        $PaymentType = 'EWALLET';
        break;
    case 'qris.php':
        $PaymentType = 'QRIS';
        break;
    case 'deposit.php':
        $type = 'deposit';
        break;
    case 'withdrawal.php':
        $type = 'withdraw';
        break;
    default:
        $PaymentType = 'BANK';
        break;
}

$info_bank_admin = "SELECT id, BankName, BankNumber, BankHolder, QR FROM bank_admin WHERE PaymentType = :PaymentType AND status = 1 ORDER BY id ASC";
$stmt = $db->prepare($info_bank_admin);
$stmt->bindParam(':PaymentType', $PaymentType, PDO::PARAM_STR);
$stmt->execute();
$bankAdmin = $stmt->fetchAll(PDO::FETCH_ASSOC);

$info_transaksi = "SELECT id, trxid, Amount, FromAccount, ToAccount, created_at, updated_at, status FROM transaction WHERE user_id = :user_id AND type = :type ORDER BY updated_at DESC LIMIT 10";
$stmt = $db->prepare($info_transaksi);
$stmt->bindParam(':type', $type, PDO::PARAM_STR);
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$stmt->execute();
$transaction = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="id">
<head>
    
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title><?= $config['title']; ?> : <?= $config['description']; ?></title>

    <meta content="<?= $config['title']; ?> <?= $config['seo']; ?>" name="description" />
    <meta content="<?= $config['keyword']; ?>" name="keywords" />
    <meta content="id_ID" property="og:locale" />
    <meta content="website" property="og:type" />
    <meta content="<?= $config['canonical']; ?>" property="og:url" />
    <meta content="<?= $config['title']; ?>" property="og:site_name" />
    <meta content="<?= $config['title']; ?> | <?= $config['description']; ?>" property="og:title" />
    <meta content="<?= $config['title']; ?> <?= $config['seo']; ?>" property="og:description" />
    <meta content="<?php echo 'https://' . $_SERVER['HTTP_HOST'] . '/' . $config['logo']; ?>" property="og:image" />
    <meta content="DarkGold" name="theme-color" />
    <meta content="id-ID" name="language" />
    <meta content="id" name="geo.region" />
    <meta content="jakarta" name="geo.placename" />
    <meta content="website" name="categories" />
    <!--<meta content="sXx61yGAmCIvIvkCHkKuXIgfYfElsZCYN64CmhPSiGM" name="google-site-verification" />-->
    

    <link rel="preload" href="/fonts/glyphicons-halflings-regular.woff" as="font" type="font/woff" crossorigin>
    <link rel="preload" href="/fonts/Lato-Regular.woff2" as="font" type="font/woff2" crossorigin>
    <link rel="preload" href="/fonts/lato-bold.woff2" as="font" type="font/woff2" crossorigin>
    <link rel="preload" href="/fonts/Open24DisplaySt.woff2" as="font" type="font/woff2" crossorigin>

    <link href="/<?= $config['favicon']; ?>" rel="icon" type="image/x-icon" />
    <link href="<?= $config['canonical']; ?>" rel="canonical" />
    <link rel="amphtml" href="<?= $config['amp']; ?>" />
    <link href="/Content/reserve-css" rel="stylesheet"/>
