<?php
// update_telegram_id.php
// Halaman untuk mengupdate Telegram ID siswa

include "config.php";
session_start();

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

// Proses update
if (isset($_POST['update'])) {
    $telegram_id = trim($_POST['telegram_id']);
    $siswa_id = intval($_POST['siswa_id']);
    
    if (!empty($telegram_id) && preg_match('/^[0-9]+$/', $telegram_id)) {
        mysqli_query($conn, "UPDATE siswa SET telegram_id='$telegram_id' WHERE id=$siswa_id");
        $_SESSION['success'] = "Telegram ID berhasil diupdate!";
    } else {
        $_SESSION['error'] = "Telegram ID harus berupa angka!";
    }
    header("Location: update_telegram_id.php");
    exit;
}

// Proses hapus Telegram ID
if (isset($_GET['hapus'])) {
    $id = intval($_GET['hapus']);
    mysqli_query($conn, "UPDATE siswa SET telegram_id='' WHERE id=$id");
    $_SESSION['success'] = "Telegram ID berhasil dihapus!";
    header("Location: update_telegram_id.php");
    exit;
}

// Ambil data siswa
$q = mysqli_query($conn, "SELECT id, nis, nisn, nama, kelas, no_wa, telegram_id FROM siswa WHERE status='aktif' ORDER BY kelas, nama");
?>

<!DOCTYPE html>
<html>
<head>
    <title>Update Telegram ID Siswa</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
    <style>
        .badge-telegram {
            background-color: #0088cc;
            color: white;
            padding: 3px 10px;
            border-radius: 15px;
            font-size: 0.75rem;
        }
        .badge-no-telegram {
            background-color: #6c757d;
            color: white;
            padding: 3px 10px;
            border-radius: 15px;
            font-size: 0.75rem;
        }
        .telegram-help {
            background-color: #e8f4fd;
            border-left: 4px solid #0088cc;
            padding: 10px 15px;
            border-radius: 5px;
            margin-bottom: 15px;
        }
    </style>
</head>
<body>
<div class="container py-4">
    <h2><i class="fab fa-telegram"></i> Update Telegram ID Siswa</h2>
    <a href="dashboard.php" class="btn btn-secondary mb-3">← Kembali</a>
    <a href="siswa.php" class="btn btn-primary mb-3">📋 Data Siswa</a>
    
    <?php if (isset($_SESSION['success'])): ?>
        <div class="alert alert-success alert-dismissible fade show">
            <i class="fas fa-check-circle"></i> <?= $_SESSION['success'] ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
        <?php unset($_SESSION['success']); ?>
    <?php endif; ?>
    
    <?php if (isset($_SESSION['error'])): ?>
        <div class="alert alert-danger alert-dismissible fade show">
            <i class="fas fa-exclamation-circle"></i> <?= $_SESSION['error'] ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
        <?php unset($_SESSION['error']); ?>
    <?php endif; ?>
    
    <div class="card">
        <div class="card-header bg-primary text-white">
            <h5 class="mb-0"><i class="fas fa-list"></i> Daftar Siswa</h5>
        </div>
        <div class="card-body">
            <div class="telegram-help">
                <h6><i class="fas fa-info-circle"></i> Cara Mendapatkan Telegram ID:</h6>
                <ol class="mb-0">
                    <li>Buka Telegram dan cari <strong>@userinfobot</strong></li>
                    <li>Kirim pesan <strong>/start</strong></li>
                    <li>Bot akan mengirimkan ID Anda: <code>Your ID: 123456789</code></li>
                    <li>Copy angka ID dan masukkan ke kolom di bawah</li>
                </ol>
            </div>
            
            <div class="table-responsive">
                <table class="table table-bordered table-hover table-striped">
                    <thead class="table-primary">
                        <tr class="text-center">
                            <th width="5%">No</th>
                            <th width="8%">NIS</th>
                            <th width="12%">NISN</th>
                            <th width="18%">Nama</th>
                            <th width="8%">Kelas</th>
                            <th width="12%">No. WA</th>
                            <th width="18%">Telegram ID</th>
                            <th width="19%">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $no=1; while($row = mysqli_fetch_assoc($q)): ?>
                        <tr>
                            <td class="text-center"><?= $no++ ?></td>
                            <td class="text-center"><?= $row['nis'] ?></td>
                            <td><?= $row['nisn'] ?></td>
                            <td><?= htmlspecialchars($row['nama']) ?></td>
                            <td class="text-center"><?= $row['kelas'] ?></td>
                            <td><?= $row['no_wa'] ?: '<span class="text-muted">-</span>' ?></td>
                            <td class="text-center">
                                <?php if (!empty($row['telegram_id'])): ?>
                                    <span class="badge-telegram">
                                        <i class="fab fa-telegram"></i> <?= $row['telegram_id'] ?>
                                    </span>
                                <?php else: ?>
                                    <span class="badge-no-telegram">
                                        <i class="fas fa-times-circle"></i> Belum diisi
                                    </span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <form method="post" class="d-flex gap-1">
                                    <input type="hidden" name="siswa_id" value="<?= $row['id'] ?>">
                                    <input type="text" name="telegram_id" class="form-control form-control-sm" 
                                           placeholder="Masukkan ID" style="width:130px"
                                           value="<?= $row['telegram_id'] ?>">
                                    <button type="submit" name="update" class="btn btn-sm btn-success" title="Simpan">
                                        <i class="fas fa-save"></i>
                                    </button>
                                    <?php if (!empty($row['telegram_id'])): ?>
                                        <a href="update_telegram_id.php?hapus=<?= $row['id'] ?>" 
                                           class="btn btn-sm btn-danger" 
                                           onclick="return confirm('Hapus Telegram ID <?= htmlspecialchars($row['nama']) ?>?')" 
                                           title="Hapus">
                                            <i class="fas fa-trash"></i>
                                        </a>
                                    <?php endif; ?>
                                </form>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>