<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Koneksi database
try {
    $pdo = new PDO("mysql:host=localhost;dbname=platform_kapal_samosir;charset=utf8mb4", "root", "");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "✅ Koneksi database berhasil!\n\n";
} catch (PDOException $e) {
    die("❌ Koneksi gagal: " . $e->getMessage() . "\n");
}

// Data test
$user_id = 11;
$kapal_id = 59;
$route_id = 9;
$kode_pesanan = 'TEST-' . date('Ymd-His');
$nama_kapal = 'KM. HORAS IMANUEL';
$tanggal = date('Y-m-d', strtotime('+2 days'));
$tanggal_pesan = date('Y-m-d');
$jumlah_penumpang = 1;
$total_harga = 50000;
$status_pembayaran = 'belum_bayar';
$catatan = 'Test booking';
$nama_lengkap = 'Test User';
$email = 'test@example.com';
$no_hp = '08123456789';
$durasi_jam = 24;
$nik_ktp = '1234567890123456';
$status_pesanan = 'pending';
$jenis_booking = 'private';
$status_pembatalan = 'aktif';
$denda = 0;

// QUERY DENGAN 20 TANDA TANYA - SATU BARIS UNTUK VALUES
$sql = "INSERT INTO pesanan (
    user_id, kapal_id, route_id, kode_pesanan, nama_kapal,
    tanggal_berangkat, tanggal_pesan, jumlah_tiket, total_harga,
    status_pembayaran, catatan, nama_lengkap, email, no_hp,
    durasi_jam, nik_ktp, status_pesanan, jenis_booking,
    status_pembatalan, denda, created_at
) VALUES (
    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW()
)";

$params = [
    $user_id,
    $kapal_id,
    $route_id,
    $kode_pesanan,
    $nama_kapal,
    $tanggal,
    $tanggal_pesan,
    $jumlah_penumpang,
    $total_harga,
    $status_pembayaran,
    $catatan,
    $nama_lengkap,
    $email,
    $no_hp,
    $durasi_jam,
    $nik_ktp,
    $status_pesanan,
    $jenis_booking,
    $status_pembatalan,
    $denda
];

echo "📋 SQL Query:\n" . $sql . "\n\n";
echo "📊 Total parameters: " . count($params) . "\n";
echo "📊 Total ? in query: " . substr_count($sql, '?') . "\n\n";

if (count($params) != substr_count($sql, '?')) {
    echo "❌ ERROR: Jumlah parameter tidak sama!\n";
    echo "   Parameters: " . count($params) . "\n";
    echo "   Tanda tanya: " . substr_count($sql, '?') . "\n";
    exit;
}

echo "🔄 Mencoba menjalankan query...\n\n";

try {
    $stmt = $pdo->prepare($sql);
    $result = $stmt->execute($params);
    
    if ($result) {
        $id = $pdo->lastInsertId();
        echo "✅ SUCCESS: Query berhasil!\n";
        echo "   ID: " . $id . "\n";
        echo "   Kode: " . $kode_pesanan . "\n";
    }
} catch (PDOException $e) {
    echo "❌ ERROR: " . $e->getMessage() . "\n";
    echo "   Error code: " . $e->getCode() . "\n";
}
?>