<?php
require_once '../../includes/functions.php';
requireAdmin();
$id = intval($_GET['id'] ?? 0);
$guru = db_fetch("SELECT g.*, u.username, u.email FROM guru g JOIN users u ON u.id = g.user_id WHERE g.id = ?", [$id]);
if (!$guru) { setFlash('danger', 'Data tidak ditemukan'); header('Location: list.php'); exit; }
$pageTitle = 'Edit Guru';
$activeMenu = 'guru';
$data = $guru;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = $_POST;
    db_update('guru', [
        'nama'          => sanitize($data['nama']),
        'nip'           => sanitize($data['nip'] ?? ''),
        'jenis_kelamin' => $data['jenis_kelamin'],
        'telepon'       => sanitize($data['telepon'] ?? ''),
        'email'         => sanitize($data['email'] ?? ''),
        'is_bk'         => isset($data['is_bk']) ? 1 : 0,
    ], 'id = :id', ['id' => $id]);
    db_update('users', ['nama_lengkap' => sanitize($data['nama']), 'email' => sanitize($data['email'] ?? '')], 'id = :id', ['id' => $guru['user_id']]);
    if (!empty($data['password'])) {
        db_update('users', ['password' => password_hash($data['password'], PASSWORD_DEFAULT)], 'id = :id', ['id' => $guru['user_id']]);
    }
    setFlash('success', 'Data guru berhasil diperbarui!');
    header('Location: list.php'); exit;
}

include '../../templates/header.php';
?>
<div class="card" style="max-width:700px;">
  <div class="card-header">
    <span class="card-title">Edit Guru: <?= htmlspecialchars($guru['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-group">
        <label class="form-label">Nama Lengkap</label>
        <input type="text" name="nama" class="form-control" value="<?= htmlspecialchars($data['nama']) ?>" required>
      </div>
      <div class="form-row cols-2">
        <div class="form-group">
          <label class="form-label">NIP</label>
          <input type="text" name="nip" class="form-control" value="<?= htmlspecialchars($data['nip'] ?? '') ?>">
        </div>
        <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>
      <div class="form-row cols-2">
        <div class="form-group">
          <label class="form-label">Email</label>
          <input type="email" name="email" class="form-control" value="<?= htmlspecialchars($data['email'] ?? '') ?>">
        </div>
        <div class="form-group">
          <label class="form-label">Telepon</label>
          <input type="text" name="telepon" class="form-control" value="<?= htmlspecialchars($data['telepon'] ?? '') ?>">
        </div>
      </div>
      <div class="form-group">
        <label class="form-label">Password Baru <span style="color:var(--text-muted);font-weight:400;">(kosongkan jika tidak diubah)</span></label>
        <input type="password" name="password" class="form-control" placeholder="Isi untuk mengganti password">
      </div>

      <!-- Peran Khusus -->
      <div class="form-group">
        <label class="form-label">Peran Khusus</label>
        <div style="display:flex;gap:16px;flex-wrap:wrap;padding:12px 16px;background:var(--surface-2);border:1px solid var(--border);border-radius:8px;">
          <label style="display:flex;align-items:center;gap:8px;cursor:pointer;font-size:13px;">
            <input type="checkbox" name="is_bk" value="1"
                   <?= !empty($data['is_bk']) ? 'checked' : '' ?>
                   style="width:16px;height:16px;accent-color:var(--primary);">
            <span>
              <i class="fas fa-user-md" style="color:var(--warning);margin-right:4px;"></i>
              <strong>Guru BK</strong>
              <span style="color:var(--text-muted);font-size:11px;margin-left:4px;">(dapat mengisi absensi semua kelas)</span>
            </span>
          </label>
        </div>
        <div style="font-size:11px;color:var(--text-muted);margin-top:6px;">
          <i class="fas fa-info-circle"></i>
          Wali kelas diatur melalui menu <strong>Data Kelas → Edit</strong>.
        </div>
      </div>
      <div style="display:flex;gap:10px;">
        <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Simpan</button>
        <a href="list.php" class="btn btn-outline">Batal</a>
      </div>
    </form>
  </div>
</div>
<?php include '../../templates/footer.php'; ?>
