<template>
    <aui-base-form
        layout="6"
        dense-list
    >
        <slot
            name="actions"
            :loading="loading"
            :has-unsaved-data="hasUnsavedData"
            :has-invalid-data="hasInvalidData"
            :reset="reset"
            :submit="submit"
        />
        <template #col-1>
            <aui-base-form-field>
                <template
                    v-if="formData.slots && formData.slots.length > 0"
                >
                    <q-item
                        v-for="(_, index) in formData.slots"
                        :key="index"
                        class="no-padding"
                    >
                        <q-item-section>
                            <q-select
                                v-model="formData.slots[index].slot"
                                dense
                                :error="false"
                                :label="$t('Key')"
                                :options="availableKeys"
                                map-options
                                emit-value
                                :readonly="!canEdit"
                                :disable="loading"
                            />
                        </q-item-section>
                        <q-item-section>
                            <q-input
                                v-model.trim="formData.slots[index].destination"
                                dense
                                clearable
                                :label="$t('Destination')"
                                :readonly="!canEdit"
                                :disable="loading"
                                :error="v$.$error && v$.formData.slots.$each.$response.$errors[index].destination.length > 0"
                                :error-message="$errMsg(v$.formData.slots.$each.$response.$errors[index].destination)"
                                @keyup.enter="submit"
                            />
                        </q-item-section>
                        <q-item-section
                            v-if="canEdit"
                            side
                        >
                            <q-btn
                                color="negative"
                                unelevated
                                dense
                                icon="delete"
                                size="sm"
                                :disable="loading"
                                @click="deleteAutoAttendant(index)"
                            />
                        </q-item-section>
                    </q-item>
                </template>
                <q-item
                    class="no-padding"
                    v-if="canEdit"
                >
                    <q-item-section
                        class="aui-list-item-section-button"
                        side
                    >
                        <q-btn
                            :label="$t('Add slot')"
                            color="primary"
                            icon="add"
                            size="sm"
                            unelevated
                            outline
                            :disable="loading || formData.slots.length > 10"
                            @click="addAutoAttendant"
                        />
                    </q-item-section>
                </q-item>
            </aui-base-form-field>
        </template>
    </aui-base-form>
</template>

<script>
import useValidate from '@vuelidate/core'
import { helpers, required } from '@vuelidate/validators'
import AuiBaseFormField from 'components/AuiBaseFormField'
import AuiBaseForm from 'components/edit-forms/AuiBaseForm'
import baseFormMixin from 'src/mixins/base-form'
import { mapGetters } from 'vuex'
export default {
    name: 'AuiAutoAttendantForm',
    components: { AuiBaseFormField, AuiBaseForm },
    mixins: [baseFormMixin],
    emits: ['remove'],
    props: {
        canEdit: {
            type: Boolean,
            default: true
        }
    },
    data () {
        return {
            v$: useValidate(),
            formData: {
                slots: []
            }
        }
    },
    validations () {
        return {
            formData: {
                slots: {
                    $each: helpers.forEach({
                        slot: {
                            required
                        },
                        destination: {
                            required
                        }
                    })
                }
            }
        }
    },
    computed: {
        ...mapGetters('subscribers', [
            'autoAttendantKey'
        ]),
        getInitialData () {
            return {
                slots: this.initialFormData?.slots || []
            }
        },
        availableKeys () {
            const selectedKeys = this.formData.slots.map((slot) => slot.slot)
            return this.autoAttendantKey.filter((key) => !selectedKeys.includes(key.value) || key.value === 'default')
        }

    },
    watch: {
        'formData.slots': {
            handler () {
                this.$forceUpdate()
            },
            deep: true
        }
    },
    methods: {
        addAutoAttendant () {
            this.formData.slots.push({
                slot: this.availableKeys[0].value,
                destination: null
            })
        },
        deleteAutoAttendant (index) {
            this.formData.slots.splice(index, 1)
        }
    }
}
</script>
