<?php
// cek_laporan.php - Cek data untuk laporan

$pdo = new PDO('mysql:host=localhost;dbname=platform_kapal_samosir;charset=utf8mb4', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

echo "========================================\n";
echo "   CEK DATA LAPORAN\n";
echo "========================================\n\n";

// 1. CEK PESANAN ID 1
echo "1. DATA PESANAN ID 1:\n";
$stmt = $pdo->query("SELECT id, kode_pesanan, total_harga, komisi_admin, nominal_diterima_supir, status_pencairan, status_pesanan, tanggal_pencairan FROM pesanan WHERE id = 1");
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo "   ID: {$row['id']}\n";
echo "   Kode: {$row['kode_pesanan']}\n";
echo "   Total: Rp " . number_format($row['total_harga'], 0, ',', '.') . "\n";
echo "   Komisi: Rp " . number_format($row['komisi_admin'], 0, ',', '.') . "\n";
echo "   Diterima Supir: Rp " . number_format($row['nominal_diterima_supir'], 0, ',', '.') . "\n";
echo "   Status Pencairan: {$row['status_pencairan']}\n";
echo "   Status Pesanan: {$row['status_pesanan']}\n";
echo "   Tgl Pencairan: " . ($row['tanggal_pencairan'] ?? 'NULL') . "\n\n";

// 2. CEK SEMUA PESANAN
echo "2. SEMUA PESANAN:\n";
$stmt = $pdo->query("SELECT id, kode_pesanan, total_harga, komisi_admin, nominal_diterima_supir, status_pencairan, status_pesanan FROM pesanan ORDER BY id DESC LIMIT 10");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo "   ID: {$row['id']} | Kode: {$row['kode_pesanan']} | Status: {$row['status_pesanan']} | Pencairan: {$row['status_pencairan']} | Diterima: Rp " . number_format($row['nominal_diterima_supir'] ?? 0, 0, ',', '.') . "\n";
}
echo "\n";

// 3. CEK TOTAL DANA DICAIRKAN
echo "3. TOTAL DANA DICAIRKAN:\n";
$stmt = $pdo->query("SELECT COALESCE(SUM(nominal_diterima_supir), 0) as total FROM pesanan WHERE status_pencairan = 'cair'");
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo "   Total: Rp " . number_format($row['total'], 0, ',', '.') . "\n\n";

// 4. CEK TOTAL KOMISI
echo "4. TOTAL KOMISI ADMIN:\n";
$stmt = $pdo->query("SELECT COALESCE(SUM(komisi_admin), 0) as total FROM pesanan WHERE status_pencairan = 'cair'");
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo "   Total: Rp " . number_format($row['total'], 0, ',', '.') . "\n\n";

// 5. CEK TOTAL PENDAPATAN
echo "5. TOTAL PENDAPATAN (status confirmed/completed):\n";
$stmt = $pdo->query("SELECT COALESCE(SUM(total_harga), 0) as total FROM pesanan WHERE status_pesanan IN ('confirmed', 'completed')");
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo "   Total: Rp " . number_format($row['total'], 0, ',', '.') . "\n";

echo "\n========================================\n";
echo "✅ SELESAI!\n";
echo "========================================\n";