<template>
    <div class='pl-common-select'>
        <el-select v-model="dataValue" clearable filterable placeholder="请选择" @change='onChange' :size='size'>
            <el-option
                v-for="item in selectDataArr"
                :key="item.id"
                :label="item[dataName]"
                :value="item[dataId]">
            </el-option>
        </el-select>
    </div>
</template>

<script>
export default {
    name: "plCommonSelect",
    props: {
        value: {required: true, type: String, default: ""},
        typeId: {required: true, type: String, default: ""},
        parentId: {required: false, default: "-1"},
        sourceKey: {required: false, default: ""},
        size: {required: false, type: String, default: "medium"}
    },
    data() {
        return {
            dataValue: '',
            dataName: "dataName",
            dataId: "id",
            currentParentId: "",
            selectDataArr: [],
            selectDataObj: {},
            selectDataNameObj: {},
        }
    },
    created() {
        this.dataValue = this.value;
        this.initCommonSelect();
    },
    methods: {
        onChange(val) {
            this.$emit("input", val);
        },
        initCommonSelect() {
            this.currentParentId = this.parentId || "";
            this.selectDataObj = {};
            this.selectDataNameObj = {};
            // let promise = this.DATA_GLOBAL.getDatas(this.typeId, this.currentParentId);
            this.axios.post(this.api.mixDataTool.getDataByType, {
                sourceKey: this.sourceKey || window.currentDataSourceKey,
                typeId: this.typeId,
                parentId: this.currentParentId
            }).then(dataRes => {
                // this.initDataValue();
                this.selectDataArr = dataRes.data || [];
                this.selectDataArr.forEach(dataEach => {
                    this.selectDataObj[dataEach.id] = dataEach;
                    this.selectDataNameObj[dataEach.id] = dataEach.dataName;
                });
                // vm.rememberSysDataObj && vm.rememberSysDataObj(vm.field,vm.arrDatasObj);
            });
        }
    }
}
</script>

<style scoped>
.pl-common-select {
    display: inline-block;
}
</style>