export const mixinView = {
    created() {
        if (this.$options.url) {
            this.url = this.$URL + this.$options.url
        }
    },
    mounted() {
        this.listAll();
    },
    data() {
        return {
            url: '',
            list: [],
            obj: {},
            filter: {
                description: ''
            },
        }
    },
    methods: {
        deleteObject(id) {
            this.httpDelete(this.url + "/" + id).then(() => {
                this.listAll();
            });
        },
        restoreObject(id) {
            this.httpPut(this.url + "/" + id + "/restore").then(() => {
                this.listAll();
            });
        },
        listAll() {
            this.obj = {}
            this.httpGet(this.url).then((res) => {
                this.list = res.body;
            });
        },
        listDeleted() {
            this.obj = {}
            this.httpGet(this.url + "/deleted").then((res) => {
                this.list = res.body;
            });
        },
        load(id) {
            this.httpGet(this.url + "/" + id).then((res) => {
                this.obj = res.body;
            });
        },
        openModalForm(modalWidth = 330, modalProps = {}) {
            this.$buefy.modal.open({
                parent: this,
                component: this.$options.form,
                width: modalWidth,
                props: modalProps
            }).$on('close', () => this.listAll())
        }
    },
    computed: {
        objIsEmpty () {
            return this.obj === null || this.obj === undefined || this.obj.code == null || this.obj.code === undefined
        },
        filteredList () {
            return this.list.filter((item) => 
              item.description.toLowerCase().indexOf(this.filter.description.toLowerCase()) >= 0
              ||
              item.code.toLowerCase().indexOf(this.filter.description.toLowerCase()) >= 0
            )
        }
    }
}