import * as API from "api/virtual/properties";
export default {
    props: {
        show: {
            type: Boolean,
            default: false
        },
    },
    data() {
        return {
            my_show: false,
            loading: false,
            configure: [],//配置总数据
            carTypeList: {},//车辆类型
            carTypeName: '',
            param: {
                name: '',//选中平台
                value: {},//选中的车辆类型
            },
        };
    },
    watch: {
        show(bool) {
            this.my_show = bool;
            if (bool) {
                this.getCarTypeToPlatformList()
            }
        },
    },
    methods: {
        getCarTypeToPlatformList() {
            API.getCarTypeToPlatformList()
                .then(res => {
                    if (res.code == 0) {
                        this.configure = res.data;
                    }
                })
        },
        getCarTypes(val) {
            if (!val) return;
            const configure = this.configure[val] || {};
            this.carTypeList = configure.carList;
            this.param.value = {};
            if (configure['已选']) {
                this.param.value = configure['已选']
            } else {
                for (let k in configure.carList) {
                    this.$set(this.param.value, k, [])
                }
            }
            this.carTypeName = Object.keys(configure.carList)[0];
        },
        ok() {
            let data = new Object;
            data.name = this.param.name;
            data.value = JSON.stringify(this.param.value);
            this.loading = true;
            API.saveCarTypeToPlatform(data)
                .then(res => {
                    this.loading = false;
                    if (res.code == 0) {
                        this.$Message.success('修改成功');
                    }
                })
                .catch(err => {
                    this.loading = false;
                })
        },
        clear() {
            this.my_show = false;
            this.configure = [];
            this.carTypeList = {};
            this.carTypeName = '';
            this.param = {
                name: '',
                value: {}
            };
        },
        visibleChange(bool) {
            if (!bool) {
                this.$emit('update:show', false);
            }
            this.configure = [];
            this.carTypeList = {};
            this.carTypeName = '';
            this.param = {
                name: '',
                value: {}
            };
        },
    }
};
