<?php
session_start();
include 'includes/config.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nama = trim($_POST['nama']);
    $no_ic = trim($_POST['no_ic']);
    $no_pendaftaran = trim($_POST['no_pendaftaran']);
    $jantina = $_POST['jantina'];
    $bangsa = trim($_POST['bangsa']);
    $agama = trim($_POST['agama']);
    $warganegara = trim($_POST['warganegara']);
    $emel = trim($_POST['emel']);
    $no_telefon = trim($_POST['no_telefon']);
    $program = trim($_POST['program']);
    $username = trim($_POST['username']);
    $kata_laluan = password_hash($_POST['kata_laluan'], PASSWORD_DEFAULT);
    $status = 'Aktif';

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

    $cek = mysqli_query($conn, "SELECT * FROM pelajar WHERE no_ic='$no_ic' OR username='$username'");
    if(mysqli_num_rows($cek) > 0){
        $error = "No IC atau Username telah digunakan.";
    } else {
        $sql = "INSERT INTO pelajar 
            (nama,no_ic,no_pendaftaran,jantina,bangsa,agama,warganegara,emel,no_telefon,program,username,kata_laluan,status,gambar)
            VALUES ('$nama','$no_ic','$no_pendaftaran','$jantina','$bangsa','$agama','$warganegara','$emel','$no_telefon','$program','$username','$kata_laluan','$status','$gambar')";
        
        if(mysqli_query($conn,$sql)){
            echo "<script>alert('Pendaftaran berjaya! Sila log masuk.'); window.location='login_pelajar.php';</script>";
            exit;
        } else {
            $error = "Ralat: " . mysqli_error($conn);
        }
    }
}
?>

<!DOCTYPE html>
<html lang="ms">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Daftar Pelajar - e-SPEKTRA</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">
<style>
body { 
    background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: 'Rubik', sans-serif;
    padding: 20px;
}
.card {
    border-radius: 12px;
    padding: 30px;
    width: 100%;
    max-width: 700px;
    background: white;
    box-shadow: 0 8px 25px rgba(0,0,0,0.2);
}
.img-preview { 
    width: 120px; 
    height: 120px; 
    object-fit: cover; 
    border-radius: 8px; 
    display: none; 
    margin-top: 10px; 
}
</style>
</head>
<body>

<div class="card">
    <h3 class="text-center mb-3">Daftar Pelajar Baharu</h3>
    <p class="text-center text-muted mb-4">Sila lengkapkan maklumat dengan tepat</p>

    <?php if(isset($error)): ?>
        <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <form method="POST" enctype="multipart/form-data">
        <?php include 'includes/form_pelajar_fields.php'; ?>

        <div class="mb-3">
            <label class="form-label">Gambar (Opsyenal)</label>
            <input type="file" name="gambar" class="form-control" accept="image/*" onchange="previewImage(event)">
            <img id="imgPreview" class="img-preview" src="#" alt="Preview Gambar">
        </div>

        <div class="d-flex justify-content-between mt-4">
            <a href="login_pelajar.php" class="btn btn-secondary"><i class="bi bi-arrow-left"></i> Kembali</a>
            <button type="submit" class="btn btn-primary"><i class="bi bi-save"></i> Daftar</button>
        </div>
    </form>
</div>

<script>
function previewImage(event) {
    const reader = new FileReader();
    const img = document.getElementById('imgPreview');
    reader.onload = function(){
        img.src = reader.result;
        img.style.display = 'block';
    }
    if(event.target.files[0]){
        reader.readAsDataURL(event.target.files[0]);
    } else {
        img.style.display = 'none';
        img.src = '#';
    }
}
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
