<script setup>
import GuestLayout from '@/Layouts/GuestLayout.vue';
import InputError from '@/Components/InputError.vue';
import InputLabel from '@/Components/InputLabel.vue';
import PrimaryButton from '@/Components/PrimaryButton.vue';
import SecondaryButton from '@/Components/SecondaryButton.vue';
import TextInput from '@/Components/TextInput.vue';
import Multiselect from '@/Components/MultiSelect.vue';
import { Head, Link, useForm } from '@inertiajs/inertia-vue3';
import rpoFields from './Data/MultiSelectOptionsRPO';
import ConditionDisplay from '@/Components/ConditionStatement.vue';
import { Inertia } from '@inertiajs/inertia';
import { ref } from 'vue';

const preoperator_val1 = ref(['-', 'NOT']);
const preoperator_val2 = ref(['AND', 'AND NOT', 'OR', 'OR NOT']);

const level = ref(0);
const podmienka = ref([]);
const count = ref(0);

const criteria_val = ref([
                           { name: "Obsahuje", operator: 'LIKE'},
                           { name: "Začína", operator: 'LIKE START'},
                           { name: "Rovná sa", operator: '='}
                        ]);

const form = useForm({
    groupoperator: '',
    preoperator: '',
    field: '',
    value: '',
    criteria: '',
});

const submit = () => {
    console.log('Post data');
    Inertia.post(`/search`, podmienka.value);
};

const AddContition = () => {
    podmienka.value.push({groupop: form.groupoperator,pre: form.preoperator, field: form.field, value: form.value, criteria: form.criteria, level: level.value, count: count.value});
    for (var i = count.value; i >= 0; i--) {
        podmienka.value[podmienka.value.length - i - 1]["groupop"] = form.groupoperator;
    }
    count.value++;
    ConstructCondition(); 
}

const AddLevel = () => {
    level.value++;
    count.value=0; 
}

const RemLevel = () => {
    level.value--;
    count.value=0; 
}

const ConstructCondition = () => {

}
</script>

<template>
    <GuestLayout>
        
        <Head title="Register" />

        <ConditionDisplay :conditions="podmienka"></ConditionDisplay>

        <div class="min-h-screen flex flex-col sm:justify-center items-center pt-6 sm:pt-0 bg-gray-100">
            <div class="w-full sm:max-w-md mt-6 px-6 py-4 bg-white shadow-md  sm:rounded-lg">
                <form @submit.prevent="submit">
                    <template v-if="level > 0">
                        <InputLabel for="Operator" value="Skupinový operátor" />
                        <Multiselect v-model="form.groupoperator" :options="preoperator_val2">
                        </Multiselect>
                    </template>


                    <InputLabel for="Operator" value="Operator" />
                    <Multiselect v-model="form.preoperator" :options="count == 0 ? preoperator_val1 : preoperator_val2">
                    </Multiselect>

                    <InputLabel for="field" value="Pole" />
                    <Multiselect v-model="form.field" label="desc" trackby="desc" :options="rpoFields"></Multiselect>

                    <InputLabel for="Kriteria" value="Kriteria" />
                    <Multiselect v-model="form.criteria" label="name" trackby="name" :options="criteria_val">
                    </Multiselect>

                    <div class="mt-4">
                        <InputLabel for="Hodnota" value="Hodnota" />
                        <TextInput id="value" type="text" class="mt-1 block w-full" v-model="form.value" required />
                        <InputError class="mt-2" :message="form.errors.email" />
                    </div>

                    <pre>podmienka = {{ podmienka }} </pre>
                    <pre>level = {{ level }}</pre>
                    <pre>count = {{ count }}</pre>


                    <div class="flex items-center justify-end mt-4">

                        <PrimaryButton class="ml-4" :class="{ 'opacity-25': form.processing }"
                            :disabled="form.processing">
                            Hľadaj
                        </PrimaryButton>




                        <SecondaryButton class="ml-4" @click="AddLevel">
                            <font-awesome-icon icon="fa-solid fa-right-from-bracket" />
                        </SecondaryButton>

                        <SecondaryButton class="ml-4" @click="RemLevel">
                            <font-awesome-icon icon="fa-solid fa-right-to-bracket" />
                        </SecondaryButton>

                        <SecondaryButton class="ml-4" @click="AddContition">
                            <font-awesome-icon icon="fa-solid fa-circle-plus" />
                        </SecondaryButton>


                    </div>
                </form>


            </div>
        </div>



    </GuestLayout>
</template>
