<?php
require_once '../../includes/functions.php';
require_once '../../includes/validation.php';
requireAdmin();

$pageTitle = 'Edit Siswa';
$activeMenu = 'siswa';
$id = intval($_GET['id'] ?? 0);
$siswa = db_fetch("SELECT * FROM siswa WHERE id = ?", [$id]);
if (!$siswa) { setFlash('danger', 'Data tidak ditemukan'); header('Location: list.php'); exit; }

$pengaturan = getPengaturan();
$kelasList = db_fetchAll("SELECT * FROM kelas WHERE tahun_ajaran = ? ORDER BY tingkat, nama_kelas", [$pengaturan['tahun_ajaran_aktif']]);
$errors = [];
$data = $siswa;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = $_POST;
    $v = validate($data)->required('nis','NIS')->required('nama','Nama')->required('jenis_kelamin','Jenis Kelamin');
    if ($v->fails()) {
        $errors = $v->getErrors();
    } else {
        db_update('siswa', [
            'nis' => sanitize($data['nis']),
            'nisn' => trim($data['nisn'] ?? '') !== '' ? sanitize(trim($data['nisn'])) : null,
            'nama' => sanitize($data['nama']),
            'jenis_kelamin' => $data['jenis_kelamin'],
            'tempat_lahir' => sanitize($data['tempat_lahir'] ?? ''),
            'tanggal_lahir' => $data['tanggal_lahir'] ?: null,
            'alamat' => sanitize($data['alamat'] ?? ''),
            'nama_ayah' => sanitize($data['nama_ayah'] ?? ''),
            'nama_ibu' => sanitize($data['nama_ibu'] ?? ''),
            'telepon_ortu' => sanitize($data['telepon_ortu'] ?? ''),
            'kelas_id' => intval($data['kelas_id']),
        ], 'id = :id', ['id' => $id]);
        setFlash('success', 'Data siswa berhasil diperbarui!');
        header('Location: list.php');
        exit;
    }
}

include '../../templates/header.php';
?>
<div class="card" style="max-width:800px;">
  <div class="card-header">
    <span class="card-title">Edit Siswa: <?= htmlspecialchars($siswa['nama']) ?></span>
    <a href="list.php" class="btn btn-outline btn-sm"><i class="fas fa-arrow-left"></i> Kembali</a>
  </div>
  <div class="card-body">
    <form method="POST">
      <div class="form-row cols-2">
        <div class="form-group">
          <label class="form-label">NIS <span class="required">*</span></label>
          <input type="text" name="nis" class="form-control" value="<?= htmlspecialchars($data['nis']) ?>" required>
        </div>
        <div class="form-group">
          <label class="form-label">NISN</label>
          <input type="text" name="nisn" class="form-control" value="<?= htmlspecialchars($data['nisn'] ?? '') ?>">
        </div>
      </div>
      <div class="form-group">
        <label class="form-label">Nama Lengkap <span class="required">*</span></label>
        <input type="text" name="nama" class="form-control" value="<?= htmlspecialchars($data['nama']) ?>" required>
      </div>
      <div class="form-row cols-3">
        <div class="form-group">
          <label class="form-label">Jenis Kelamin</label>
          <select name="jenis_kelamin" class="form-control">
            <option value="L" <?= $data['jenis_kelamin'] === 'L' ? 'selected' : '' ?>>Laki-laki</option>
            <option value="P" <?= $data['jenis_kelamin'] === 'P' ? 'selected' : '' ?>>Perempuan</option>
          </select>
        </div>
        <div class="form-group">
          <label class="form-label">Kelas</label>
          <select name="kelas_id" class="form-control">
            <?php foreach ($kelasList as $k): ?>
            <option value="<?= $k['id'] ?>" <?= $data['kelas_id'] == $k['id'] ? 'selected' : '' ?>><?= htmlspecialchars($k['nama_kelas']) ?></option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="form-group">
          <label class="form-label">Tanggal Lahir</label>
          <input type="date" name="tanggal_lahir" class="form-control" value="<?= $data['tanggal_lahir'] ?? '' ?>">
        </div>
      </div>
      <div class="form-row cols-2">
        <div class="form-group">
          <label class="form-label">Nama Ayah</label>
          <input type="text" name="nama_ayah" class="form-control" value="<?= htmlspecialchars($data['nama_ayah'] ?? '') ?>">
        </div>
        <div class="form-group">
          <label class="form-label">Nama Ibu</label>
          <input type="text" name="nama_ibu" class="form-control" value="<?= htmlspecialchars($data['nama_ibu'] ?? '') ?>">
        </div>
      </div>
      <div class="form-group">
        <label class="form-label">Alamat</label>
        <textarea name="alamat" class="form-control" rows="2"><?= htmlspecialchars($data['alamat'] ?? '') ?></textarea>
      </div>
      <div style="display:flex;gap:10px;margin-top:8px;">
        <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Simpan Perubahan</button>
        <a href="list.php" class="btn btn-outline">Batal</a>
      </div>
    </form>
  </div>
</div>
<?php include '../../templates/footer.php'; ?>