<?php
// debug_absensi_telegram.php
// Debug kirim notifikasi saat absensi

include "config.php";
include "telegram_config.php";

header('Content-Type: text/html; charset=utf-8');

echo "<h2>🔍 DEBUG ABSENSI TELEGRAM</h2>";
echo "<hr>";

// Ambil data siswa
$nisn = '9876543210';
$query = mysqli_query($conn, "SELECT id, nama, kelas, no_wa, telegram_id FROM siswa WHERE nisn='$nisn' AND status='aktif'");
$siswa = mysqli_fetch_assoc($query);

if (!$siswa) {
    echo "<p style='color:red'>❌ Siswa tidak ditemukan!</p>";
    exit;
}

echo "<h3>Data Siswa:</h3>";
echo "<pre>";
print_r($siswa);
echo "</pre>";

echo "<h3>1. Test Kirim ke Admin (Chat ID: " . TELEGRAM_CHAT_ID . ")</h3>";
$pesan = "🏫 TEST ABSENSI\n\n";
$pesan .= "Nama: " . $siswa['nama'] . "\n";
$pesan .= "Kelas: " . $siswa['kelas'] . "\n";
$pesan .= "Status: TEST\n";
$pesan .= "Waktu: " . date("d-m-Y H:i:s") . "\n";
$pesan .= "\nTerima kasih.";

$result_admin = kirimTelegram($pesan);
echo "<p>Hasil Admin: <strong>" . $result_admin . "</strong></p>";

echo "<h3>2. Test Kirim ke Siswa (Telegram ID: " . $siswa['telegram_id'] . ")</h3>";
$result_siswa = kirimTelegram($pesan, $siswa['telegram_id']);
echo "<p>Hasil Siswa: <strong>" . $result_siswa . "</strong></p>";

echo "<h3>3. Simulasi Proses Absensi</h3>";
echo "<form method='post'>";
echo "<button type='submit' name='simulate_absensi' class='btn btn-primary'>Simulasi Absensi</button>";
echo "</form>";

if (isset($_POST['simulate_absensi'])) {
    // Simulasi kirim notifikasi
    $pesan_notif = "🏫 NOTIFIKASI ABSENSI SISWA\n\n";
    $pesan_notif .= "Nama: " . $siswa['nama'] . "\n";
    $pesan_notif .= "Kelas: " . $siswa['kelas'] . "\n";
    $pesan_notif .= "Status: HADIR\n";
    $pesan_notif .= "Waktu: " . date("d-m-Y H:i:s") . "\n";
    $pesan_notif .= "Keterangan: Tepat Waktu\n";
    $pesan_notif .= "\nTerima kasih.";

    echo "<h4>Kirim Notifikasi:</h4>";
    
    // Kirim ke admin
    $r1 = kirimTelegram($pesan_notif);
    echo "<p>Admin: " . $r1 . "</p>";
    
    // Kirim ke siswa
    if (!empty($siswa['telegram_id'])) {
        $r2 = kirimTelegram($pesan_notif, $siswa['telegram_id']);
        echo "<p>Siswa: " . $r2 . "</p>";
    } else {
        echo "<p style='color:red'>Siswa: Telegram ID kosong!</p>";
    }
}

echo "<hr>";
echo "<p><a href='scan_wa_api.php' class='btn btn-primary'>Kembali ke Scan</a></p>";
echo "<p><a href='siswa.php' class='btn btn-secondary'>Data Siswa</a></p>";
?>