<?php
// File: /admin/ucf_team_manage.php
require_once '../session_init.php';
require_once '../db.php';

// Security check
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    die("Access Denied");
}

// Handle editing
$edit_id = isset($_GET['edit']) ? (int)$_GET['edit'] : 0;
$edit_data = null;
if ($edit_id > 0) {
    $stmt = $conn->prepare("SELECT * FROM ucf_team WHERE id = ?");
    $stmt->bind_param("i", $edit_id);
    $stmt->execute();
    $edit_data = $stmt->get_result()->fetch_assoc();
    $stmt->close();
}

// Fetch all existing members
$result = $conn->query("SELECT * FROM ucf_team ORDER BY role_level, id ASC");
$members = $result ? $result->fetch_all(MYSQLI_ASSOC) : [];
?>

<?php include 'header.php'; ?>

<div class="p-6 bg-gray-50 min-h-screen">
    <div class="max-w-6xl mx-auto">
        <h1 class="text-3xl font-extrabold text-ucf-charcoal mb-6">Manage UCF Team</h1>

        <!-- ✅ Add/Edit Form -->
        <div class="bg-white shadow-md rounded-lg p-6 mb-10">
            <h2 class="text-2xl font-bold text-ucf-green mb-4">
                <?php echo $edit_data ? "Edit Team Member" : "Add New Team Member"; ?>
            </h2>

            <form action="ucf_team_handler.php" method="POST" enctype="multipart/form-data" class="space-y-5">
                <input type="hidden" name="id" value="<?php echo $edit_data['id'] ?? ''; ?>">

                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label class="block text-sm font-medium text-gray-700">Name *</label>
                        <input type="text" name="name" required class="mt-1 block w-full rounded-md border-gray-300"
                               value="<?php echo htmlspecialchars($edit_data['name'] ?? ''); ?>">
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-700">Role Level *</label>
                        <select name="role_level" required class="mt-1 block w-full rounded-md border-gray-300">
                            <?php
                            $levels = ['Core Panel', 'Advisors Panel', 'Executive Panel', 'Member'];
                            foreach ($levels as $level) {
                                $sel = (isset($edit_data['role_level']) && $edit_data['role_level'] === $level) ? 'selected' : '';
                                echo "<option value='$level' $sel>$level</option>";
                            }
                            ?>
                        </select>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-700">Designation *</label>
                        <input type="text" name="designation" required class="mt-1 block w-full rounded-md border-gray-300"
                               value="<?php echo htmlspecialchars($edit_data['designation'] ?? ''); ?>">
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-700">Photo</label>
                        <input type="file" name="photo" accept="image/*" class="mt-1 block w-full text-sm">
                        <?php if (!empty($edit_data['photo_path'])): ?>
                            <img src="<?php echo htmlspecialchars($edit_data['photo_path']); ?>" class="mt-3 h-24 rounded shadow">
                        <?php endif; ?>
                    </div>
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700">Description</label>
                    <textarea name="description" rows="4"
                              class="mt-1 block w-full rounded-md border-gray-300"><?php echo htmlspecialchars($edit_data['description'] ?? ''); ?></textarea>
                </div>

                <div class="flex items-center justify-between">
                    <button type="submit" name="<?php echo $edit_data ? 'update' : 'add'; ?>"
                            class="bg-ucf-green hover:bg-ucf-green-dark text-white px-5 py-2 rounded-md">
                        <?php echo $edit_data ? 'Update Member' : 'Add Member'; ?>
                    </button>
                    <?php if ($edit_data): ?>
                        <a href="ucf_team_manage.php" class="text-gray-500 hover:text-ucf-green">Cancel Edit</a>
                    <?php endif; ?>
                </div>
            </form>
        </div>

        <!-- ✅ Members List -->
        <div class="bg-white shadow-md rounded-lg p-6">
            <h2 class="text-2xl font-bold text-ucf-charcoal mb-4">Current Team Members</h2>

            <?php if (!empty($members)): ?>
                <div class="overflow-x-auto">
                    <table class="min-w-full border border-gray-200 divide-y divide-gray-200">
                        <thead class="bg-ucf-green text-white">
                            <tr>
                                <th class="py-3 px-4 text-left text-sm font-semibold">Photo</th>
                                <th class="py-3 px-4 text-left text-sm font-semibold">Name</th>
                                <th class="py-3 px-4 text-left text-sm font-semibold">Level</th>
                                <th class="py-3 px-4 text-left text-sm font-semibold">Designation</th>
                                <th class="py-3 px-4 text-left text-sm font-semibold">Actions</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-100">
                            <?php foreach ($members as $member): ?>
                                <tr class="hover:bg-gray-50">
                                    <td class="py-3 px-4">
                                        <?php if ($member['photo_path']): ?>
                                            <img src="<?php echo htmlspecialchars($member['photo_path']); ?>" class="h-12 w-12 rounded-full object-cover">
                                        <?php else: ?>
                                            <span class="text-gray-400">—</span>
                                        <?php endif; ?>
                                    </td>
                                    <td class="py-3 px-4 font-medium text-gray-800"><?php echo htmlspecialchars($member['name']); ?></td>
                                    <td class="py-3 px-4 text-gray-600"><?php echo htmlspecialchars($member['role_level']); ?></td>
                                    <td class="py-3 px-4 text-gray-600"><?php echo htmlspecialchars($member['designation']); ?></td>
                                    <td class="py-3 px-4">
                                        <a href="ucf_team_manage.php?edit=<?php echo $member['id']; ?>"
                                           class="text-blue-600 hover:underline font-semibold mr-3">Edit</a>
                                        <a href="ucf_team_handler.php?delete=<?php echo $member['id']; ?>"
                                           onclick="return confirm('Are you sure you want to delete this member?');"
                                           class="text-red-600 hover:underline font-semibold">Delete</a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php else: ?>
                <p class="text-gray-500">No team members found.</p>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
