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

require_once 'includes/config.php';

// ตรวจสอบ id
if (!isset($_GET['id']) || empty($_GET['id'])) {
    die("ไม่พบข้อมูลสมาชิก");
}

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

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

$result = $stmt->get_result();

if ($result->num_rows == 0) {

    $stmt->close();

    die("ไม่พบข้อมูลสมาชิก");
}

$member = $result->fetch_assoc();

$stmt->close();

include 'includes/header.php';
?>

<section class="member-section">

    <div class="container">

        <div class="detail-card">

            <div class="detail-avatar text-center mb-4">

                <?php
                $photoPath = "uploads/profiles/" . $member['photo'];

                if (!empty($member['photo']) && file_exists(__DIR__ . "/" . $photoPath)):
                ?>

                    <img
                        src="<?= $photoPath; ?>"
                        alt="รูปสมาชิก"
                        class="rounded-circle shadow"
                        style="
                            width:220px;
                            height:220px;
                            object-fit:cover;
                            border:5px solid #ffffff;
                        ">

                <?php else: ?>

                    <div
                        class="rounded-circle bg-secondary text-white d-inline-flex justify-content-center align-items-center shadow"
                        style="
                            width:220px;
                            height:220px;
                            font-size:80px;
                        ">

                        👤

                    </div>

                <?php endif; ?>

            </div>

            <h2>

                <?= htmlspecialchars($member['prefix'] . " " . $member['first_name'] . " " . $member['last_name']) ?>

            </h2>

            <div class="badge-major">

                <?= htmlspecialchars($member['major']) ?>

            </div>

            <table class="table table-bordered mt-4">

                <tr>
                    <th width="220">รหัสนักศึกษา</th>
                    <td><?= htmlspecialchars($member['student_id']) ?></td>
                </tr>

                <tr>
                    <th>เพศ</th>
                    <td><?= htmlspecialchars($member['gender']) ?></td>
                </tr>

                <tr>
                    <th>คณะ</th>
                    <td><?= htmlspecialchars($member['faculty']) ?></td>
                </tr>

                <tr>
                    <th>สาขาวิชา</th>
                    <td><?= htmlspecialchars($member['major']) ?></td>
                </tr>

                <tr>
                    <th>ปีที่จบ</th>
                    <td><?= htmlspecialchars($member['graduation_year']) ?></td>
                </tr>

                <tr>
                    <th>โทรศัพท์</th>
                    <td><?= htmlspecialchars($member['phone']) ?></td>
                </tr>

                <tr>
                    <th>Email</th>
                    <td><?= htmlspecialchars($member['email']) ?></td>
                </tr>

                <tr>
                    <th>ที่อยู่</th>
                    <td><?= nl2br(htmlspecialchars($member['address'])) ?></td>
                </tr>

                <tr>
                    <th>สถานที่ทำงาน</th>
                    <td><?= htmlspecialchars($member['workplace']) ?></td>
                </tr>

                <tr>
                    <th>ตำแหน่ง</th>
                    <td><?= htmlspecialchars($member['position']) ?></td>
                </tr>

            </table>

            <div class="text-center mt-4">

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

                    ← กลับหน้าทำเนียบสมาชิก

                </a>

            </div>

        </div>

    </div>

</section>

<?php
$conn->close();
include 'includes/footer.php';
?>