<?php
require_once '../config/db.php';
header('Content-Type: application/json');
try {
    // Lihat nilai asli metode_bayar + 10 transaksi terbaru
    $st = $pdo->query(
        "SELECT p.id, p.metode_bayar, p.keterangan, p.jumlah_bayar,
                p.created_at, s.nama
         FROM pembayaran p
         JOIN siswa s ON p.siswa_id = s.id
         ORDER BY p.id DESC LIMIT 10"
    );
    $rows = $st->fetchAll(PDO::FETCH_ASSOC);
    
    $st2 = $pdo->query("SELECT DISTINCT metode_bayar, COUNT(*) as n FROM pembayaran GROUP BY metode_bayar");
    $unik = $st2->fetchAll(PDO::FETCH_ASSOC);
    
    echo json_encode([
        'metode_unik' => $unik,
        'transaksi_terbaru' => $rows
    ], JSON_PRETTY_PRINT);
} catch (Exception $e) {
    echo json_encode(['error' => $e->getMessage()]);
}