<?php

session_start();

if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {

    echo '<p class="text-danger">Unauthorized access</p>';

    exit;

}



require_once '../config.php';

$conn = getDBConnection();



$action = $_GET['action'] ?? '';

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



if ($action === 'edit' && $id > 0) {

    $result = $conn->query("SELECT id, username, full_name, email, phone, role, is_active FROM users WHERE id = $id");

    $user = $result ? $result->fetch_assoc() : null;

    

    if ($user) {

        ?>

        <input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">

        <div class="mb-3">

            <label class="form-label">Username</label>

            <input type="text" class="form-control" value="<?php echo htmlspecialchars($user['username']); ?>" disabled>

            <small class="text-muted">Username cannot be changed</small>

        </div>

        <div class="mb-3">

            <label class="form-label">Full Name *</label>

            <input type="text" class="form-control" name="full_name" value="<?php echo htmlspecialchars($user['full_name']); ?>" required>

        </div>

        <div class="mb-3">

            <label class="form-label">Email *</label>

            <input type="email" class="form-control" name="email" value="<?php echo htmlspecialchars($user['email']); ?>" required>

        </div>

        <div class="mb-3">

            <label class="form-label">Phone</label>

            <input type="tel" class="form-control" name="phone" value="<?php echo htmlspecialchars($user['phone']); ?>">

        </div>

        <div class="mb-3">

            <label class="form-label">Role *</label>

            <select class="form-select" name="role" required>

                <option value="admin"  <?php echo $user['role'] === 'admin' ? 'selected' : ''; ?>>Admin</option>

                                <option value="headteacher"  <?php echo $user['role'] === 'headteacher' ? 'selected' : ''; ?>>Head Teacher/Mistress</option>

                                <option value="accountant"  <?php echo $user['role'] === 'accountant' ? 'selected' : ''; ?>>Accountant</option>

                                <option value="cashier"  <?php echo $user['role'] === 'cashier' ? 'selected' : ''; ?>>Cashier</option>

                                <option value="registrar"  <?php echo $user['role'] === 'registrar' ? 'selected' : ''; ?>>Registrar</option>

                                 <option value="supervisor"  <?php echo $user['role'] === 'supervisor' ? 'selected' : ''; ?>>Supervisor</option>

                                  <option value="director"  <?php echo $user['role'] === 'director' ? 'selected' : ''; ?>>Director</option>

                                  <option value="tutor"  <?php echo $user['role'] === 'tutor' ? 'selected' : ''; ?>>Tutor</option>
                                  <option value="nurse"  <?php echo $user['role'] === 'nurse' ? 'selected' : ''; ?>>Nurse</option>

                                  <option value="cook"  <?php echo $user['role'] === 'cook' ? 'selected' : ''; ?>>Cook</option>

                                   <option value="assist. cook"  <?php echo $user['role'] === 'assist. cook' ? 'selected' : ''; ?>>Assistant Cook</option>

                                   <option value="principal"  <?php echo $user['role'] === 'principal' ? 'selected' : ''; ?>>Principal</option>

            </select>

        </div>

        <div class="mb-3">

            <div class="form-check">

                <input class="form-check-input" type="checkbox" name="is_active" id="is_active" value="1" <?php echo $user['is_active'] ? 'checked' : ''; ?>>

                <label class="form-check-label" for="is_active">

                    Active Account

                </label>

            </div>

        </div>

        <?php

    } else {

        echo '<p class="text-danger">User not found</p>';

    }

}



$conn->close();

?>