<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
require __DIR__ . '/../Login/config.php'; // config.php menyediakan $pdo

// Pastikan ada ID anggota yang dikirim
if (!isset($_GET['id'])) {
    header("Location: daftaranggota.php");
    exit;
}

$id = (int) $_GET['id'];
$errors = [];
$success = false;

// Ambil data anggota berdasarkan ID
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");
$stmt->execute([':id' => $id]);
$anggota = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$anggota) {
    die("Anggota tidak ditemukan.");
}

// Jika form disubmit
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $fullname = trim($_POST['fullname'] ?? '');
    $nis = trim($_POST['nis'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $username = trim($_POST['username'] ?? '');
    $phone = trim($_POST['phone'] ?? '');
    $class = trim($_POST['class'] ?? '');

    // Validasi sederhana
    if ($fullname === '') $errors[] = "Nama lengkap wajib diisi.";
    if ($nis === '') $errors[] = "NIS wajib diisi.";
    if ($email === '') $errors[] = "Email wajib diisi.";
    if ($username === '') $errors[] = "Username wajib diisi.";
    if ($phone === '') $errors[] = "Nomor telepon wajib diisi.";
    if ($class === '') $errors[] = "Kelas wajib diisi.";

    if (empty($errors)) {
        try {
            $stmt = $pdo->prepare("UPDATE users 
                                   SET fullname = :fullname, nis = :nis, email = :email, username = :username, 
                                       phone = :phone, class = :class 
                                   WHERE id = :id");
            $stmt->execute([
                ':fullname' => $fullname,
                ':nis' => $nis,
                ':email' => $email,
                ':username' => $username,
                ':phone' => $phone,
                ':class' => $class,
                ':id' => $id
            ]);

            header("Location: daftaranggota.php?success=1");
            exit;
        } catch (Exception $e) {
            $errors[] = "Gagal memperbarui data anggota: " . $e->getMessage();
        }
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Anggota - Pustakaia</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
body { margin:0; font-family:"Poppins", Arial, sans-serif; line-height:1.6; background:#f4f7f9; color:#222; display:flex; min-height:100vh; flex-direction:column; }
a { text-decoration:none; }
.container { display:flex; flex:1; }
main { flex:1; padding:20px; margin-left:250px; }

/* Sidebar */
.sidebar { width:250px; height:100vh; background:#fff; border-right:1px solid #ddd; padding:10px 0; position:fixed; top:0; left:0; overflow-y:auto; }
.sidebar-header { padding:15px 20px; border-bottom:1px solid #ddd; }
.sidebar-header h2 { margin:0; font-size:18px; color:#069797; }
.sidebar-header p { margin:0; font-size:12px; color:#777; }
.sidebar-menu { list-style:none; padding:0; margin:0; }
.sidebar-menu li { font-size:14px; position:relative; }
.sidebar-menu li > a { display:flex; align-items:center; padding:12px 20px; color:#444; width:100%; transition: background 0.2s; }
.sidebar-menu li > a i { margin-right:10px; font-size:16px; color:#666; }
.sidebar-menu li > a:hover { background:#f5f5f5; border-radius:6px; }
.sidebar-menu li > a.active { background:#e0ebff; color:#069797; font-weight:600; border-radius:6px; }
.sidebar-menu li > a.active i { color:#069797; }
.submenu { list-style:none; padding-left:0; margin:0; display:none; }
.submenu li a { display:flex; align-items:center; padding:8px 40px; font-size:13px; color:#444; }
.submenu li a:hover { background:#f5f5f5; border-radius:6px; }

/* Form Card */
.form-card {
    background:#fff;
    padding:20px;
    border-radius:8px;
    box-shadow:0 2px 6px rgba(0,0,0,0.1);
    max-width:1600px;
    margin:auto;
    box-sizing:border-box;
}
.form-card label{display:block;margin-top:12px;font-weight:600;color:#069797;}
.form-card input[type=text], .form-card input[type=email] {
    width:100%; padding:10px; margin-top:6px; border:1px solid #ddd; border-radius:4px; box-sizing:border-box;
}
.form-card button{background:#069797;color:#fff;padding:10px 16px;border:none;border-radius:6px;margin-top:12px;cursor:pointer;font-weight:700;}
.errors{background:#ffe6e6;border:1px solid #ffcccc;padding:10px;margin-bottom:12px;}
.success{background:#e6ffef;border:1px solid #c7f0d6;padding:10px;margin-bottom:12px;}

/* Footer */
footer { background:#fff; border-top:1px solid #ddd; padding:15px 30px; font-size:14px; display:flex; justify-content:space-between; align-items:center; margin-top:auto; margin-left:250px; color:#555; }
.footer-right a { margin-left:15px; color:#555; font-weight:500; }
.footer-right a:hover { text-decoration:underline; }
</style>
</head>
<body>
<div class="container">
    <!-- Sidebar -->
    <aside class="sidebar">
        <div class="sidebar-header">
            <h2>📚 Pustakaia</h2>
            <p>Admin Panel</p>
        </div>
        <ul class="sidebar-menu">
            <li><a href="dsadmin.php"><i class="fa fa-tachometer-alt"></i> Dashboard</a></li>
            <li class="has-submenu">
                <a href="#"><i class="fa fa-book"></i> Kelola Buku</a>
                <ul class="submenu">
                    <li><a href="daftarbuku.php"><i class="fa fa-list"></i> Daftar Buku</a></li>
                    <li><a href="add_book.php"><i class="fa fa-plus"></i> Tambahkan Buku</a></li>
                </ul>
            </li>
            <li class="has-submenu active">
                <a href="#"><i class="fa fa-users"></i> Kelola Anggota</a>
                <ul class="submenu" style="display:block;">
                <li><a href="daftaranggota.php" class="active" style="color:#069797;"><i class="fa fa-list"></i> Daftar Anggota</a></li>
                    <li><a href="add_anggota.php"><i class="fa fa-user-plus"></i> Tambahkan Anggota</a></li>
                </ul>
            </li>
            <li><a href="#"><i class="fa fa-exchange-alt"></i> Kelola Peminjaman</a></li>
            <li><a href="#"><i class="fa fa-bell"></i> Notifikasi</a></li>
        </ul>
        <hr>
        <ul class="sidebar-menu">
            <li><a href="#"><i class="fa fa-cog"></i> Pengaturan</a></li>
            <li><a href="../index2.php"><i class="fa fa-sign-out-alt"></i> Keluar</a></li>
        </ul>
    </aside>

    <!-- Main Content -->
    <main>
        <h1>Edit Data Anggota</h1>
        <div class="form-card">
            <?php if (!empty($errors)): ?>
                <div class="errors">
                    <ul>
                        <?php foreach ($errors as $e): ?>
                            <li><?= htmlspecialchars($e) ?></li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            <?php endif; ?>

            <form method="post">
                <label>Nama Lengkap</label>
                <input type="text" name="fullname" value="<?= htmlspecialchars($_POST['fullname'] ?? $anggota['fullname']) ?>" required>

                <label>NIS</label>
                <input type="text" name="nis" value="<?= htmlspecialchars($_POST['nis'] ?? $anggota['nis']) ?>" required>

                <label>Email</label>
                <input type="email" name="email" value="<?= htmlspecialchars($_POST['email'] ?? $anggota['email']) ?>" required>

                <label>Username</label>
                <input type="text" name="username" value="<?= htmlspecialchars($_POST['username'] ?? $anggota['username']) ?>" required>

                <label>Nomor Telepon</label>
                <input type="text" name="phone" value="<?= htmlspecialchars($_POST['phone'] ?? $anggota['phone']) ?>" required>

                <label>Kelas</label>
                <input type="text" name="class" value="<?= htmlspecialchars($_POST['class'] ?? $anggota['class']) ?>" required>

                <button type="submit">Simpan Perubahan</button>
            </form>
        </div>
    </main>
</div>

<footer>
    <div>© <?= date('Y') ?>, Pustakaia | Website developed by Grup 3</div>
    <div class="footer-right">
        <a href="#">Terms of Use</a>
        <a href="#">Privacy Policy</a>
    </div>
</footer>

<script>
document.querySelectorAll('.has-submenu > a').forEach(menu => {
    menu.addEventListener('click', e => {
        e.preventDefault();
        const submenu = menu.nextElementSibling;
        submenu.style.display = submenu.style.display === 'block' ? 'none' : 'block';
    });
});
</script>
</body>
</html>
