<?php
include 'koneksi.php';
session_start();

$nama = $_SESSION['user']['nama_asli'] ?? '';
$tanggal = $_GET['tanggal'] ?? '';

$response = ['success' => false];

if ($nama && $tanggal) {
    $query = "
        SELECT 
            SUM(orderan) AS orderan, 
            SUM(total_ongkir) AS total_ongkir, 
            SUM(potongan) AS potongan, 
            SUM(tabungan) AS tabungan, 
            SUM(total_setoran) AS total_setoran, 
            SUM(total_pendapatan) AS total_pendapatan 
        FROM transaksi 
        WHERE nama = '$nama' AND tanggal = '$tanggal'
    ";

    $q = mysqli_query($koneksi, $query);

    if (!$q) {
        $response['error'] = 'Query error: ' . mysqli_error($koneksi);
    } elseif ($data = mysqli_fetch_assoc($q)) {
        $response = array_merge(['success' => true], $data);
    } else {
        $response['message'] = 'Data tidak ditemukan.';
    }
} else {
    $response['message'] = 'Nama atau tanggal tidak tersedia.';
}

echo json_encode($response);
