<?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 pelajar
$sql = "SELECT * FROM pelajar WHERE id = $id_pelajar LIMIT 1";
$result = mysqli_query($conn, $sql);
$pelajar = mysqli_fetch_assoc($result);

// Kira merit auto
$sqlMerit = "
    SELECT COUNT(*) AS jumlah_hadir
    FROM penyertaan
    WHERE pelajar_id = $id_pelajar
      AND status_kehadiran = 'Hadir'
";
$resultMerit = mysqli_query($conn, $sqlMerit);
$dataMerit = mysqli_fetch_assoc($resultMerit);
$jumlahMerit = ($dataMerit['jumlah_hadir'] ?? 0) * 5;

// Gambar profil
$gambar_url = !empty($pelajar['gambar']) ? 'uploads/' . $pelajar['gambar'] : 'images/default-avatar.png';

// Handle upload gambar
if (isset($_FILES['gambar'])) {
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["gambar"]["name"]);
    $uploadOk = 1;
    $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

    $check = getimagesize($_FILES["gambar"]["tmp_name"]);
    if($check === false) $uploadOk = 0;

    if ($uploadOk && move_uploaded_file($_FILES["gambar"]["tmp_name"], $target_file)) {
        mysqli_query($conn, "UPDATE pelajar SET gambar='".mysqli_real_escape_string($conn, $_FILES["gambar"]["name"])."' WHERE id=$id_pelajar");
        header("Location: profil_pelajar.php");
        exit;
    }
}
?>

<style>
.main-content {
    margin-left: 180px;
    margin-top: 100px;
    padding: 30px 40px;
    min-height: calc(100vh - 100px);
    font-family: 'Rubik', sans-serif;
    background-color: #f8f9fc;
}

.page-title {
    font-size: 32px;
    font-weight: 700;
    color: #3b0a77;
    margin-bottom: 20px;
}

.card-container {
    background: white;
    padding: 25px 30px;
    border-radius: 12px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.08);
}

.img-profile {
    width: 150px;
    height: 150px;
    object-fit: cover;
    border-radius: 50%;
    cursor: pointer;
    border: 3px solid #3b0a77;
}

.btn-official {
    background-color: #3b0a77;
    color: white;
    border-radius: 8px;
    padding: 10px 20px;
    font-weight: 600;
    text-decoration: none;
    margin-right: 10px;
    display: inline-block;
    transition: 0.3s;
}
.btn-official:hover { 
    background-color: #2b0652; 
    text-decoration: none;
}

.badge-merit {
    background: #198754;
    color: #fff;
    padding: 8px 14px;
    border-radius: 8px;
    font-size: 15px;
}
</style>

<div class="main-content">
    <h2 class="page-title">
        Profil Pelajar
        <span class="badge-merit">Markah Merit: <?= $jumlahMerit ?></span>
    </h2>

    <div class="card-container">
        <div class="row align-items-center g-4">
            <div class="col-md-3 text-center">
                <img src="<?= htmlspecialchars($gambar_url) ?>" alt="Gambar Profil" class="img-profile"
                     onclick="document.getElementById('uploadGambar').click();" title="Klik untuk tukar gambar">
                <form id="formGambar" method="POST" enctype="multipart/form-data" style="display:none;">
                    <input type="file" name="gambar" id="uploadGambar" accept="image/*" onchange="document.getElementById('formGambar').submit();">
                </form>
            </div>
            <div class="col-md-9">
                <div class="row">
                    <div class="col-md-6">
                        <p><strong>Nama:</strong> <?= htmlspecialchars($pelajar['nama'] ?? '-') ?></p>
                        <p><strong>No. Kad Pengenalan:</strong> <?= htmlspecialchars($pelajar['no_ic'] ?? '-') ?></p>
                        <p><strong>No. Pendaftaran:</strong> <?= htmlspecialchars($pelajar['no_pendaftaran'] ?? '-') ?></p>
                        <p><strong>Program:</strong> <?= htmlspecialchars($pelajar['program'] ?? '-') ?></p>
                        <p><strong>Jantina:</strong> <?= htmlspecialchars($pelajar['jantina'] ?? '-') ?></p>
                    </div>
                    <div class="col-md-6">
                        <p><strong>Bangsa:</strong> <?= htmlspecialchars($pelajar['bangsa'] ?? '-') ?></p>
                        <p><strong>Agama:</strong> <?= htmlspecialchars($pelajar['agama'] ?? '-') ?></p>
                        <p><strong>No. Telefon:</strong> <?= htmlspecialchars($pelajar['no_telefon'] ?? '-') ?></p>
                        <p><strong>E-mel:</strong> <?= htmlspecialchars($pelajar['emel'] ?? '-') ?></p>
                    </div>
                </div>

                <div class="mt-4">
                    <a href="kemaskini_profil.php" class="btn-official">Kemaskini Maklumat</a>
                    <a href="tukar_katalaluan_pelajar.php" class="btn-official">Tukar Kata Laluan</a>
                </div>
            </div>
        </div>
    </div>
</div>

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