<template>
    <teleport to="body">
        <div class="modal fade show d-block admin-modal" tabindex="-1" role="dialog" aria-modal="true" @click.self="emit('close')">
            <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Edit User</h5>
                        <button type="button" class="btn-close" aria-label="Close" @click="emit('close')"></button>
                    </div>
                    <form @submit.prevent="handleSubmit">
                        <div class="modal-body">
                            <div class="mb-3">
                                <label class="form-label">Username</label>
                                <input class="form-control" :value="user?.username || ''" type="text" disabled />
                            </div>

                            <div class="mb-3">
                                <label class="form-label">Email</label>
                                <input class="form-control" :value="user?.email || ''" type="text" disabled />
                            </div>

                            <div class="mb-3">
                                <label class="form-label">Status</label>
                                <input class="form-control" :value="user?.is_active ? 'Active' : 'Inactive'" type="text" disabled />
                            </div>

                            <div>
                                <label class="form-label">Groups</label>
                                <select v-model="groupIds" class="form-select" multiple>
                                    <option v-for="group in groups" :key="group.id" :value="group.id">
                                        {{ group.name }}
                                    </option>
                                </select>
                                <div class="small text-muted mt-1">Ctrl/Cmd+Click for multiple groups</div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-outline-secondary" @click="emit('close')">Cancel</button>
                            <button type="submit" class="btn btn-primary" :disabled="submitting">
                                {{ submitting ? "Saving..." : "Save Changes" }}
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div class="modal-backdrop fade show admin-modal-backdrop"></div>
    </teleport>
</template>

<script setup>
import { ref, watch } from "vue";

const props = defineProps({
    user: {
        type: Object,
        default: null,
    },
    groups: {
        type: Array,
        default: () => [],
    },
    submitting: {
        type: Boolean,
        default: false,
    },
});

const emit = defineEmits(["close", "submit"]);

const groupIds = ref([]);

watch(
    () => props.user,
    (user) => {
        groupIds.value = [...(user?.group_ids || [])];
    },
    { immediate: true },
);

const handleSubmit = () => {
    emit("submit", { group_ids: groupIds.value });
};
</script>

<style scoped>
.admin-modal {
    z-index: 2000;
    overflow-y: auto;
    padding-top: max(0.5rem, env(safe-area-inset-top));
}

.admin-modal-backdrop {
    z-index: 1990;
}

.admin-modal .modal-dialog {
    margin: 1rem auto;
}

@media (max-width: 767.98px) {
    .admin-modal {
        padding-top: max(0.75rem, env(safe-area-inset-top));
    }

    .admin-modal .modal-dialog {
        margin: 0.75rem;
        max-width: none;
    }
}
</style>
