<template>
    <el-checkbox-group v-model="checkList" @change='change'>
        <el-checkbox v-for='item in checkboxArr' :key='item.key' :label="item.id">{{ item.dataName }}</el-checkbox>
    </el-checkbox-group>
</template>

<script>
export default {
    name: "plCommonCheckbox",
    props: {
        value: {required: false, type: Array, default: () => []},
        typeId: {required: true, type: String, default: ""},
        parentId: {required: false, default: "-1"},
        sourceKey: {required: false, default: ""},
        size: {required: false, default: "medium"}
    },
    data() {
        return {
            checkList: [],
            currentParentId: "",
            checkboxArr: [],
            checkboxObj: {},
        }
    },
    watch: {
        value() {
            this.checkList = JSON.parse(JSON.stringify(this.value));
        }
    },
    created() {
        // console.log('pl-common-radio,created', this.value);
        this.checkList = JSON.parse(JSON.stringify(this.value));
        this.init();
    },
    methods: {
        change(val) {
            this.$emit("input", val);
        },
        init() {
            this.currentParentId = this.parentId || "";
            this.checkboxObj = {};
            this.axios.post(this.api.mixDataTool.getDataByType, {
                sourceKey: this.sourceKey || window.currentDataSourceKey,
                typeId: this.typeId,
                parentId: this.currentParentId
            }).then(dataRes => {
                this.checkboxArr = dataRes.data || [];
                this.checkboxArr.forEach(item => {
                    this.checkboxObj[item.id] = item;
                });
            });
        }
    }
}
</script>

<style scoped>

</style>