<?php
session_start();
if (!isset($_SESSION['id_pelajar'])) {
    header('Location: login_pelajar.php');
    exit;
}

include 'includes/header_pelajar.php';
include 'includes/sidebar_pelajar.php';
include 'includes/config.php';

$id_pelajar = (int) $_SESSION['id_pelajar'];

// Dapatkan maklumat semasa pelajar
$sql = "SELECT * FROM pelajar WHERE id=$id_pelajar LIMIT 1";
$result = mysqli_query($conn, $sql);
$pelajar = mysqli_fetch_assoc($result);

$message = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nama = $_POST['nama'];
    $no_ic = $_POST['no_ic'];
    $no_pendaftaran = $_POST['no_pendaftaran'];
    $jantina = $_POST['jantina'];
    $bangsa = $_POST['bangsa'];
    $agama = $_POST['agama'];
    $warganegara = $_POST['warganegara'];
    $emel = $_POST['emel'];
    $no_telefon = $_POST['no_telefon'];
    $program = $_POST['program'];

    $gambar = $pelajar['gambar']; // Kekalkan gambar lama
    if (!empty($_FILES['gambar']['name'])) {
        $gambar = time() . '_' . $_FILES['gambar']['name'];
        move_uploaded_file($_FILES['gambar']['tmp_name'], "uploads/" . $gambar);
    }

    $sqlUpdate = "UPDATE pelajar SET 
        nama='$nama',
        no_ic='$no_ic',
        no_pendaftaran='$no_pendaftaran',
        jantina='$jantina',
        bangsa='$bangsa',
        agama='$agama',
        warganegara='$warganegara',
        emel='$emel',
        no_telefon='$no_telefon',
        program='$program',
        gambar='$gambar'
        WHERE id=$id_pelajar";

    if (mysqli_query($conn, $sqlUpdate)) {
        $message = "Maklumat profil berjaya dikemaskini.";
        // Kemaskini semula data
        $result = mysqli_query($conn, $sql);
        $pelajar = mysqli_fetch_assoc($result);
    } else {
        $message = "Ralat: " . mysqli_error($conn);
    }
}
?>

<div class="main-content">
    <h2 class="page-title">Kemaskini Maklumat Profil</h2>

    <?php if($message): ?>
        <div class="alert alert-success"><?= htmlspecialchars($message) ?></div>
    <?php endif; ?>

    <div class="card-container">
        <form method="POST" enctype="multipart/form-data">
            <div class="row g-4 align-items-center">
                <div class="col-md-3 text-center">
                    <img src="<?= !empty($pelajar['gambar']) ? 'uploads/'.$pelajar['gambar'] : 'images/default-avatar.png'; ?>" 
                         class="img-profile mb-3" style="width:150px;height:150px;object-fit:cover;border-radius:50%;border:3px solid #3b0a77;cursor:pointer;"
                         onclick="document.getElementById('uploadGambar').click();" 
                         title="Klik untuk tukar gambar">
                    <input type="file" name="gambar" id="uploadGambar" style="display:none;" accept="image/*">
                </div>

                <div class="col-md-9">
                    <div class="row">
                        <div class="col-md-6">
                            <label>Nama</label>
                            <input type="text" name="nama" class="form-control" value="<?= htmlspecialchars($pelajar['nama']) ?>" required>
                            
                            <label>No. Kad Pengenalan</label>
                            <input type="text" name="no_ic" class="form-control" value="<?= htmlspecialchars($pelajar['no_ic']) ?>" required>
                            
                            <label>No. Pendaftaran</label>
                            <input type="text" name="no_pendaftaran" class="form-control" value="<?= htmlspecialchars($pelajar['no_pendaftaran']) ?>" required>
                            
                            <label>Jantina</label>
                            <select name="jantina" class="form-control" required>
                                <option value="Lelaki" <?= $pelajar['jantina']=='Lelaki'?'selected':'' ?>>Lelaki</option>
                                <option value="Perempuan" <?= $pelajar['jantina']=='Perempuan'?'selected':'' ?>>Perempuan</option>
                            </select>

                            <label>Program</label>
                            <input type="text" name="program" class="form-control" value="<?= htmlspecialchars($pelajar['program']) ?>">
                        </div>

                        <div class="col-md-6">
                            <label>Bangsa</label>
                            <input type="text" name="bangsa" class="form-control" value="<?= htmlspecialchars($pelajar['bangsa']) ?>">
                            
                            <label>Agama</label>
                            <input type="text" name="agama" class="form-control" value="<?= htmlspecialchars($pelajar['agama']) ?>">
                            
                            <label>Warganegara</label>
                            <input type="text" name="warganegara" class="form-control" value="<?= htmlspecialchars($pelajar['warganegara']) ?>">
                            
                            <label>No. Telefon</label>
                            <input type="text" name="no_telefon" class="form-control" value="<?= htmlspecialchars($pelajar['no_telefon']) ?>">
                            
                            <label>E-mel</label>
                            <input type="email" name="emel" class="form-control" value="<?= htmlspecialchars($pelajar['emel']) ?>">
                        </div>
                    </div>

                    <div class="mt-4">
                        <button type="submit" class="btn-official">Kemaskini Maklumat</button>
                        <a href="profil_pelajar.php" class="btn-official">Kembali ke Profil</a>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

<?php include 'includes/footer.php'; ?>
