<template>
    <div>
        <autocomplete-selector 
            :value="value"
            :errors="errors"
            label="Cui se adresează"
            :dataprovider="dataprovider"
            :hasInsert="true"

            @focus="$emit('focus')"
            @blur="$emit('blur')"
            @click:append-outer="OnClickAppendOuter"
            
            @input="$emit('input', $event)"
        />

        <adresare-form-menu 
            :app="app"
            :form="app.forms.Adresare"
            @insert="OnInsertAdresare"
        />
    </div>
</template>

<script>

    import DataProvider from 'APPS/Dataproviders/Adresare/Adresare';
    
    export default {
        props: {
            value: {required: true},
            errors: {default: null},
            app: {default: null},
            adresareForm: {default: null},
        },

        data: () => ({
            dataprovider: null,
        }),

        methods: {
            OnClickAppendOuter(e) {
                this.ShowActionForm(e, 'insert', null);
            },

            OnInsertAdresare(record) {
                this.dataprovider.Get();
                this.$emit('input', record.id);
            },

            ShowActionForm(e, action, record) {
                if(! this.adresareForm )
                {
                    return false;
                }
                this.adresareForm.menu.visible = false;
                this.adresareForm.Show(null, action, record);

                this.$nextTick( () => {
                    this.adresareForm.menu.visible = true;
                    this.adresareForm.menu.x = e.clientX;
                    this.adresareForm.menu.y = e.clientY;

                });                
            }
        },

        created() {
            this.dataprovider = new DataProvider(null);
        },

        components: {
            AutocompleteSelector: require('./../AutocompleteSelector').default,
        },

        name: 'AdresareAutocompleteSelector',
    }
</script>