<?php
require __DIR__ . '/_bootstrap.php';

$id = (int)($_GET['id'] ?? 0);
$stmt = $pdo->prepare("SELECT * FROM students WHERE id=?");
$stmt->execute([$id]);
$data = $stmt->fetch();
if(!$data){ http_response_code(404); echo "Not found"; exit; }

include __DIR__ . '/_header.php';
?>
<div class="page-header"><h1>Edit Student</h1></div>

<div class="card">
  <form action="<?php echo url_for('/admin/student_edit_post.php?id=' . $data['id']); ?>" method="post" enctype="multipart/form-data" class="form-grid">
    <div class="field">
      <label>ID Number</label>
      <input type="text" name="id_number" value="<?php echo htmlspecialchars($data['id_number']); ?>" required>
    </div>

    <div class="field">
      <label>Student Number</label>
      <input type="text" name="student_number" value="<?php echo htmlspecialchars($data['student_number']); ?>" required>
    </div>

    <div class="field">
      <label>First Name</label>
      <input type="text" name="first_name" value="<?php echo htmlspecialchars($data['first_name']); ?>" required>
    </div>

    <div class="field">
      <label>Last Name</label>
      <input type="text" name="last_name" value="<?php echo htmlspecialchars($data['last_name']); ?>" required>
    </div>

    <div class="field">
      <label>Place of Birth</label>
      <input type="text" name="place_of_birth" value="<?php echo htmlspecialchars($data['place_of_birth']); ?>" required>
    </div>

    <div class="field">
      <label>Date of Birth</label>
      <input type="date" name="date_of_birth" value="<?php echo htmlspecialchars($data['date_of_birth']); ?>" required>
    </div>

    <div class="field">
      <label>Nationality</label>
      <input type="text" name="nationality" value="<?php echo htmlspecialchars($data['nationality']); ?>" required>
    </div>

    <div class="field">
      <label>Email</label>
      <input type="email" name="email" value="<?php echo htmlspecialchars($data['email']); ?>" required>
    </div>

<div class="field">
  <label>Password <span class="help">(leave blank to keep)</span></label>
  <div class="pw-wrap">
    <input type="password" name="password" id="std_password_edit" autocomplete="new-password">
    <button type="button" class="pw-eye" aria-label="Show password" data-target="std_password_edit">
      <svg class="icon-eye" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
        <path d="M1 12s4-7 11-7 11 7 11 7-4 7-11 7S1 12 1 12Z"/>
        <circle cx="12" cy="12" r="3"/>
      </svg>
      <svg class="icon-eye-off" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
        <path d="M17.94 17.94A10.94 10.94 0 0 1 12 19c-7 0-11-7-11-7a21.91 21.91 0 0 1 5.06-5.94M9.9 4.24A10.94 10.94 0 0 1 12 4c7 0 11 7 11 7a21.83 21.83 0 0 1-3.17 4.33"/>
        <line x1="1" y1="1" x2="23" y2="23"/>
      </svg>
    </button>
  </div>
</div>



    <!-- admin/student_edit.php -->
<div class="field row-span">
  <label>Document</label>
  <textarea name="document_text" rows="1" placeholder="Student Document"><?php
    echo htmlspecialchars($data['document_text'] ?? '');
  ?></textarea>
</div>



    <div class="actions-bar">
      <!--<a class="btn" href="<?php echo url_for('/admin/student_view.php?id=' . $data['id']); ?>">Cancel</a>-->
      <a class="btn" href="<?php echo url_for('/admin/students.php'); ?>">Cancel</a>
      <button class="btn btn-primary" type="submit">Update</button>
    </div>
  </form>
</div>
<?php include __DIR__ . '/_footer.php'; ?>
