<?php
// api/endpoints/riwayat_siswa_get.php
$bulanNamaMap = [
    1=>'Januari', 2=>'Februari', 3=>'Maret',     4=>'April',
    5=>'Mei',     6=>'Juni',     7=>'Juli',       8=>'Agustus',
    9=>'September',10=>'Oktober',11=>'November',  12=>'Desember'
];

$nis = $_GET['nis'] ?? '';
if (empty($nis)) {
    respond(['status' => 'error', 'message' => 'NIS wajib diisi'], 400);
}

$stmt = $pdo->prepare("
    SELECT p.id, p.tagihan_id, p.jumlah_bayar, p.metode_bayar,
           p.no_rekening, p.keterangan, p.created_at,
           t.bulan, t.tahun, t.nominal, t.status AS status_tagihan,
           s.nama AS nama_siswa, s.nis AS nis_siswa, s.kelas, s.jurusan
    FROM pembayaran p
    JOIN tagihan_spp t ON p.tagihan_id = t.id
    JOIN siswa s       ON t.siswa_id   = s.id
    WHERE s.nis = ?
    ORDER BY p.created_at DESC
");
$stmt->execute([$nis]);
$rows = $stmt->fetchAll();

$result = [];
foreach ($rows as $r) {
    $bulanInt = (int)$r['bulan'];
    $result[] = [
        'id'            => (string)$r['id'],
        // ✅ camelCase — sama dengan riwayat_get.php
        'jumlahBayar'   => (float)$r['jumlah_bayar'],
        'metodeBayar'   => $r['metode_bayar'],
        'noRekening'    => $r['no_rekening'] ?? '',
        'keterangan'    => $r['keterangan']  ?? '',
        'tanggalBayar'  => $r['created_at'],
        'bulan'         => $bulanInt,
        'bulanNama'     => $bulanNamaMap[$bulanInt] ?? '',
        'tahun'         => (int)$r['tahun'],
        'nominal'       => (float)$r['nominal'],
        'statusTagihan' => $r['status_tagihan'],
        'namaSiswa'     => $r['nama_siswa'],
        'nisSiswa'      => $r['nis_siswa'],
        'kelas'         => $r['kelas'],
        'jurusan'       => $r['jurusan'],
        'noKwitansi'    => '',
    ];
}

respond(['status' => 'success', 'data' => $result]);