<?php
/**
 * Admin User Create/Edit Form View
 * Used for both creating a new user and editing an existing one by an administrator.
 */
$isEdit = !empty($user['id']);
$roleColors = ['student' => 'primary', 'mentor' => 'success', 'tutor' => 'purple', 'admin' => 'danger'];
$roleColor = $isEdit ? ($roleColors[$user['role']] ?? 'secondary') : 'primary';
?>
<div class="container-fluid py-4 px-4">
    <!-- Breadcrumb -->
    <nav aria-label="breadcrumb" class="mb-4">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="<?= APP_URL ?>/admin" class="text-decoration-none text-primary">Admin</a></li>
            <li class="breadcrumb-item"><a href="<?= APP_URL ?>/admin/users" class="text-decoration-none text-primary">Users</a></li>
            <li class="breadcrumb-item active text-muted"><?= $isEdit ? 'Edit ' . htmlspecialchars($user['first_name'] . ' ' . $user['last_name']) : 'Add User' ?></li>
        </ol>
    </nav>

    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="fw-bold font-outfit text-dark m-0">
                <i class="fa-solid fa-user-plus text-primary me-2"></i>
                <?= $isEdit ? 'Edit User Details' : 'Add New User' ?>
            </h2>
            <p class="text-muted mb-0"><?= $isEdit ? 'Modify system access and details for ' . htmlspecialchars($user['first_name'] . ' ' . $user['last_name']) : 'Enter details to create a new user profile' ?></p>
        </div>
        <a href="<?= APP_URL ?>/admin/users" class="btn btn-outline-secondary rounded-pill px-4 font-outfit fw-bold">
            <i class="fa-solid fa-arrow-left me-2"></i>Back to List
        </a>
    </div>

    <form action="<?= APP_URL ?>/admin/users<?= $isEdit ? '/' . $user['id'] . '/edit' : '/create' ?>" method="POST" enctype="multipart/form-data">
        <?= csrf_field() ?>

        <div class="row g-4">
            <!-- Left Panel: Core User Info -->
            <div class="col-lg-8">
                <div class="card border-0 shadow-sm mb-4">
                    <div class="card-body p-4">
                        <h5 class="fw-bold font-outfit text-dark mb-4"><i class="fa-solid fa-id-card text-info me-2"></i>Core Identity Details</h5>
                        
                        <div class="row g-3">
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">First Name <span class="text-danger">*</span></label>
                                <input type="text" name="first_name" class="form-control" required value="<?= htmlspecialchars($user['first_name'] ?? '') ?>" placeholder="e.g. John">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Last Name <span class="text-danger">*</span></label>
                                <input type="text" name="last_name" class="form-control" required value="<?= htmlspecialchars($user['last_name'] ?? '') ?>" placeholder="e.g. Doe">
                            </div>
                            <div class="col-12">
                                <label class="form-label fw-bold font-outfit text-sm">Email Address <span class="text-danger">*</span></label>
                                <input type="email" name="email" class="form-control" required value="<?= htmlspecialchars($user['email'] ?? '') ?>" placeholder="name@example.com">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Phone Number</label>
                                <input type="tel" name="phone" class="form-control" value="<?= htmlspecialchars($user['phone'] ?? '') ?>" placeholder="e.g. +233501234567">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Gender</label>
                                <select name="gender" class="form-select">
                                    <option value="">Select Gender</option>
                                    <option value="male" <?= ($user['gender'] ?? '') === 'male' ? 'selected' : '' ?>>Male</option>
                                    <option value="female" <?= ($user['gender'] ?? '') === 'female' ? 'selected' : '' ?>>Female</option>
                                    <option value="other" <?= ($user['gender'] ?? '') === 'other' ? 'selected' : '' ?>>Other</option>
                                    <option value="prefer_not" <?= ($user['gender'] ?? '') === 'prefer_not' ? 'selected' : '' ?>>Prefer Not to Say</option>
                                </select>
                            </div>
                            
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Password <?= $isEdit ? '(Leave blank to keep current)' : '<span class="text-danger">*</span>' ?></label>
                                <input type="password" name="password" class="form-control" <?= $isEdit ? '' : 'required' ?> minlength="8" placeholder="Minimum 8 characters">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold font-outfit text-sm">Confirm Password <?= $isEdit ? '' : '<span class="text-danger">*</span>' ?></label>
                                <input type="password" name="password_confirm" class="form-control" <?= $isEdit ? '' : 'required' ?> placeholder="Repeat password">
                            </div>
                        </div>
                    </div>
                </div>
        </div>

            <!-- Right Panel: Authorization & Controls -->
            <div class="col-lg-4">
                <div class="card border-0 shadow-sm mb-4">
                    <div class="card-body p-4">
                        <h6 class="fw-bold font-outfit text-dark mb-4"><i class="fa-solid fa-shield-halved text-primary me-2"></i>Access Level & State</h6>
                        
                        <div class="mb-4">
                            <label class="form-label fw-bold font-outfit text-sm">System Role <span class="text-danger">*</span></label>
                            <select name="role" class="form-select" required>
                                <option value="student" <?= ($user['role'] ?? 'student') === 'student' ? 'selected' : '' ?>>Student</option>
                                <option value="mentor" <?= ($user['role'] ?? '') === 'mentor' ? 'selected' : '' ?>>Mentor</option>
                                <option value="tutor" <?= ($user['role'] ?? '') === 'tutor' ? 'selected' : '' ?>>Tutor</option>
                                <option value="admin" <?= ($user['role'] ?? '') === 'admin' ? 'selected' : '' ?>>Administrator</option>
                            </select>
                        </div>

                        <div class="mb-4">
                            <label class="form-label fw-bold font-outfit text-sm">Account Status <span class="text-danger">*</span></label>
                            <select name="status" class="form-select" required>
                                <option value="active" <?= ($user['status'] ?? 'active') === 'active' ? 'selected' : '' ?>>Active</option>
                                <option value="inactive" <?= ($user['status'] ?? '') === 'inactive' ? 'selected' : '' ?>>Inactive</option>
                                <option value="suspended" <?= ($user['status'] ?? '') === 'suspended' ? 'selected' : '' ?>>Suspended</option>
                                <option value="pending" <?= ($user['status'] ?? '') === 'pending' ? 'selected' : '' ?>>Pending Verification</option>
                            </select>
                        </div>

                        <div class="mb-3 form-check form-switch">
                            <input class="form-check-input" type="checkbox" name="email_verified" id="email_verified" value="1" <?= ($user['email_verified'] ?? 1) ? 'checked' : '' ?>>
                            <label class="form-check-label font-outfit fw-bold text-sm" for="email_verified">Email Verified</label>
                        </div>
                    </div>
                </div>

                <div class="d-grid gap-2">
                    <button type="submit" class="btn btn-primary rounded-pill py-2 font-outfit fw-bold">
                        <i class="fa-solid fa-floppy-disk me-2"></i><?= $isEdit ? 'Update Account' : 'Create Account' ?>
                    </button>
                    <a href="<?= APP_URL ?>/admin/users" class="btn btn-outline-secondary rounded-pill py-2 font-outfit fw-bold">Cancel</a>
                </div>
            </div>
        </div>
    </form>
</div>
