<script setup lang="ts">
import LayoutAuthenticated from '@/Layouts/LayoutAuthenticated.vue';
import SectionMain from '@/Components/SectionMain.vue';
import SectionTitleLineWithButton from '@/Components/SectionTitleLineWithButton.vue';
import { Head, usePage, useForm } from '@inertiajs/vue3';
import { computed, Ref } from 'vue';
import CardBox from '@/Components/CardBox.vue';
import BaseButton from '@/Components/BaseButton.vue';
import BaseButtons from '@/Components/BaseButtons.vue';
import { stardust } from '@eidellev/adonis-stardust/client';
import { mdiArrowLeftBoldOutline, mdiShareVariant } from '@mdi/js';
import FormValidationErrors from '@/Components/FormValidationErrors.vue';
// import { Dataset } from '@/Dataset';
import FormField from '@/Components/FormField.vue';
import FormControl from '@/Components/FormControl.vue';

const props = defineProps({
    errors: {
        type: Object,
        default: () => ({}),
    },
    dataset: {
        type: Object,
        default: () => ({}),
    },
    reviewers: {
        type: Object,
        default: () => ({}),
    },
});

// const isButtonDisabled = computed(() => {
//     return form.processing || form.reviewer_id != undefined;
// });
const isButtonDisabled = computed(() => {
    return form.processing || form.reviewer_id === undefined;
});

const flash: Ref<any> = computed(() => {
    return usePage().props.flash;
});
// const errors: Ref<any> = computed(() => {
//     return usePage().props.errors;
// });

const form = useForm({
    reviewer_id: undefined,    
});
// let form = useForm<Dataset>(props.dataset as Dataset);


// const isPreferationRequired = computed(() => form.preferation === 'yes_preferation');

const handleSubmit = async (e) => {
    e.preventDefault();

    await form.put(stardust.route('editor.dataset.approveUpdate', [props.dataset.id]));    
};
</script>

<template>
    <LayoutAuthenticated>

        <Head title="Approve dataset" />
        <SectionMain>
            <SectionTitleLineWithButton :icon="mdiShareVariant" title="Approve corrected dataset" main>
                <BaseButton :route-name="stardust.route('editor.dataset.list')" :icon="mdiArrowLeftBoldOutline" label="Back"
                    color="white" rounded-full small />
            </SectionTitleLineWithButton>

            <CardBox :form="true" @submit.prevent="handleSubmit" id="approveForm">
                <FormValidationErrors v-bind:errors="errors" />

                <div class="mb-4">

                    <div class="w-full">
                        <div class="block font-bold mb-2" for="owner">Preferred Reviewer:</div>
                        <div id="owner" class="w-full p-2 rounded-md">{{ dataset.preferred_reviewer }}</div>
                    </div>
                    <div class="w-full">
                        <div class="block font-bold mb-2" for="owner2">Preferred Reviewer Email:</div>
                        <div id="owner2" class="w-full p-2 rounded-md">{{ dataset.preferred_reviewer_email }}</div>
                    </div>
                    <FormField label="Reviewer" help="reviewer is required" label-for="selectReviewer"
                        :class="{ 'text-red-400': form.errors.reviewer_id }" class="w-full mx-2 flex-1">
                        <FormControl id="selectReviewer" v-model="form.reviewer_id" :type="'select'" placeholder="-- select reviewer --"
                            :errors="form.errors.reviewer_id" :options="reviewers">
                            <div class="text-red-400 text-sm" v-if="form.errors.reviewer_id">
                                {{ form.errors.reviewer_id }}
                            </div>
                        </FormControl>
                    </FormField>
                </div>


                <div v-if="flash && flash.warning" class="flex flex-col mt-6 animate-fade-in">
                    <div class="bg-yellow-500 border-l-4 border-orange-400 text-white p-4" role="alert">
                        <p class="font-bold">Be Warned</p>
                        <p>{{ flash.warning }}</p>
                    </div>
                </div>

                <template #footer>
                    <BaseButtons>
                        <BaseButton type="submit" color="info" label="Set Approved"
                            :class="{ 'opacity-25': form.processing }"
                            :disabled="isButtonDisabled" :icon="mdiShareVariant"
                            small />

                        <!-- <button :disabled="errors.any()" type="submit" class="pure-button">
                    <i class="fa fa-share"></i>
                    <span>Set Approved</span>
                </button> -->

                    </BaseButtons>
                </template>
            </CardBox>
        </SectionMain>
    </LayoutAuthenticated>
</template>
