<script setup lang="ts">
import SearchableSelect from '@/Components/Common/SearchableSelect.vue';
import AdminLayout from '@/Layouts/AdminLayout.vue';
import admin from '@/routes/admin';
import { Head, Link, useForm } from '@inertiajs/vue3';
import { ChevronRight, Eye, EyeOff } from 'lucide-vue-next';
import { PropType, ref } from 'vue';

const showPassword = ref(false);
const showConfirmPassword = ref(false);

defineProps({
    roles: Array as PropType<any[]>,
});

const form = useForm({
    name: '',
    email: '',
    password: '',
    password_confirmation: '',
    role_id: '',
});

const submit = () => {
    form.post(admin.users.store.url(), {
        onSuccess: () => form.reset(),
    });
};
</script>

<template>
    <Head :title="__('Add New Member')" />

    <AdminLayout>
        <div class="animate-fade-in mx-auto max-w-2xl space-y-6">
            <div
                class="flex flex-col items-start justify-between gap-6 border-b border-slate-200 pb-6 sm:flex-row sm:items-center dark:border-slate-800"
            >
                <div>
                    <div class="mb-2 flex items-center gap-2 text-[10px] font-black tracking-widest text-slate-400 uppercase">
                        <Link :href="admin.users.index.url()" class="hover:text-brand-500">{{ __('Member Directory') }}</Link>
                        <ChevronRight :size="8" class="opacity-40 rtl:rotate-180" />
                        <span class="text-brand-500">{{ __('New Member') }}</span>
                    </div>
                    <h2 class="text-2xl font-extrabold tracking-tight text-slate-900 dark:text-white">{{ __('Add New Member') }}</h2>
                    <p class="mt-1 text-sm text-slate-500 dark:text-slate-400">{{ __('Create a new account for your organization.') }}</p>
                </div>
            </div>

            <div class="rounded-[2rem] border border-slate-200/60 bg-white p-6 shadow-sm dark:border-slate-700 dark:bg-slate-800">
                <form @submit.prevent="submit" class="space-y-6">
                    <div>
                        <label class="mb-2 block text-xs font-bold text-slate-600 dark:text-slate-400">{{ __('Full Name') }}</label>
                        <input
                            v-model="form.name"
                            type="text"
                            class="w-full rounded-2xl border border-slate-200 bg-slate-50 px-5 py-3 font-medium text-slate-900 transition-all outline-none placeholder:text-slate-400 focus:border-brand-500 focus:ring-4 focus:ring-brand-500/10 dark:border-slate-700 dark:bg-slate-900/50 dark:text-white dark:placeholder:text-slate-500"
                        />
                        <p v-if="form.errors.name" class="mt-1 text-xs text-red-500">{{ form.errors.name }}</p>
                    </div>

                    <div>
                        <label class="mb-2 block text-xs font-bold text-slate-600 dark:text-slate-400">{{ __('Email Address') }}</label>
                        <input
                            v-model="form.email"
                            type="email"
                            class="w-full rounded-2xl border border-slate-200 bg-slate-50 px-5 py-3 font-medium text-slate-900 transition-all outline-none placeholder:text-slate-400 focus:border-brand-500 focus:ring-4 focus:ring-brand-500/10 dark:border-slate-700 dark:bg-slate-900/50 dark:text-white dark:placeholder:text-slate-500"
                        />
                        <p v-if="form.errors.email" class="mt-1 text-xs text-red-500">{{ form.errors.email }}</p>
                    </div>

                    <div class="grid grid-cols-1 gap-6 sm:grid-cols-2">
                        <div>
                            <label class="mb-2 block text-xs font-bold text-slate-600 dark:text-slate-400">{{ __('Password') }}</label>
                            <div class="relative">
                                <input
                                    v-model="form.password"
                                    :type="showPassword ? 'text' : 'password'"
                                    class="w-full rounded-2xl border border-slate-200 bg-slate-50 px-5 py-3 pr-10 font-medium text-slate-900 transition-all outline-none placeholder:text-slate-400 focus:border-brand-500 focus:ring-4 focus:ring-brand-500/10 dark:border-slate-700 dark:bg-slate-900/50 dark:text-white dark:placeholder:text-slate-500"
                                />
                                <button
                                    type="button"
                                    @click="showPassword = !showPassword"
                                    class="absolute inset-y-0 right-0 flex items-center px-3 text-slate-400 hover:text-slate-600 dark:hover:text-slate-300"
                                >
                                    <Eye v-if="!showPassword" class="h-5 w-5" />
                                    <EyeOff v-else class="h-5 w-5" />
                                </button>
                            </div>
                            <p v-if="form.errors.password" class="mt-1 text-xs text-red-500">{{ form.errors.password }}</p>
                        </div>
                        <div>
                            <label class="mb-2 block text-xs font-bold text-slate-600 dark:text-slate-400">{{ __('Confirm Password') }}</label>
                            <div class="relative">
                                <input
                                    v-model="form.password_confirmation"
                                    :type="showConfirmPassword ? 'text' : 'password'"
                                    class="w-full rounded-2xl border border-slate-200 bg-slate-50 px-5 py-3 pr-10 font-medium text-slate-900 transition-all outline-none placeholder:text-slate-400 focus:border-brand-500 focus:ring-4 focus:ring-brand-500/10 dark:border-slate-700 dark:bg-slate-900/50 dark:text-white dark:placeholder:text-slate-500"
                                />
                                <button
                                    type="button"
                                    @click="showConfirmPassword = !showConfirmPassword"
                                    class="absolute inset-y-0 right-0 flex items-center px-3 text-slate-400 hover:text-slate-600 dark:hover:text-slate-300"
                                >
                                    <Eye v-if="!showConfirmPassword" class="h-5 w-5" />
                                    <EyeOff v-else class="h-5 w-5" />
                                </button>
                            </div>
                        </div>
                    </div>

                    <div>
                        <label class="mb-2 block text-xs font-bold text-slate-600 dark:text-slate-400">{{ __('Access Role') }}</label>
                        <SearchableSelect v-model="form.role_id" :options="roles" value-key="id" label-key="name" :placeholder="__('Select Role')" />
                        <p v-if="form.errors.role_id" class="mt-1 text-xs text-red-500">{{ form.errors.role_id }}</p>
                    </div>

                    <div class="flex justify-end gap-3 pt-4">
                        <Link
                            :href="admin.users.index.url()"
                            class="rounded-2xl border border-slate-200 px-6 py-3 text-center text-sm font-bold text-slate-500 transition-all hover:bg-slate-50 dark:border-slate-800 dark:hover:bg-slate-800"
                        >
                            {{ __('Cancel') }}
                        </Link>
                        <button
                            type="submit"
                            :disabled="form.processing"
                            class="rounded-2xl bg-brand-600 px-8 py-3 text-sm font-bold text-white shadow-xl shadow-brand-600/20 transition-all hover:bg-brand-700 disabled:opacity-50"
                        >
                            {{ form.processing ? __('Saving...') : __('Create Member') }}
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </AdminLayout>
</template>
