export const mixinForm = {
    created() {
        this.obj = this.initObject()
        if (this.$options.url) {
            this.url = this.$URL + this.$options.url
        }
        if (this.$options.method) {
            this.method = this.$options.method
        }
    },
    data() {
        return {
            method: 'POST',
            url: '',
            obj: null,
        }
    },
    methods: {
        initObject() {
            return {}
        },
        preSave() {},
        save() {
            this.$validator.validateAll().then(valid => {
                if(valid) {
                    this.$validator.reset();
                    this.preSave();
                    if (this.method === 'POST') {
                        this.httpPost(this.url, this.obj).then((res) => {
                            this.$emit('close', res)
                        });
                    } else if (this.method === 'PUT') {
                        this.httpPut(this.url, this.obj).then((res) => {
                            this.$emit('close', res)
                        });
                    } else {
                        alert('HTTP Method not implemented', this.method)
                    }
                }
            })
        }
    }
}