<?php
require __DIR__ . '/../Login/config.php';
require_login();

$user_id = $_SESSION['user_id'];

// Ambil data user
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

$success = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username']);
    $fullname = trim($_POST['fullname']);
    $nis = trim($_POST['nis']);
    $phone = trim($_POST['phone']);
    $class = trim($_POST['class']);
    $photo = $user['photo'];

    // Upload Foto Profil
    if (!empty($_FILES['photo']['name'])) {
        $targetDir = "../uploads/";
        if (!is_dir($targetDir)) mkdir($targetDir, 0777, true);

        $filename = uniqid() . "_" . basename($_FILES["photo"]["name"]);
        $targetFile = $targetDir . $filename;
        $ext = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));

        $allowed = ['jpg', 'jpeg', 'png', 'gif'];
        if (!in_array($ext, $allowed)) {
            $error = "Format gambar tidak valid.";
        } else {
            if (move_uploaded_file($_FILES["photo"]["tmp_name"], $targetFile)) {
                $photo = $filename;
            } else {
                $error = "Gagal upload foto.";
            }
        }
    }

    if (!$error) {
        $sql = "UPDATE users 
                SET username=?, fullname=?, nis=?, phone=?, class=?, photo=? 
                WHERE id=?";
        $stmt = $pdo->prepare($sql);
        $stmt->execute([$username, $fullname, $nis, $phone, $class, $photo, $user_id]);

        $_SESSION['username'] = $username;
        $_SESSION['photo'] = $photo;

        $success = "Profil berhasil diperbarui.";
        header("Refresh:1");
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Edit Profil - Pustakaia</title>
<meta name="viewport" content="width=device-width,initial-scale=1">

<style>
    :root { --accent:#069797; }
    * { box-sizing:border-box; margin:0; padding:0; }

    body {
        font-family:"Poppins", Arial, sans-serif;
        background:linear-gradient(135deg,#f9f9f9,#e9f5ec);
        color:#222;
        min-height:100vh;
        display:flex;
        flex-direction:column;
    }

    /* HEADER */
    header {
        display:flex;
        justify-content:space-between;
        align-items:center;
        padding:15px 30px;
        background:#fff;
        border-bottom:1px solid #ddd;
        position:sticky;
        top:0;
        z-index:100;
        width:100%;
    }
    .logo {
        font-size:20px;
        font-weight:bold;
        color:var(--accent);
        display:flex;
        align-items:center;
        gap:10px;
    }

    /* MAIN */
    main {
        flex:1;
        display:flex;
        justify-content:center;
        padding:36px 20px;
    }
    .card {
        width:100%;
        max-width:600px;
        background:#fff;
        padding:24px;
        border-radius:12px;
        box-shadow:0 6px 18px rgba(0,0,0,0.06);
    }
    h2 {
        color:var(--accent);
        margin-bottom:15px;
        text-align:center;
    }

    .photo-container {
        text-align:center;
        margin-bottom:18px;
    }
    .photo-container img {
        width:120px;
        height:120px;
        border-radius:50%;
        object-fit:cover;
        border:3px solid var(--accent);
    }

    label { font-weight:500; display:block; margin-bottom:5px; }
    input {
        width:100%;
        padding:10px;
        border:1px solid #ddd;
        border-radius:6px;
        margin-bottom:15px;
        font-size:14px;
    }
    button {
        background:var(--accent);
        color:#fff;
        padding:12px;
        border-radius:8px;
        border:none;
        font-size:16px;
        cursor:pointer;
        width:100%;
    }
    button:hover { background:#048080; }

    .msg { text-align:center; margin-bottom:14px; font-size:14px; }
    .success { color:green; }
    .error { color:red; }

    /* FOOTER */
    footer {
        background:#fff;
        padding:20px 40px;
        font-size:14px;
        color:#222;
        border-top:1px solid #ddd;
        margin-top:auto;
    }
    .footer-bottom {
        display:flex;
        justify-content:space-between;
        align-items:center;
        flex-wrap:wrap;
        font-size:13px;
    }
    .footer-links a {
        margin-left:14px;
        color:#222;
        text-decoration:none;
        font-weight:500;
    }
    .footer-links a:hover { text-decoration:underline; }

    @media(max-width:600px){
        header, footer { padding:12px; }
        main { padding:20px; }
    }
</style>

</head>
<body>

<header>
    <div class="logo">
        <a href="books.php" style="text-decoration:none;color:var(--accent);font-size:18px;margin-right:8px">⬅</a>
        📚 Pustakaia
    </div>
</header>

<main>
    <div class="card">

        <h2>Edit Profil</h2>

        <?php if ($success): ?>
            <p class="msg success"><?= htmlspecialchars($success) ?></p>
        <?php endif; ?>

        <?php if ($error): ?>
            <p class="msg error"><?= htmlspecialchars($error) ?></p>
        <?php endif; ?>

        <div class="photo-container">
            <img src="../uploads/<?= htmlspecialchars($user['photo'] ?: 'default.png') ?>" alt="Foto Profil">
        </div>

        <form method="post" enctype="multipart/form-data">

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

            <label>Nama Lengkap</label>
            <input type="text" name="fullname" required value="<?= htmlspecialchars($user['fullname']) ?>">

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

            <label>No HP</label>
            <input type="text" name="phone" value="<?= htmlspecialchars($user['phone']) ?>">

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

            <label>Foto Profil</label>
            <input type="file" name="photo" accept="image/*">

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

    </div>
</main>

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

</body>
</html>
