<template>
    <pl-select-tree v-if="ifTree==true" v-model="formData[field]" :options="selectArr" :props="defaultSelectTreeProps">
    </pl-select-tree>
    <el-select v-else-if="ifTree==false" v-model="formData[field]" :clearable="true" placeholder="请选择" :multiple="multiple" :filterable="filterable">
        <el-option
                v-for="item in selectArr"
                :key="item.value"
                :label="item.label"
                :value="item.value">
        </el-option>
    </el-select>
</template>

<script>
    import {PlSelectTree} from 'sasp-base';

    export default {
        name: "pl-self-data-select",//自定义数据来源下拉框
        components: {
            PlSelectTree
        },
        props: {
            formData: {type: Object, required: true},//操作对象
            field: {type: String, required: true},//操作字段
            dataUrl: {type: String, required: true},//数据来源
            valueField: {type: String, required: true},//值域字段
            labelField: {type: String, required: true},//显示域字段
            multiple: {type: Boolean,  default: false},//是否多选
            filterable: {type: Boolean,  default: true},//是否搜索
            ifTree: {type: Boolean, required: true,default:false},//是否为下拉树
            selfField: {type: String},//为树时标识本级字段
            parentField: {type: String},//为树时标识父级字段
            childField: {type: String},//为树时存放下级树数据字段
            paramObj:{type: String,default:''}//过滤条件
        },
        data() {
            return {
                selectArr: [],
                defaultSelectTreeProps: {
                    parent: 'parentId',   // 父级唯一标识
                    value: 'id',          // 唯一标识
                    label: 'text',       // 标签显示
                    children: 'children', // 子级
                },
            }
        },
        created() {
            let vm = this;
            if (vm.dataUrl && vm.valueField && vm.labelField) {
                let url=vm.dataUrl;
                if (vm.dataUrl.indexOf("/") != 0 && vm.dataUrl.indexOf("http") != 0) {
                    url = "/" + vm.dataUrl;
                }
                vm.selectArr = [];
                let paramObj={};
                if(vm.paramObj){
                    paramObj = eval('('+vm.paramObj+')');
                }
                vm.spcpFormAxios.post(url, paramObj).then(function (data) {
                    if (data) {
                        for (let i = 0; i < data.data.length; i++) {
                            if (vm.ifTree) {
                                vm.selectArr.push({id:data.data[i][vm.selfField],value: data.data[i][vm.valueField],
                                    text: data.data[i][vm.labelField],children: data.data[i][vm.childField],parent: data.data[i][vm.parentField]});
                            } else {
                                vm.selectArr.push({value: data.data[i][vm.valueField], label: data.data[i][vm.labelField]});
                            }
                        }
                    }
                });
            }
        }
    }
</script>

<style scoped>

</style>
