<script>
import load from '@/components/base/load';
import Cookies from 'js-cookie';
export default {
    name:'mbase',
    mixins: [load],
    data() {
        return {
            globalConfig:{
                dictkey:[],
                dictData:{},
                userModuleMethod:[],
                modulecode:'',
                billmain:{},
                dictgroupkey:[]
            },
            billapproveurl:'',
            billKey:'ph_key',
            uiconfig:{},
            uiproconfig:[],
            usercustominfo:{},
            cout:0,
            showDetailAppendix:false,
            detalAppendixFlag:true,
            billDetailAppendixConfig:{
                showTitle:false,
                scenetype:'4'
            },
            billAsyncImportConfig:{

            },
            billImportConfig:{
                importKey:'billno',
                typeData:[]
            },
            importReset:true,
            printModeReset:true,
            showBillPrintMode:false,
            showAsyncImportExport:false,
            showBillContPrintMode:false,
            showPrintMode:false,
            billprintModeUrl:'/widgetmodelv3/#/bill-print?modulecode={modulecode}&dataid={dataid}&mktcode={mktcode}&token={token}',
            printModeUrl:'',
            eventObject:['valueChanged','setDataBefore','searchBefore','cellbeginedit','importSuccess','addRowBefore','indexMethod','delRowBefore','delRowAfter','onSelectionChange','formatValue','updateCellConfig','overrideSearchParam','overrideData','refurbishCheck','onSingleChange','onSetRowCheck','onSelect','onSelectCancel','onSelectionChange','customSummary','customImgPath','onDelTag','onSearch','onAppend','filterData','onAddTag','getDictData','setLabel','setImportUrl','onLinkTo','newSaveFormBefore','setQueryConfig','setNodeClass','setTreeNodeIcon','rowClassName','customSummaryGroup','disabledDate'],
            layoutTypeList:['ListDetails','ListDetailsV2','BillListDetails','ListDetailsV3']
        };
    },
    computed: {
        uiLayoutType () {
            return 'm' + this.uiconfig.layoutType;
        }
    },
    created () {
        if (JSON.stringify(this.config) !== '{}') {
            if (this.config.eventObject) {
               Object.assign(this,this.config.eventObject);
            }
            this.uiconfig = Object.assign({},this.uiconfig, this.config);
            delete this.uiconfig.eventObject;
            this.init();
            if (this.customInit) {
                this.customInit();
            }
        }
    },

    props:{
        config: {
            type:Object,
            default () {
                return {};
            }
        }
    } ,
    methods:{
        initConfig(config,obj) {
            this.uiconfig = config;
            Object.assign(this,obj);
            this.init();
            if (this.customInit) {
                this.customInit();
            }
        },
        saveGridCol (obj) {
            let para = {};
            para.aid = Cookies.get('user_id') ? Cookies.get('user_id'): 0 ;
            para.sid = Cookies.get('stationId') ? Cookies.get('stationId'): 0 ;
            para.modulecode = this.globalConfig.modulecode;
            para.colset = JSON.stringify(obj.para);
            this.asyncPost(this.OmdUrl.amp, 'usercenter.usercustominfo.saveColset', para, function (data) {

            });
        },
        saveScene (obj) {
            let para = {};
            para.aid = Cookies.get('user_id') ? Cookies.get('user_id'): 0 ;
            para.sid = Cookies.get('stationId') ? Cookies.get('stationId'): 0 ;
            para.modulecode = this.globalConfig.modulecode;
            para.filterscene = JSON.stringify(obj.para);
            this.asyncPost(this.OmdUrl.amp, 'usercenter.usercustominfo.saveScene', para, function (data) {

            });
        },
        getUserCustomInfo () {
            let self = this;
            let para = {};
            para.aid = Cookies.get('user_id') ? Cookies.get('user_id'): 0 ;
            para.sid = Cookies.get('stationId') ? Cookies.get('stationId'): 0 ;
            para.modulecode = this.globalConfig.modulecode;
             self.synchroPost(this.OmdUrl.amp, 'usercenter.usercustominfo.getUserInfo', para, function (data) {
                if (data.usercustominfo) {
                   self.usercustominfo = data.usercustominfo;
                }
            });
        },
        getUiCustomInfo () {
            let self = this;
            let para = {};
            para.modulecode = this.globalConfig.modulecode;
             self.synchroPost(this.OmdUrl.amp, 'usercenter.uiproconfig.search', para, function (data) {
                if (data.uiproconfig && data.uiproconfig.length > 0 && data.uiproconfig[0].layoutPro) {
                   self.uiproconfig = JSON.parse(data.uiproconfig[0].layoutPro);
                }
            });
        },
        init() {
            // 处理模块号提示
            this.globalConfig.modulecode = this.$Method.getUrlParam('modulecode');
            if (!this.globalConfig.modulecode) {
                this.globalConfig.modulecode = this.$Method.getUrlParam('billmoduleid');
            }
            if (this.billmoduleid) {
                this.globalConfig.modulecode = this.billmoduleid;
            }

            if ( this.uiconfig.layoutType !== 'BirtReport') {
                if (this.OmdUrl[this.uiconfig.resources]) {
                    this.uiconfig.resources = this.OmdUrl[this.uiconfig.resources];
                }
            }

            if (this.uiconfig.billKey) {
                this.billKey = this.uiconfig.billKey;
            }

            this.setUserModuleMethod();
            this.getUiCustomInfo();
            let tmp = {};
            this.initUiConfig(this.uiconfig,tmp);
            this.initUiCustomConfig();
            // if (this.uiconfig.layoutType === 'ListDetails') {
            if (this.$utils.includes(this.layoutTypeList,this.uiconfig.layoutType)) {
                this.getUserCustomInfo();
                this.uiconfig.billQueryConfig.usercustominfo  = this.$Method.copy(this.usercustominfo);
                if (this.billisworkflow) {
                    this.uiconfig.detailConfig.billisworkflow = this.billisworkflow;
                }
            }
            if (this.billapproveurl) {
                this.uiconfig.detailConfig.billapproveurl = this.billapproveurl;
            }

            if (this.uiconfig.billImportConfig) {
                this.billImportConfig = Object.assign({}, {}, this.uiconfig.billImportConfig);
            }

            if(this.showAsyncImportExport) {
                if(this.uiconfig.billImportConfig) {
                    this.billAsyncImportConfig = Object.assign({}, this.billAsyncImportConfig, this.uiconfig.billImportConfig);
                }

                if (!this.billAsyncImportConfig.resources) {
                    this.billAsyncImportConfig.resources =  this.uiconfig.resources;
                }
            }

            this.uiconfig.showAsyncImportExport = this.showAsyncImportExport;
            if (this.uiconfig.detailConfig) {
                this.uiconfig.detailConfig.showAsyncImportExport = this.showAsyncImportExport;
            }
            if (this.showBillPrintMode || this.showBillContPrintMode) {
                if (!this.uiconfig.billPrintConfig) {
                    this.uiconfig.billPrintConfig = {};
                }
                this.uiconfig.billPrintConfig.showPrint = this.showBillPrintMode;
                this.uiconfig.billPrintConfig.showContPrint = this.showBillContPrintMode;
            }
            let accounType = Cookies.get('accounType');
            if (accounType) {
                if (accounType === '1' || accounType === '2') {
                    this.uiconfig.menuToolbar.enablePermissions = false;
                }
            }
            this.setDictData();
            this.importReset = false;
            this.$nextTick(() => {
                this.importReset = true;
            });
        },
        initUiConfig(obj,tmp) {
            if (obj instanceof Array) {
                obj.forEach(el => {
                    this.initUiConfig(el,tmp);
                })
            } else {
                if (obj instanceof  Object) {
                    for (let pro in obj) {
                        if (obj[pro] instanceof Array || obj[pro] instanceof Object) {
                            if (pro === 'data') {
                                continue;
                            }
                            this.initUiConfig(obj[pro],tmp);
                        } else {
                            if (pro === 'dictkey') {
                                if (!tmp[obj[pro]]) {
                                    tmp[obj[pro]] = 'Y';
                                    this.globalConfig.dictkey.push(obj[pro]);
                                }
                            }
                            if (this._isFun(pro)) {
                                obj[pro] = this[obj[pro]];
                            }
                        }
                    }
                }

            }
        },
        initUiCustomConfig () {
            this.uiproconfig.forEach(el => {
                if (el.pro && el.pro.length > 0) {
                    if (el.isflag === '1') {
                        // if (this.uiconfig.layoutType === 'ListDetails') {
                        if (this.$utils.includes(this.layoutTypeList,this.uiconfig.layoutType)) {
                            el.pro.forEach(el2 => {
                                this.commonUiCustomConfig(el2,this.uiconfig.billQueryConfig.gridConfig.items);
                            })
                        }
                        if (this.uiconfig.layoutType === 'LeftListRightList' || this.uiconfig.layoutType === 'LeftListRightDetails') {
                            el.pro.forEach(el2 => {
                                this.commonUiCustomConfig(el2,this.uiconfig.gridConfig.items);
                            })
                        }
                    }
                    if (el.isflag === '2') {
                        // if (this.uiconfig.layoutType === 'ListDetails' || this.uiconfig.layoutType === 'LeftListRightDetails' || this.uiconfig.layoutType === 'LeftTreeRightDetails') {
                        if (this.$utils.includes(this.layoutTypeList,this.uiconfig.layoutType) || this.uiconfig.layoutType === 'LeftListRightDetails' || this.uiconfig.layoutType === 'LeftTreeRightDetails') {
                            el.pro.forEach(el2 => {
                                this.commonUiCustomConfig2(el2,this.uiconfig.detailConfig.formsConfig.items);
                            });
                        }
                    }
                    if (el.isflag === '3') {
                        // if (this.uiconfig.layoutType === 'ListDetails' || this.uiconfig.layoutType === 'LeftListRightDetails' || this.uiconfig.layoutType === 'LeftTreeRightDetails') {
                        if (this.$utils.includes(this.layoutTypeList,this.uiconfig.layoutType) || this.uiconfig.layoutType === 'LeftListRightDetails' || this.uiconfig.layoutType === 'LeftTreeRightDetails') {
                            let index = this.uiconfig.detailConfig.formsConfig.items.findIndex(el3 => el3.name === el.typename);
                            if (index > -1) {
                                el.pro.forEach(el2 => {
                                    this.commonUiCustomConfig(el2,this.uiconfig.detailConfig.formsConfig.items[index].items);
                                });
                            }

                        }
                    }
                    if (el.isflag === '4') {
                        // if (this.uiconfig.layoutType === 'ListDetails' || this.uiconfig.layoutType === 'LeftListRightDetails' || this.uiconfig.layoutType === 'LeftTreeRightDetails') {
                        if (this.$utils.includes(this.layoutTypeList,this.uiconfig.layoutType) || this.uiconfig.layoutType === 'LeftListRightDetails' || this.uiconfig.layoutType === 'LeftTreeRightDetails') {
                            if (this.uiconfig.detailConfig.billDetailConfig && this.uiconfig.detailConfig.billDetailConfig.items) {
                                el.pro.forEach(el2 => {
                                    this.commonUiCustomConfig2(el2,this.uiconfig.detailConfig.billDetailConfig.items);
                                });
                            }
                        }
                        if (this.uiconfig.layoutType === 'LeftListRightList') {
                             el.pro.forEach(el2 => {
                                this.commonUiCustomConfig2(el2,this.uiconfig.detailGridsConfig);
                            });
                        }
                    }
                    if (el.isflag === '5') {
                        // if (this.uiconfig.layoutType === 'ListDetails' || this.uiconfig.layoutType === 'LeftListRightDetails' || this.uiconfig.layoutType === 'LeftTreeRightDetails') {
                        if (this.$utils.includes(this.layoutTypeList,this.uiconfig.layoutType) || this.uiconfig.layoutType === 'LeftListRightDetails' || this.uiconfig.layoutType === 'LeftTreeRightDetails') {
                            if (this.uiconfig.detailConfig.billDetailConfig && this.uiconfig.detailConfig.billDetailConfig.items) {
                                let index = this.uiconfig.detailConfig.billDetailConfig.items.findIndex(el3 => el3.name === el.typename);
                                if (index > -1) {
                                    el.pro.forEach(el2 => {
                                        if (this.uiconfig.detailConfig.billDetailConfig.items[index].type === 'EditGridCard') {
                                            this.commonUiCustomConfig(el2,this.uiconfig.detailConfig.billDetailConfig.items[index].items);
                                        } else {
                                            this.commonUiCustomConfig(el2,this.uiconfig.detailConfig.billDetailConfig.items[index].formCondig.items);
                                            this.commonUiCustomConfig(el2,this.uiconfig.detailConfig.billDetailConfig.items[index].gridCondig.items);
                                        }
                                    });
                                }
                            }
                        }
                        if (this.uiconfig.layoutType === 'LeftListRightList') {
                            let index = this.uiconfig.detailGridsConfig.findIndex(el3 => el3.name === el.typename);
                            if (index > -1) {
                                el.pro.forEach(el2 => {
                                     this.commonUiCustomConfig(el2,this.uiconfig.detailGridsConfig.items[index].items);
                                });
                            }
                        }

                        if (this.uiconfig.layoutType === 'BillSingleGrid') {
                            el.pro.forEach(el2 => {
                                this.commonUiCustomConfig(el2,this.uiconfig.gridConfig.items);
                            });
                        }
                    }
                }
            });

        },
        commonUiCustomConfig(obj,items) {
            let index = items.findIndex(el => el.name === obj.fieldname || el.sname === obj.fieldname);
            if (index > -1) {
                let tmp = {};
                if (obj.fieldtext) {
                    tmp.label = obj.fieldtext
                }
                if (obj.fieldwidth) {
                    tmp.width = Number(obj.fieldwidth);
                }
                if (obj.fieldcol) {
                    tmp.colNum = Number(obj.fieldcol);
                }
                if (obj.fieldvisible) {
                    tmp.visible = false;
                }
                if (obj.fieldrequired) {
                    tmp.required = true;
                }
                 if (obj.defaultValue) {
                    tmp.defaultValue = obj.defaultValue;
                }
                Object.assign(items[index], tmp);
            }
        },
         commonUiCustomConfig2(obj,items) {
            let index = items.findIndex(el => el.name === obj.fieldname );
            if (index > -1) {
                let tmp = {};
                if (obj.fieldtext) {
                    tmp.title = obj.fieldtext
                }
                if (obj.fieldvisible) {
                    tmp.visible = false;
                }
                Object.assign(items[index], tmp);
            }
        },
        _isFun (pro) {
            let index = this.eventObject.findIndex(el => el === pro);
            if (index > -1) {
                return true;
            } else {
                return false;
            }
        },
        setDictData() {
             // 调用字典方法
            if (this.globalConfig.dictkey.length > 0) {
                const dictkey = this.globalConfig.dictkey.join(',');
                this.globalConfig.dictData = this.getDict(dictkey,'code,name,pcode,pro1,ddvalue');
            }
            // 自定义添加
            if (this.initUserDict) {
                Object.assign(this.globalConfig.dictData, this.initUserDict());
            }
        },
        setUserModuleMethod() {
            let data = this.getUserModuleMethod(this.globalConfig.modulecode);
            if (data) {
                if (data.modulemethod) {
                    this.globalConfig.userModuleMethod = this.$Method.copy(data.modulemethod);
                }
                if (data.billisworkflow) {
                    this.billisworkflow = data.billisworkflow;
                }
                if (data.billapproveurl) {
                    this.billapproveurl = data.billapproveurl;
                }
                if (data.uipro) {
                    let pro = JSON.parse(data.uipro);
                    if (pro.asyncImportExport === 'Y') {
                        this.showAsyncImportExport = true;
                    }
                    if (pro.printMode === 'Y') {
                        this.showBillPrintMode = true;
                    }
                    if (pro.contPrintMode === 'Y') {
                        this.showBillContPrintMode = true;
                    }
                }
            }
        },
        openDetailAppendix(e) {
            this.$refs.detailappendix.setReadOnly(e.readOnly);
            let data = this.getData();
            let obj = {};
            obj.billno = data[this.billKey];
            obj.billmoduleid = this.globalConfig.modulecode
            obj.idkey = e.rowinfo.row.ph_key;
            if (this.setDetailAppendix) {
                obj = this.setDetailAppendix(obj,e.rowinfo);
            }
            if (!obj.idkey) {
                this.alert('未设定附件绑定信息');
                return;
            }
            this.$refs.detailappendix.setData(obj.billno,obj.billmoduleid,obj.idkey);
            this.$refs.detailappendix.setReadOnly(e.readOnly);
            this.$nextTick(() => {
                if (e.readOnly) {
                    let data = this.$refs.detailappendix.getData();
                    if (data.length === 0) {
                        this.alert('没有附件可查看');
                        return;
                    }
                }
                this.showDetailAppendix = true;
            })
        },
        doAction (action) {
            if (!action.name) {
                return;
            }
            if (action.name === 'actionappendix') {
                this.openDetailAppendix(action);
                return;
            }
            const method = action.name;
            if (this[method]) {
                this[method](action);
            } else {
                this.doPost(action);
            }
        },
        getActionMethod(action) {
            let name = action.name;
            const type = action.name;
            if (type === 'addData') {
                name = 'save';
            }
            if (type === 'saveData') {
                name = 'save';
            }
            if (type === 'delData') {
                name = 'save';
            }
            if (type === 'auditData') {
                name = 'billaudit';
            }
            if (type === 'cancelData') {
                name = 'billcancel';
            }
            if (type === 'voidData') {
                name = 'billvoid';
            }
            if (type === 'billImport' || type === 'importExcel') {
                name = 'importexcel';
            }
            if (type === 'copyData') {
                name = 'copy';
            }
            if (type === 'submitData') {
                name = 'billsubmit';
            }
            if (type === 'revokeData') {
                name = 'billrepeal';
            }
            if (this.getCustomActionMethod) {
                const temp = this.getCustomActionMethod(type, name);
                if(temp) {
                    name = temp;
                }
            }
            return name;
        },
        doPost(action) {
            let flag = true;
            let self = this;
            this.loadingtext = self.getActionName(action);
            const methodAfter = action.name + 'After';
            let method = this.uiconfig.classPrefix + '.' + this.getActionMethod(action);
            this.showLoading();
            let postData = action.para;
            if (action.name === 'saveData') {
                postData = this.$refs.layout.getChangeData();
            }
            if (this.setChangeData) {
                postData = this.setChangeData(action);
            }
            self.asyncPost(this.uiconfig.resources, method, postData, function(data){
                let msg = self.loadingtext +'成功!';
                if (data.msg) {
                    msg = data.msg;
                }
                self.hideLoading();
                self.$nextTick(() =>{
                    setTimeout(() => {
                        self.alert(msg,null,3);
                    }, 250);
                });

                if (self.$refs.layout.setPhkey) {
                    if (data instanceof Array) {
                        if (data.length > 0) {
                            self.$refs.layout.setPhkey(data[0][self.billKey]);
                        }
                    } else {
                        if (data[self.billKey]) {
                            self.$refs.layout.setPhkey(data[self.billKey]);
                        }
                    }

                }

                if (self[methodAfter]) {
                    self[methodAfter](data);
                }
                if (action.onOk) {
                    action.onOk(action, data);
                }
            },function(data) {
                const msg = self.loadingtext +'失败!' + data;
                self.hideLoading();
                self.$nextTick(() =>{
                    setTimeout(() => {
                        self.alert(msg,null,2);
                    }, 250);
                });
                flag = false;
            },function(data) {
                const msg = self.loadingtext +'异常!' + data;
                self.hideLoading();
                self.$nextTick(() =>{
                    setTimeout(() => {
                        self.alert(msg,null,2);
                    }, 250);
                });
                flag = false;
            });
            return flag;
        },
        getActionName(action) {
            let btnConfig = this.globalConfig.userModuleMethod.find(el => el.name === action.name);
            if (!btnConfig) {
                btnConfig = this.uiconfig.menuToolbar.items.find(el => el.name === action.name);
            }
            let text = '';
            if (btnConfig && btnConfig.text) {
                text = btnConfig.text;
            }
            if (this.getCustomActionName) {
                const temp = this.getCustomActionName(action.name, text);
                if(temp) {
                    text = temp;
                }
            }
            return text;
        },
        // 下载导入模版
        downTemplate() {
            const url = this.uiconfig.resources;
            let param = this.getDownTemplateParam();
            let billImport = this.getBillImport();
            billImport.setModeltype('down');
            billImport.setDownUrl(url,param);
        },
        asyncExport(grid) {
            this.$refs.billAsyncImport.setResources(this.uiconfig.resources)
            this.$refs.billAsyncImport.setModeltype('export');
            this.$nextTick(() => {
                this.$refs.billAsyncImport.initData(null,this.globalConfig.modulecode,grid);
                this.$refs.billAsyncImport.setShowUpload(true);
            })

        },
        getDownTemplateParam () {
            if (this.customGetDownTemplateParam) {
                return this.customGetDownTemplateParam();
            } else {
                let obj = {};
                obj.billmoduleid = this.globalConfig.modulecode;
                return obj;
            }
        },

        // 导入
        billImport(action) {
            const url = this.uiconfig.resources;
            const method =  this.uiconfig.classPrefix + '.' + this.getActionMethod(action);
            let para = this.getImportParam();
            if (this.showAsyncImportExport) {
                this.$refs.billAsyncImport.setResources(url);
                this.$refs.billAsyncImport.setModeltype('upload');
                this.$nextTick(() => {
                    this.$refs.billAsyncImport.setUrl(url,method,para);
                    this.$refs.billAsyncImport.initData(para.billno,null);
                    this.$refs.billAsyncImport.setShowUpload(true);
                });
            } else {
                let billImport = this.getBillImport();
                billImport.setModeltype('upload');
                billImport.setUrl(url,method,para);
                billImport.setShowUpload(true);
            }
        },
        // 导入
        importExcel() {
            const url =this.uiconfig.resources;
            const classPrefix = this.uiconfig.classPrefix ;
            let para = this.getImportParam();
            let billImport = this.getBillImport();
            billImport.setUrl(url,classPrefix,para);
            billImport.setShowUpload(true);
        },
        // 导出
        billExport(action) {
            let url = this.uiconfig.resources;
            const method = this.uiconfig.classPrefix + '.' + this.getActionMethod(action);
            let param = this.getExportParam();
            let billImport = this.getBillImport();
            const cfg = billImport.config || {};
            const typeData = cfg.typeData || [];
            if (typeData.length <= 1) {
                this.doBillExport(url, method, param, this);
                return;
            }
            billImport.setExportUrl(url, method, param, typeData);
        },
        getExportParam() {
            if (this.customGetExportParam) {
                return this.customGetExportParam();
            }

            let data = this.getData();
            let obj = {};
            let name = 'billno';
            obj.billno = data[name];
            obj.billmoduleid = this.globalConfig.modulecode;
            if(data['mktcode']) {
                obj.mktcode = data['mktcode'];
            }
            return obj;
        },

        getBillImport() {
            return this.$refs.billImport;
        },
        getImportParam () {
            if (this.customGetImportParam) {
                return this.customGetImportParam();
            } else {
                let data = this.getData();
                let obj = {};
                obj[this.billKey] = data[this.billKey];
                let name = 'billno';
                if (this.uiconfig.billImportConfig.importKey) {
                    name = this.uiconfig.billImportConfig.importKey;
                }
                obj.billno = data[name];
                obj.billmoduleid = this.globalConfig.modulecode;
                return obj;
            }
        },
        getHeadForm(name){
            if (this.$refs.layout && this.$refs.layout.getForm && this.$refs.layout.getForm()) {
                return this.$refs.layout.getForm().getForm(name);
            } else {
                return null;
            }
        },
        getDetail(name){
            if (this.$refs.layout && this.$refs.layout.getForm && this.$refs.layout.getForm()) {
                return this.$refs.layout.getForm().getDetail(name);
            } else {
                return null;
            }
        },
        getForm() {
            if (this.$refs.layout && this.$refs.layout.getForm) {
                return this.$refs.layout.getForm();
            } else {
                return null;
            }
        },
        getData() {
            if (this.$refs.layout && this.$refs.layout.getData) {
                return this.$refs.layout.getData();
            } else {
                return null;
            }
        },
        getTree () {
            if (this.$refs.layout && this.$refs.layout.getTree) {
                return this.$refs.layout.getTree();
            } else {
                return null;
            }
        },
        getTreeCondition() {
            if (this.$refs.layout && this.$refs.layout.getTreeCondition) {
                return this.$refs.layout.getTreeCondition();
            } else {
                return null;
            }
        },
        getBillQureyGrid () {
            if (this.$refs.layout && this.$refs.layout.getBillQureyGrid) {
                return this.$refs.layout.getBillQureyGrid();
            } else {
                return null;
            }
        },
        getMenuToolbar() {
            if (this.$refs.layout && this.$refs.layout.getMenuToolbar) {
                return this.$refs.layout.getMenuToolbar();
            } else {
                return null;
            }
        },
        getGrid () {
            if (this.$refs.layout && this.$refs.layout.getGrid) {
                return this.$refs.layout.getGrid();
            } else {
                return null;
            }
        },
        changedStatus(value) {
           this.$refs.layout.changedStatus(value);
        },
        initForm(data) {
            this.$refs.layout.initForm(data);
        },
        refurbish(value) {
            this.$refs.layout.refurbish(value);
        },
        setDetailVisible(name, value) {
            this.$refs.layout.setDetailVisible(name, value);
        },
        getSelectedNode(){
            if (this.$refs.layout && this.$refs.layout.getSelectedNode) {
                return this.$refs.layout.getSelectedNode();
            } else {
                return null;
            }
        },
        getDetailGrids(name) {
            if (this.$refs.layout && this.$refs.layout.getDetailGrids) {
                return this.$refs.layout.getDetailGrids(name);
            } else {
                return null;
            }
        },
        setEffectData(data) {
            this.$refs.layout.setEffectData(data);
        },
        getReportConfig () {
	        let reportfile = '';
            let self = this;
	        let searchParam = {'moduleid': this.globalConfig.modulecode};
            self.synchroPost(this.OmdUrl.mall, 'mss.base.reportconfig.search', searchParam, function (data) {
                if (data.reportconfig && data.reportconfig.length > 0) {
                   reportfile =data.reportconfig[0].reportfile;
                }
            });
	        return reportfile;
        },
        initBillApprovalData(obj) {
            let self = this;
            self.synchroPost(this.OmdUrl.mallmaster, 'master.mdm.portal.refurbishBillApproval', obj, function (data) {
            });
        },
        setDetailAppendixConfig(obj) {
            this.billDetailAppendixConfig = Object.assign({},this.billDetailAppendixConfig,obj);
            this.detalAppendixFlag = false;
            this.$nextTick(() => {
                 this.detalAppendixFlag = true;
            });
        },
        itemPrintConfig(tmpvalue,list) {
            let value = this.$Method.copy(tmpvalue);
            if (value.type !== 'selection' && value.type !== 'index'  && value.type !== 'PictureBox' && value.type !== 'FileBox' && value.type !== 'VideoBox')  {
                let printConfig = {};
                printConfig.printname = value.label;
                printConfig.printtype = '4';
                let pro = {};
                if (value.type === 'ComboBox') {
                    if (!value.data) {
                        if (value.dictkey) {
                            value.data = this.globalConfig.dictData[value.dictkey];
                        }
                    }
                }
                for (let el in value) {
                    if (typeof value[el] !== "function") {
                        pro[el] = value[el];
                        if (el === 'data') {
                            let data = [];
                            if (value[el]) {
                                let textField = 'name';
                                let valueField = 'code';
                                if (value.textField) {
                                    textField = value.textField;
                                }
                                if (value.valueField) {
                                    valueField = value.valueField;
                                }
                                value[el].forEach(el2 => {
                                    let tmp = {};
                                    tmp[textField] = el2[textField];
                                    tmp[valueField] = el2[valueField];
                                    data.push(tmp);
                                });
                            }
                            pro.data = data;
                        }
                    }
                }
                if ('{}' != JSON.stringify(pro)) {
                    printConfig.printpro = JSON.stringify(pro);
                }
                if (value.textName) {
                    printConfig.printcode = value.textName;
                    list.push(printConfig);
                } else if (value.sname) {
                    if (value.type === 'DateRangeBox') {
                        pro.type = 'DateBox';
                    }
                    if (value.type === 'NumberRangeBox') {
                        pro.type = 'NumberBox';
                    }
                    let sconfig = this.$Method.copy(printConfig);
                    sconfig.printname = sconfig.printname + '(开始)';
                    sconfig.printcode = value.sname;
                    list.push(sconfig);
                    let econfig = this.$Method.copy(printConfig);
                    econfig.printname = sconfig.printname + '(结束)';
                    econfig.printcode = value.ename;
                    list.push(econfig);
                } else {
                    printConfig.printcode = value.name;
                    list.push(printConfig);
                }
            }
        },
        initGridPrintConfig(el,list) {
            if (el.children && el.children.length > 0) {
                el.children.forEach(el2 => {
                    this.initGridPrintConfig(el2,list);
                })
            } else {
                this.itemPrintConfig(el,list);
            }
		},
        getBillPrintConfig() {
            let  billprintConfig = {};
            billprintConfig.modulecode = this.globalConfig.modulecode;
            billprintConfig.printcode = billprintConfig.modulecode;
            billprintConfig.printname = document.title;
            billprintConfig.printtype = '1';
            billprintConfig.parentid = '0';
            billprintConfig.children = [];
            let mainConfig = {};
            mainConfig.printcode = this.uiconfig.response;
            mainConfig.printname = '单据主表';
            mainConfig.printtype = '2';
            mainConfig.children = [];
            // if (this.uiconfig.layoutType === 'ListDetails') {
            if (this.$utils.includes(this.layoutTypeList,this.uiconfig.layoutType)) {
                this.uiconfig.detailConfig.formsConfig.items.forEach(el => {
                   if(el.items) {
                       el.items.forEach(el2 => {
                            if (el2.label) {
                                this.itemPrintConfig(el2,mainConfig.children);
                            }
                       })
                   }
               });
               this.uiconfig.detailConfig.billDetailConfig.items.forEach(el => {
                    if (el.type === 'EditGridCard') {
                        let temp = {};
                        temp.printcode = el.name;
                        temp.printname = el.title;
                        temp.printtype = '3';
                        temp.children = [];
                        el.items.forEach(el2 => {
                            this.initGridPrintConfig(el2,temp.children);
                        });
                        mainConfig.children.push(temp);
                    } else if (el.type === 'DetailsBox') {
                        let temp = {};
                        temp.printcode = el.name;
                        temp.printname = el.formConfig.title;
                        temp.printtype = '3';
                        temp.children = [];
                        el.formConfig.items.forEach(el2 => {
                            this.itemPrintConfig(el2,temp.children);
                        });
                        mainConfig.children.push(temp);
                        if (el.gridName) {
                            let temp2 = {};
                            temp2.printcode = el.gridName;
                            temp2.printname = el.gridConfig.title;
                            temp2.printtype = '3';
                            temp2.children = [];
                            el.gridConfig.items.forEach(el2 => {
                                this.initGridPrintConfig(el2,temp.children);
                            });
                            mainConfig.children.push(temp2);
                        }

                    }
                });
                billprintConfig.children.push(mainConfig);
            }
            return billprintConfig;
        },
        printBillData() {
            this.loadingtext= '打印数据准备中....';
            this.showLoading();
            this.checkPrintConfig();
        },
        setPrintModeUrl (dataid) {
            let curlUrl = this.$Method.copy(this.billprintModeUrl);
            curlUrl = curlUrl.replace('{modulecode}',this.globalConfig.modulecode);
            curlUrl = curlUrl.replace('{dataid}',dataid);
            curlUrl = curlUrl.replace('{token}',this.getToken());
            curlUrl = curlUrl.replace('{mktcode}',this.printMkt());
            if (this.showBillContPrintMode) {
                curlUrl = curlUrl +'&contprint=Y'
            }
            // 打开打印模板
            this.printModeUrl = curlUrl;
            this.printModeReset = false;
            this.$nextTick(() => {
                this.printModeReset = true;
                this.$nextTick(() => {
                    this.hideLoading();
                    this.$nextTick(() => {
                        this.showPrintMode = true;
                    });
                });
            });
        },

        printMkt() {
            let tmp = this.$refs.layout.getData();
            if (tmp.mktcode) {
                return tmp.mktcode;
            }
            return '';
        },
        checkPrintConfig () {
            let flag = false;
            let para = {};
            let self  = this;
            para.modulecode = this.globalConfig.modulecode;
            self.asyncPost(this.OmdUrl.sysconfboot, 'mss.sysconf.printconfig.checkPrintConfig', para, function (data) {
                if(data.flag === 'Y'){
                    let dataid = self.savePrintData();
                    self.setPrintModeUrl(dataid);
		        } else {
                    self.initPrintConfig();
                }
            });
            return flag;
        },
        initPrintConfig() {
            let self = this;
            let para = this.getBillPrintConfig();
            self.asyncPost(this.OmdUrl.sysconfboot, 'mss.sysconf.printconfig.initConfig', para, function (data) {
                if(data.flag === '1'){
			       let dataid = self.savePrintData()
                   self.setPrintModeUrl(dataid);
		        }
            });
        },
        savePrintData() {
            let tmp = this.$refs.layout.getInitFormData();
            let dataid = '';
            let self = this;
            let para = {};
            let  printdata ={};
	        printdata.modulecode = this.globalConfig.modulecode;
	        printdata.billkey = tmp.billkey;
	        printdata.datapro = JSON.stringify(tmp.formdata);
	        printdata.flag = 'I';
            para.printdata = printdata;
            self.synchroPost(this.OmdUrl.sysconfboot, 'mss.sysconf.printdata.savePrintData', para, function (data) {
                if(data.flag === '1'){
                    dataid = data.dataid;
		        }
            });
	        return dataid;
        },
        asyncImportClose(value) {
            if (!value) {
                this.refurbish();
            }
        },
        doImportSuccess() {
            this.refurbish();
        },
        setHideDividerValue(value) {
            this.$refs.layout.setHideDividerValue(value);
        },
        handleSuccessAfter(response, file, fileList) {
            this.$emit('handleSuccessAfter' , response, file, fileList);
        }
    },
    watch: {
        showDetailAppendix(value) {
            if (!value) {
                this.$nextTick(() => {
                    if (this.shutDownDetailAppendix) {
                        this.shutDownDetailAppendix();
                    }
                });
            }
        }
    },
};
</script>
