<?php
// notifikasi_admin.php
// Halaman admin untuk mengatur notifikasi

include "config.php";
include "push_notification.php";
include "telegram_config.php";
session_start();

if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    header('Location: login.php');
    exit;
}

// Proses update Telegram Chat ID
if (isset($_POST['update_telegram'])) {
    $chat_id = mysqli_real_escape_string($conn, $_POST['telegram_chat_id']);
    $content = file_get_contents('telegram_config.php');
    $content = preg_replace("/define\('TELEGRAM_CHAT_ID', '[^']*'\)/", "define('TELEGRAM_CHAT_ID', '$chat_id')", $content);
    file_put_contents('telegram_config.php', $content);
    $message = "✅ Telegram Chat ID berhasil diupdate";
}

// Proses test notifikasi
if (isset($_POST['test_notification'])) {
    $test_message = "🧪 Test Notifikasi dari Sistem Absensi\nWaktu: " . date('Y-m-d H:i:s');
    $wa_result = kirimWA($conn, $_POST['test_wa'] ?? '', $test_message);
    $telegram_result = kirimTelegram($test_message);
    $push_result = kirimPushNotification($test_message, 'Test Notifikasi');
    $notification_results = "WA: $wa_result | Telegram: $telegram_result | Push: $push_result";
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Pengaturan Notifikasi</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body>
    <div class="container mt-4">
        <h2><i class="fas fa-bell"></i> Pengaturan Notifikasi</h2>
        <a href="dashboard.php" class="btn btn-secondary mb-3"><i class="fas fa-arrow-left"></i> Kembali</a>
        
        <?php if (isset($message)): ?>
            <div class="alert alert-success"><?= $message ?></div>
        <?php endif; ?>
        
        <?php if (isset($notification_results)): ?>
            <div class="alert alert-info"><?= $notification_results ?></div>
        <?php endif; ?>
        
        <div class="row">
            <!-- Telegram Settings -->
            <div class="col-md-6">
                <div class="card mb-4">
                    <div class="card-header bg-primary text-white">
                        <i class="fab fa-telegram"></i> Telegram Bot
                    </div>
                    <div class="card-body">
                        <form method="POST">
                            <div class="mb-3">
                                <label class="form-label">Bot Token</label>
                                <input type="text" class="form-control" value="<?= TELEGRAM_BOT_TOKEN ?>" readonly>
                                <small class="text-muted">Token sudah diset di telegram_config.php</small>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Chat ID</label>
                                <input type="text" class="form-control" name="telegram_chat_id" 
                                       value="<?= TELEGRAM_CHAT_ID ?>" 
                                       placeholder="Masukkan Chat ID Telegram">
                                <small class="text-muted">
                                    Kirim pesan ke bot lalu akses: 
                                    https://api.telegram.org/bot<?= TELEGRAM_BOT_TOKEN ?>/getUpdates
                                </small>
                            </div>
                            <button type="submit" name="update_telegram" class="btn btn-primary">
                                <i class="fas fa-save"></i> Update Chat ID
                            </button>
                        </form>
                    </div>
                </div>
            </div>
            
            <!-- Push Notification Settings -->
            <div class="col-md-6">
                <div class="card mb-4">
                    <div class="card-header bg-success text-white">
                        <i class="fas fa-bell"></i> Push Notification
                    </div>
                    <div class="card-body">
                        <h6>VAPID Public Key:</h6>
                        <div class="mb-2">
                            <code class="bg-light p-2 d-block text-break"><?= getVapidPublicKey() ?: 'Belum diset' ?></code>
                        </div>
                        <h6>VAPID Private Key:</h6>
                        <div class="mb-3">
                            <code class="bg-light p-2 d-block text-break"><?= substr(getVapidPrivateKey(), 0, 50) . '...' ?: 'Belum diset' ?></code>
                        </div>
                        <div class="alert alert-info">
                            <i class="fas fa-info-circle"></i> Generate VAPID keys di 
                            <a href="https://web-push-codelab.glitch.me/" target="_blank">web-push-codelab</a>
                            lalu update di database tabel profil_sekolah
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Test Notification -->
        <div class="card">
            <div class="card-header bg-warning">
                <i class="fas fa-vial"></i> Test Notifikasi
            </div>
            <div class="card-body">
                <form method="POST">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Nomor WA Test (opsional)</label>
                                <input type="text" class="form-control" name="test_wa" placeholder="08123456789">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">&nbsp;</label>
                                <button type="submit" name="test_notification" class="btn btn-warning w-100">
                                    <i class="fas fa-bell"></i> Kirim Test Notifikasi
                                </button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html>