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

// ตรวจสอบ id
if (!isset($_GET['id'])) {
    header("Location: member_manage.php");
    exit;
}

$id = intval($_GET['id']);

// บันทึกข้อมูล
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    requireValidCsrf();

    $prefix          = trim($_POST['prefix']);
    $first_name      = trim($_POST['first_name']);
    $last_name       = trim($_POST['last_name']);
    $gender          = trim($_POST['gender']);
    $faculty         = trim($_POST['faculty']);
    $major           = trim($_POST['major']);
    $graduation_year = trim($_POST['graduation_year']);
    $phone           = trim($_POST['phone']);
    $email           = trim($_POST['email']);
    $address         = trim($_POST['address']);
    $workplace       = trim($_POST['workplace']);
    $position        = trim($_POST['position']);

    $sql = "UPDATE members SET
            prefix=?,
            first_name=?,
            last_name=?,
            gender=?,
            faculty=?,
            major=?,
            graduation_year=?,
            phone=?,
            email=?,
            address=?,
            workplace=?,
            position=?
            WHERE id=?";

    $stmt = $conn->prepare($sql);

    $stmt->bind_param(
        "ssssssssssssi",
        $prefix,
        $first_name,
        $last_name,
        $gender,
        $faculty,
        $major,
        $graduation_year,
        $phone,
        $email,
        $address,
        $workplace,
        $position,
        $id
    );

    if ($stmt->execute()) {

        header("Location: member_manage.php");
        exit;

    }

}

// ดึงข้อมูลสมาชิก
$stmt = $conn->prepare("SELECT * FROM members WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->execute();

$result = $stmt->get_result();

if ($result->num_rows == 0) {
    die("ไม่พบข้อมูลสมาชิก");
}

$row = $result->fetch_assoc();

include '../includes/header.php';
?>

<section class="form-section">

<div class="container">

<h2>✏️ แก้ไขข้อมูลสมาชิก</h2>

<form method="post">
<input type="hidden" name="csrf_token" value="<?= htmlspecialchars(csrfToken()) ?>">

<label>คำนำหน้า</label>

<input
type="text"
name="prefix"
value="<?= htmlspecialchars($row['prefix']) ?>">

<label>ชื่อ</label>

<input
type="text"
name="first_name"
value="<?= htmlspecialchars($row['first_name']) ?>">

<label>นามสกุล</label>

<input
type="text"
name="last_name"
value="<?= htmlspecialchars($row['last_name']) ?>">

<label>เพศ</label>

<input
type="text"
name="gender"
value="<?= htmlspecialchars($row['gender']) ?>">

<label>คณะ</label>

<input
type="text"
name="faculty"
value="<?= htmlspecialchars($row['faculty']) ?>">

<label>สาขา</label>

<input
type="text"
name="major"
value="<?= htmlspecialchars($row['major']) ?>">

<label>ปีที่จบ</label>

<input
type="text"
name="graduation_year"
value="<?= htmlspecialchars($row['graduation_year']) ?>">

<label>โทรศัพท์</label>

<input
type="text"
name="phone"
value="<?= htmlspecialchars($row['phone']) ?>">

<label>Email</label>

<input
type="email"
name="email"
value="<?= htmlspecialchars($row['email']) ?>">

<label>ที่อยู่</label>

<textarea
name="address"><?= htmlspecialchars($row['address']) ?></textarea>

<label>สถานที่ทำงาน</label>

<input
type="text"
name="workplace"
value="<?= htmlspecialchars($row['workplace']) ?>">

<label>ตำแหน่ง</label>

<input
type="text"
name="position"
value="<?= htmlspecialchars($row['position']) ?>">

<div class="mt-4">

<button
type="submit"
class="btn btn-primary">

บันทึกข้อมูล

</button>

<a
href="member_manage.php"
class="btn btn-secondary">

ยกเลิก

</a>

</div>

</form>

</div>

</section>

<?php include '../includes/footer.php'; ?>
