<script setup lang="ts">
import { Head, useForm } from '@inertiajs/vue3';
import { route } from 'ziggy-js';
import AppLayout from '@/layouts/AppLayout.vue';

const props = defineProps<{
    dynamic: {
        id: number;
        name: string;
    };
}>();

const form = useForm({
    email: '',
    role: 'participant',
});

const breadcrumbs = [
    {
        name: 'Dynamics',
        href: route('dynamics.index'),
    },
    {
        name: props.dynamic.name,
        href: route('dynamics.show', props.dynamic.id),
    },
    {
        name: 'Invite User',
        href: route('dynamics.invitations.create', props.dynamic.id),
    },
];

function submit() {
    form.post(route('dynamics.invitations.store', props.dynamic.id), {
        onSuccess: () => form.reset(),
    });
}
</script>

<template>
    <Head title="Invite User" />

    <div class="c-invite-user">
        <div class="c-invite-user__container">
            <div class="c-invite-user__card">
                <div class="c-invite-user__body">
                    <h3 class="c-invite-user__title">
                        Invite User to {{ dynamic.name }}
                    </h3>

                    <form
                        @submit.prevent="submit"
                        class="c-invite-user__form"
                    >
                        <div class="c-invite-user__field">
                            <label
                                for="email"
                                class="c-invite-user__label"
                                >Email Address</label
                            >
                            <input
                                v-model="form.email"
                                id="email"
                                type="email"
                                required
                                class="c-invite-user__input"
                            />
                            <div
                                v-if="form.errors.email"
                                class="c-invite-user__error"
                            >
                                {{ form.errors.email }}
                            </div>
                        </div>

                        <div class="c-invite-user__field">
                            <label
                                for="role"
                                class="c-invite-user__label"
                                >Role</label
                            >
                            <select
                                v-model="form.role"
                                id="role"
                                class="c-invite-user__select"
                            >
                                <option value="owner">Owner (Full Control)</option>
                                <option value="participant">Participant</option>
                                <option value="editor">Editor</option>
                                <option value="viewer">Viewer</option>
                            </select>
                            <div
                                v-if="form.errors.role"
                                class="c-invite-user__error"
                            >
                                {{ form.errors.role }}
                            </div>
                        </div>

                        <div class="c-invite-user__actions">
                            <button
                                type="submit"
                                :disabled="form.processing"
                                class="c-invite-user__submit-btn"
                            >
                                Send Invitation
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped>
@reference "../../../css/app.css";

.c-invite-user {
    @apply py-12;
}

.c-invite-user__container {
    @apply mx-auto max-w-7xl sm:px-6 lg:px-8;
}

.c-invite-user__card {
    @apply overflow-hidden bg-white shadow-sm sm:rounded-lg dark:bg-gray-800;
}

.c-invite-user__body {
    @apply p-6 text-gray-900 dark:text-gray-100;
}

.c-invite-user__title {
    @apply text-lg font-medium;
}

.c-invite-user__form {
    @apply mt-6 space-y-6;
}

.c-invite-user__field {
    @apply block;
}

.c-invite-user__label {
    @apply block text-sm font-medium text-gray-700 dark:text-gray-300;
}

.c-invite-user__input {
    @apply mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 dark:focus:border-indigo-600 dark:focus:ring-indigo-600;
}

.c-invite-user__select {
    @apply mt-1 block w-full rounded-md border-gray-300 bg-white p-2 text-sm shadow-sm focus:border-indigo-500 focus:ring-indigo-500 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 dark:focus:border-indigo-600 dark:focus:ring-indigo-600;
}

.c-invite-user__error {
    @apply text-sm text-red-600;
}

.c-invite-user__actions {
    @apply flex items-center gap-4;
}

.c-invite-user__submit-btn {
    @apply inline-flex items-center rounded-md border border-transparent bg-gray-800 px-4 py-2 text-xs font-semibold tracking-widest text-white uppercase transition duration-150 ease-in-out hover:bg-gray-700 focus:bg-gray-700 focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 focus:outline-none active:bg-gray-900 dark:bg-gray-200 dark:text-gray-800 dark:hover:bg-white dark:focus:bg-white dark:focus:ring-offset-gray-800 dark:active:bg-gray-300;
}
</style>
