<?php
header('Content-Type: application/json; charset=utf-8');
require '../config/koneksi.php';

$response = array();
$data = array(
    "pesanan_pending" => "0",
    "antrean_aktif" => "0",
    "total_pendapatan" => "0",
    "foto_tertunda" => "0"
);

// 1. Hitung Pesanan Menunggu (Status = Menunggu Pembayaran)
// ASUMSI NAMA TABEL: trx_reservasi, KOLOM STATUS: status_tx
$query_pesanan = $conn->query("SELECT COUNT(*) as total FROM trx_reservasi WHERE status_tx = 'Menunggu Pembayaran'");
if($query_pesanan && $row = $query_pesanan->fetch_assoc()) {
    $data['pesanan_pending'] = $row['total'];
}

// 2. Hitung Pendapatan (Status = Lunas / Selesai)
$query_pendapatan = $conn->query("SELECT SUM(total_harga) as total_uang FROM trx_reservasi WHERE status_tx IN ('Lunas', 'Selesai')");
if($query_pendapatan && $row = $query_pendapatan->fetch_assoc()) {
    // Format agar lebih rapi (misal: 2.8M atau pakai number_format)
    $total = $row['total_uang'] ? $row['total_uang'] : 0;
    if ($total >= 1000000) {
        $data['total_pendapatan'] = round($total / 1000000, 1) . 'M';
    } else if ($total >= 1000) {
        $data['total_pendapatan'] = round($total / 1000, 1) . 'K';
    } else {
        $data['total_pendapatan'] = $total;
    }
}

// 3. Hitung Antrean Aktif (Status = Menunggu Sesi / Proses Foto)
// ASUMSI NAMA TABEL: trx_antrean, KOLOM STATUS: status_antrean
$query_antrean = $conn->query("SELECT COUNT(*) as total FROM trx_antrean WHERE status_antrean IN ('Menunggu Sesi', 'Proses Foto')");
if($query_antrean && $row = $query_antrean->fetch_assoc()) {
    $data['antrean_aktif'] = $row['total'];
}

// 4. Hitung Foto Tertunda (Status = Menunggu File)
$query_distribusi = $conn->query("SELECT COUNT(*) as total FROM trx_reservasi WHERE status_tx = 'Menunggu File'");
if($query_distribusi && $row = $query_distribusi->fetch_assoc()) {
    $data['foto_tertunda'] = $row['total'];
}

$response = array(
    "status" => "sukses",
    "data" => $data
);

echo json_encode($response);
$conn->close();
?>