<?php
// cek_pencairan.php - Cek status pencairan dana

$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 STATUS PENCAIRAN DANA\n";
echo "========================================\n\n";

// 1. CEK PESANAN BELUM CAIR
echo "1. PESANAN STATUS 'belum':\n";
$stmt = $pdo->query("
    SELECT id, kode_pesanan, status_pencairan, status_pesanan, 
           status_pembayaran, nominal_diterima_supir 
    FROM pesanan 
    WHERE status_pencairan = 'belum' 
    AND status_pesanan IN ('confirmed', 'completed') 
    AND status_pembayaran = 'lunas' 
    ORDER BY id DESC 
    LIMIT 10
");
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($data)) {
    echo "   ❌ Tidak ada pesanan dengan status pencairan = belum\n\n";
} else {
    foreach ($data as $row) {
        echo "   ID: {$row['id']} | Kode: {$row['kode_pesanan']} | Status: {$row['status_pencairan']} | Jumlah: Rp " . number_format($row['nominal_diterima_supir'] ?? 0, 0, ',', '.') . "\n";
    }
    echo "\n";
}

// 2. CEK PESANAN SUDAH CAIR
echo "2. PESANAN STATUS 'cair':\n";
$stmt = $pdo->query("
    SELECT id, kode_pesanan, status_pencairan, tanggal_pencairan, 
           nominal_diterima_supir 
    FROM pesanan 
    WHERE status_pencairan = 'cair' 
    ORDER BY id DESC 
    LIMIT 10
");
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($data)) {
    echo "   ❌ Tidak ada pesanan dengan status pencairan = cair\n\n";
} else {
    foreach ($data as $row) {
        echo "   ID: {$row['id']} | Kode: {$row['kode_pesanan']} | Status: {$row['status_pencairan']} | Tgl: " . ($row['tanggal_pencairan'] ?? 'NULL') . " | Jumlah: Rp " . number_format($row['nominal_diterima_supir'] ?? 0, 0, ',', '.') . "\n";
    }
    echo "\n";
}

// 3. CEK SEMUA STATUS PENCAIRAN
echo "3. SEMUA STATUS PENCAIRAN:\n";
$stmt = $pdo->query("SELECT DISTINCT status_pencairan, COUNT(*) as total FROM pesanan GROUP BY status_pencairan");
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($data as $row) {
    echo "   {$row['status_pencairan']}: {$row['total']} pesanan\n";
}
echo "\n";

// 4. CEK KOLOM DI DATABASE
echo "4. CEK KOLOM:\n";
$stmt = $pdo->query("SHOW COLUMNS FROM pesanan LIKE 'status_pencairan'");
if ($stmt->rowCount() > 0) {
    $col = $stmt->fetch(PDO::FETCH_ASSOC);
    echo "   ✅ status_pencairan: {$col['Type']}\n";
} else {
    echo "   ❌ status_pencairan TIDAK ADA!\n";
}

$stmt = $pdo->query("SHOW COLUMNS FROM pesanan LIKE 'tanggal_pencairan'");
if ($stmt->rowCount() > 0) {
    $col = $stmt->fetch(PDO::FETCH_ASSOC);
    echo "   ✅ tanggal_pencairan: {$col['Type']}\n";
} else {
    echo "   ❌ tanggal_pencairan TIDAK ADA!\n";
}

echo "\n========================================\n";