<?php
ob_start();
require 'config.php';
ob_clean();
header('Content-Type: application/json');

if (!isset($_SESSION['user_id'])) {
    echo json_encode(['success' => false, 'message' => 'Belum login']);
    exit;
}

try {
    // Ambil setting tunai
    $tunai = $pdo->query("SELECT value FROM payment_settings WHERE key_name = 'tunai_enabled' LIMIT 1")->fetchColumn();

    // Ambil QRIS
    $qris_enabled = $pdo->query("SELECT value FROM payment_settings WHERE key_name = 'qris_enabled' LIMIT 1")->fetchColumn();
    $qris_img     = $pdo->query("SELECT value FROM payment_settings WHERE key_name = 'qris_image' LIMIT 1")->fetchColumn();
    $qris_name    = $pdo->query("SELECT value FROM payment_settings WHERE key_name = 'qris_merchant_name' LIMIT 1")->fetchColumn();

    // Ambil daftar bank aktif
    $stmt = $pdo->query("SELECT * FROM payment_banks WHERE is_active = 1 ORDER BY sort_order ASC");
    $banks = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode([
        'success' => true,
        'tunai'   => ['enabled' => (bool)$tunai],
        'qris'    => [
            'enabled'       => (bool)$qris_enabled,
            'image'         => $qris_img ?: null,
            'merchant_name' => $qris_name ?: 'GoldenWok Resto',
        ],
        'banks'   => $banks,
    ]);
} catch (Exception $e) {
    // Jika tabel belum ada, kembalikan default semua aktif
    echo json_encode([
        'success' => true,
        'tunai'   => ['enabled' => true],
        'qris'    => ['enabled' => false, 'image' => null, 'merchant_name' => 'GoldenWok Resto'],
        'banks'   => [],
    ]);
}