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

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

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nama = mysqli_real_escape_string($conn, $_POST['nama']);
    $no_ic = mysqli_real_escape_string($conn, $_POST['no_ic']);
    $no_pendaftaran = mysqli_real_escape_string($conn, $_POST['no_pendaftaran']);
    $jantina = $_POST['jantina'] ?? '';
    $bangsa = $_POST['bangsa'] ?? '';
    $agama = $_POST['agama'] ?? '';
    $warganegara = $_POST['warganegara'] ?? '';
    $emel = mysqli_real_escape_string($conn, $_POST['emel']);
    $no_telefon = mysqli_real_escape_string($conn, $_POST['no_telefon']);
    $program = $_POST['program'] ?? '';
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $kata_laluan = $_POST['kata_laluan'];
    $kata_laluan2 = $_POST['kata_laluan2'];

    if ($kata_laluan !== $kata_laluan2) {
        $error = 'Kata laluan tidak sepadan.';
    } else {
        // Check for duplicates
        $check = mysqli_query($conn, "SELECT * FROM pelajar WHERE username='$username' OR no_ic='$no_ic' OR no_pendaftaran='$no_pendaftaran' OR emel='$emel' LIMIT 1");
        if (mysqli_num_rows($check) > 0) {
            $error = 'Nama pengguna, No IC, No Pendaftaran atau Email telah digunakan.';
        } else {
            $password_hash = password_hash($kata_laluan, PASSWORD_DEFAULT);

            $sql = "INSERT INTO pelajar 
                (nama, no_ic, no_pendaftaran, jantina, bangsa, agama, warganegara, emel, no_telefon, program, username, kata_laluan) 
                VALUES 
                ('$nama', '$no_ic', '$no_pendaftaran', '$jantina', '$bangsa', '$agama', '$warganegara', '$emel', '$no_telefon', '$program', '$username', '$password_hash')";

            if (mysqli_query($conn, $sql)) {
                $success = 'Pendaftaran berjaya! Sila log masuk.';
            } else {
                $error = 'Ralat semasa pendaftaran: ' . 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 Akaun Pelajar - e-SPEKTRA</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body class="bg-light d-flex align-items-center justify-content-center" style="min-height:100vh;">

<div class="container" style="max-width: 600px;">
  <div class="card shadow-sm p-4">
    <h3 class="mb-4 text-center">Daftar Akaun Pelajar</h3>

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

    <form method="post" action="">
      <div class="row mb-3">
        <div class="col-md-6">
          <label for="nama" class="form-label">Nama Penuh</label>
          <input type="text" class="form-control" id="nama" name="nama" required />
        </div>
        <div class="col-md-6">
          <label for="no_ic" class="form-label">No. Kad Pengenalan</label>
          <input type="text" class="form-control" id="no_ic" name="no_ic" required />
        </div>
      </div>

      <div class="row mb-3">
        <div class="col-md-6">
          <label for="no_pendaftaran" class="form-label">No. Pendaftaran</label>
          <input type="text" class="form-control" id="no_pendaftaran" name="no_pendaftaran" required />
        </div>
        <div class="col-md-6">
          <label for="jantina" class="form-label">Jantina</label>
          <select class="form-select" id="jantina" name="jantina" required>
            <option value="" selected disabled>Pilih...</option>
            <option value="Lelaki">Lelaki</option>
            <option value="Perempuan">Perempuan</option>
          </select>
        </div>
      </div>

      <div class="row mb-3">
        <div class="col-md-4">
          <label for="bangsa" class="form-label">Bangsa</label>
          <select class="form-select" id="bangsa" name="bangsa" required>
            <option value="" selected disabled>Pilih...</option>
            <option value="Melayu">Melayu</option>
            <option value="Cina">Cina</option>
            <option value="India">India</option>
            <option value="Orang Asli">Orang Asli</option>
            <option value="Bumiputera Sabah">Bumiputera Sabah</option>
            <option value="Bumiputera Sarawak">Bumiputera Sarawak</option>
            <option value="Lain-lain">Lain-lain</option>
          </select>
        </div>
        <div class="col-md-4">
          <label for="agama" class="form-label">Agama</label>
          <select class="form-select" id="agama" name="agama" required>
            <option value="" selected disabled>Pilih...</option>
            <option value="Islam">Islam</option>
            <option value="Kristian">Kristian</option>
            <option value="Hindu">Hindu</option>
            <option value="Buddha">Buddha</option>
            <option value="Lain-lain">Lain-lain</option>
          </select>
        </div>
        <div class="col-md-4">
          <label for="warganegara" class="form-label">Warganegara</label>
          <select class="form-select" id="warganegara" name="warganegara" required>
            <option value="" selected disabled>Pilih...</option>
            <option value="Malaysia">Malaysia</option>
            <option value="Bukan Warganegara">Bukan Warganegara</option>
            <option value="Lain-lain">Lain-lain</option>
          </select>
        </div>
      </div>

      <div class="mb-3">
        <label for="program" class="form-label">Program Pengajian</label>
        <select class="form-select" id="program" name="program" required>
          <option value="" selected disabled>Pilih...</option>
          <option value="SIJIL TEKNOLOGI MAKLUMAT">SIJIL TEKNOLOGI MAKLUMAT</option>
          <option value="SIJIL TEKNOLOGI ELEKTRIK">SIJIL TEKNOLOGI ELEKTRIK</option>
          <option value="DIPLOMA TEKNOLOGI ELEKTRONIK INSTRUMENTASI">DIPLOMA TEKNOLOGI ELEKTRONIK INSTRUMENTASI</option>
        </select>
      </div>

      <div class="mb-3">
        <label for="emel" class="form-label">E-mel</label>
        <input type="email" class="form-control" id="emel" name="emel" required />
      </div>

      <div class="mb-3">
        <label for="no_telefon" class="form-label">No. Telefon</label>
        <input type="text" class="form-control" id="no_telefon" name="no_telefon" />
      </div>

      <div class="row mb-3">
        <div class="col-md-6">
          <label for="username" class="form-label">Nama Pengguna</label>
          <input type="text" class="form-control" id="username" name="username" required />
        </div>
        <div class="col-md-6">
          <label for="kata_laluan" class="form-label">Kata Laluan</label>
          <input type="password" class="form-control" id="kata_laluan" name="kata_laluan" required />
        </div>
      </div>

      <div class="mb-3">
        <label for="kata_laluan2" class="form-label">Sahkan Kata Laluan</label>
        <input type="password" class="form-control" id="kata_laluan2" name="kata_laluan2" required />
      </div>

      <button type="submit" class="btn btn-primary w-100">Daftar</button>
    </form>

    <div class="mt-3 text-center">
      <p>Sudah ada akaun? <a href="login_pelajar.php">Log masuk sekarang</a></p>
    </div>
  </div>
</div>

</body>
</html>
