<?php
include 'koneksi.php';
header('Content-Type: text/html; charset=utf-8');
echo "<style>body{font-family:monospace;padding:20px;background:#111;color:#0f0;} .ok{color:#0f0} .err{color:#f44} .warn{color:#fa0}</style>";
echo "<h2>🔍 Cek Koneksi & Database SPBU</h2>";

if ($koneksi) {
    echo "<p class='ok'>✅ Koneksi ke MySQL berhasil</p>";
} else {
    echo "<p class='err'>❌ Koneksi GAGAL: " . mysqli_connect_error() . "</p>"; exit;
}

$db_cek = mysqli_query($koneksi, "SELECT DATABASE() AS db");
$db_row = mysqli_fetch_assoc($db_cek);
echo "<p class='ok'>✅ Database aktif: <b>{$db_row['db']}</b></p>";
echo "<p class='ok'>✅ Nama tabel: <b>$nama_tabel</b></p>";

$tabel_cek = mysqli_query($koneksi, "SHOW TABLES LIKE '$nama_tabel'");
if (mysqli_num_rows($tabel_cek) > 0) {
    echo "<p class='ok'>✅ Tabel <b>$nama_tabel</b> ditemukan</p>";
} else {
    echo "<p class='err'>❌ Tabel <b>$nama_tabel</b> TIDAK ADA!</p>";
    echo "<p class='warn'>⚠️ Jalankan schema_mysql.sql di phpMyAdmin</p>"; exit;
}

$count_cek = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM `$nama_tabel`");
$count_row = mysqli_fetch_assoc($count_cek);
echo "<p class='ok'>✅ Jumlah data: <b>{$count_row['total']}</b> baris</p>";

$test = mysqli_query($koneksi, "INSERT INTO `$nama_tabel` (nama_spbu, no_wa, status_24_jam, lat, lng) VALUES ('TEST_DELETE_ME', '000', 'no', 0, 0)");
if ($test) {
    $ins_id = mysqli_insert_id($koneksi);
    mysqli_query($koneksi, "DELETE FROM `$nama_tabel` WHERE id = $ins_id");
    echo "<p class='ok'>✅ Test INSERT & DELETE berhasil — database siap digunakan!</p>";
} else {
    echo "<p class='err'>❌ Test INSERT gagal: " . mysqli_error($koneksi) . "</p>";
}
echo "<hr><p>File ini bisa dihapus setelah tidak diperlukan.</p>";
