<template>
    <span>
        <el-button size="small" @click="openPopupTable">{{buttonName}}</el-button>
        <el-dialog :title="title" :append-to-body="true" width="80%" :visible.sync="ifopenpopupTable">
            <common-table-list popup-formater
                               :viewId="tableId"
                               is-component :showTopBtns="false"
                               @choosePopup="choosePopup"
                               @unChoosePopup="unChoosePopup"
                               :filterCondition="filterCondition"
                               :popupChooseId="chooseId"></common-table-list>
        </el-dialog>
    </span>
</template>

<script>
    export default {
        name: "pl-popup-table-select",
        props: {
            formId: {
                type: String,
                default: ''
            },
            viewId: {
                type: String,
                default: ''
            },
            tableId: {
                type: String,
                default: ''
            },
            uid: {
                type: String,
                default: ''
            },
            buttonName: {
                type: String,
                default: '选择'
            },
            showFieldEng: {
                type: String,
                default: ''
            },
            filterCondition: {
                type: String,
                default: ''
            },
            headerName: {
                type: String,
                default: '数据列表'
            },
            tableName: {
                type: String,
                default: ''
            },
            value: {}
        },
        data() {
            return {
                ifopenpopupTable: false,
                tableProps: {},
                popupOption:{},
                formObj: this.value,
                chooseId:"",
                title:this.headerName||"数据列表"
            }
        },
        methods: {
            openPopupTable() {
                //打开弹出数据表
                let vm = this;
                if(vm.formObj["OTHER_ITEM"]){
                    let otherItem = JSON.parse(vm.formObj["OTHER_ITEM"]);
                    vm.chooseId = otherItem[vm.showFieldEng];
                }
                vm.ifopenpopupTable = true;
            },
            unChoosePopup(data){
                //点击取消选择弹出表数据
                let vm = this;
                let setObj = null;
                //获取弹出表配置信息，获取回填相关配置
                if(vm.popupOption.hasOwnProperty(vm.uid)){
                    setObj = vm.popupOption[vm.uid];
                    if(setObj){
                        vm.popupCallBack(setObj,null);
                    }
                }else{
                    vm.axios.post('/fieldsetController/listNoPage',
                    {
                        'formId':vm.formId,'ssmk':'tcsjb',
                        'viewId':vm.viewId,'tableId':vm.tableId,
                        'uid':vm.uid
                    }).then(function (res) {
                        if(res&&res.data){
                            vm.popupOption[vm.uid]=res.data;
                            setObj=res.data;
                            if(setObj){
                                vm.popupCallBack(setObj,null);
                            }
                        }
                    });
                }
            },
            choosePopup(data){
                //点击选择弹出表数据
                let vm = this;
                let setObj = null;
                //获取弹出表配置信息，获取回填相关配置
                if(vm.popupOption.hasOwnProperty(vm.uid)){
                    setObj = vm.popupOption[vm.uid];
                    if(setObj){
                        vm.popupCallBack(setObj,data);
                    }
                }else{
                    vm.axios.post('/fieldsetController/listNoPage',
                    {
                        'formId':vm.formId,'ssmk':'tcsjb',
                        'viewId':vm.viewId,'tableId':vm.tableId,
                        'uid':vm.uid
                    }).then(function (res) {
                        if(res&&res.data){
                            vm.popupOption[vm.uid]=res.data;
                            setObj=res.data;
                            if(setObj){
                                vm.popupCallBack(setObj,data);
                            }
                        }
                    });
                }
            },
            popupCallBack(setObj,data){
                //回填
                let vm = this;
                let temporaryData = '';
                for(let i=0;i<setObj.length;i++){
                    temporaryData = data?data[setObj[i].callbackField.toUpperCase()]:"";
                    if(temporaryData){
                        temporaryData = temporaryData.replace(/\r\n/g, '<br/>');
                    }
                    vm.formObj[setObj[i].linkField.toUpperCase()] = temporaryData;
                }
                //将回填数据填回表单视图
                let otherItem={};
                if(vm.formObj.hasOwnProperty("OTHER_ITEM")&&vm.formObj["OTHER_ITEM"]){
                    otherItem = JSON.parse(vm.formObj["OTHER_ITEM"]);
                    if(!data){
                        delete otherItem[vm.showFieldEng];
                    }
                }
                if(data){
                    otherItem[vm.showFieldEng]=data["ID"];
                    vm.ifopenpopupTable = false;
                }
                vm.formObj["OTHER_ITEM"] = JSON.stringify(otherItem);
                vm.$emit('input',vm.formObj);
            }
        }

    }
</script>

<style scoped>

</style>