<?php
// laporan_test.php - Test laporan langsung

$pdo = new PDO('mysql:host=localhost;dbname=platform_kapal_samosir;charset=utf8mb4', 'root', '');

echo "<h1>TEST LAPORAN</h1>";

// Ambil data
$stmt = $pdo->query("
    SELECT 
        k.nama_kapal,
        COUNT(p.id) as total_pesanan,
        COALESCE(SUM(p.total_harga), 0) as total_pendapatan,
        COALESCE(SUM(p.komisi_admin), 0) as total_komisi,
        COALESCE(SUM(p.nominal_diterima_supir), 0) as total_diterima_supir
    FROM kapal k
    LEFT JOIN pesanan p ON k.id = p.kapal_id AND p.status_pesanan IN ('confirmed', 'completed')
    WHERE k.status_verifikasi = 'verified'
    GROUP BY k.id, k.nama_kapal
");
$data = $stmt->fetchAll();

echo "<table border='1' cellpadding='10'>";
echo "<tr><th>Kapal</th><th>Pesanan</th><th>Pendapatan</th><th>Komisi</th><th>Diterima Supir</th></tr>";
foreach ($data as $row) {
    echo "<tr>";
    echo "<td>" . $row['nama_kapal'] . "</td>";
    echo "<td>" . $row['total_pesanan'] . "</td>";
    echo "<td>Rp " . number_format($row['total_pendapatan'], 0, ',', '.') . "</td>";
    echo "<td>Rp " . number_format($row['total_komisi'], 0, ',', '.') . "</td>";
    echo "<td>Rp " . number_format($row['total_diterima_supir'], 0, ',', '.') . "</td>";
    echo "</tr>";
}
echo "</table>";

// Total
$stmt = $pdo->query("SELECT COALESCE(SUM(total_harga), 0) as total FROM pesanan WHERE status_pesanan IN ('confirmed', 'completed')");
$total = $stmt->fetch(PDO::FETCH_ASSOC);
echo "<h3>Total Pendapatan: Rp " . number_format($total['total'], 0, ',', '.') . "</h3>";
?>
