import Vue from "vue"
import Api from "~/service/Api"
import Utils from "../utils"

export default {
    data() {
       
    },
    created(){
        if(this.root){
            //非顶级vue实例，不需要执行
            return;
        }
        if(this.codes){
            Vue.set(this.codes,"codeSet",{})
        }
    },
    mounted() {
        //this.restoreDisplay()
    },
    methods: {
        updapteValueSet(values){
            if(!values){
                return
            }
            //顶级实例，进入设置
            if(!this.root){
                for(let key in values){
                    Vue.set(this.codes.codeSet,key,values[key])
                }
            }
            else{
                this.root.updapteValueSet(values)
            }
        },
        //获取后台setValues、setCodeValues传来的动态码，并自动转为码表值
        getValues(key){
            let arr = this.codes.codeSet[key]
            if(!arr)
                return undefined
            return arr.map(item=>{
               let itemArr =  item.split("\t")
               if(itemArr.length > 1)
                    return {label:item,value:itemArr[0]}
                else
                    return {label:itemArr[0],value:itemArr[0]}
            })
        },
        showBackendErrors(fieldErrors){
            // 清除之前的校验状态
            if(!this.getRoot().$refs.modelForm){
                return
            }
            this.getRoot().$refs.modelForm.clearValidate();
            const fields = this.getRoot().$refs.modelForm.fields;
            Utils.positioningErrorMsg(fieldErrors, fields);
            const tab = Utils.positioningErrorMsg(fieldErrors, fields);
            return tab;
        },
        loading(text){
            const loading = this.$loading({
                lock: true,
                text,
                spinner: 'el-icon-loading',
                background: 'rgba(200, 200, 200, 0.3)'
            });
            return loading
        },
        getRoot(){
            return (this.root || this)
        },
        async init(params) {
            const loading = this.loading("交易加载中")
            let rtnmsg = await Api.post(this.declareParams.trnName + "/init", { params })
            if(rtnmsg.respCode == SUCCESS){
                this.updapteValueSet(rtnmsg.codeSet)
            }
            loading.close()
            return rtnmsg
        },
        
        async save(params) {
            const loading = this.loading("正在保存交易")
            let rtnmsg = await Api.post(this.declareParams.trnName + "/saveData", this.wrapper(params))
            loading.close()
            return rtnmsg
        },
        async executeCheck(rulePath,params){
            const loading = this.loading("校验进行中")
            let rtnmsg = await  Api.post(this.declareParams.trnName+"/executeCheck/"+rulePath, this.wrapper(params))
            if(rtnmsg.respCode == SUCCESS){
                this.updapteValueSet(rtnmsg.codeSet)
                this.showBackendErrors(rtnmsg.fieldErrors)
            }
            loading.close()
            return rtnmsg
        },
        async executeDefault(rulePath, params) {
            let rtnmsg = await  Api.post(this.declareParams.trnName + "/executeDefault/" + rulePath, this.wrapper(params))
            if(rtnmsg.respCode == SUCCESS){
                this.updapteValueSet(rtnmsg.codeSet)
                this.showBackendErrors(rtnmsg.fieldErrors)
            }
            return rtnmsg
        },
        async executeRule(rulePath, params,delayCb) {
            const loading = this.loading("正在请求数据")
            let rtnmsg = await  Api.post(this.declareParams.trnName + "/executeRule/" + rulePath, this.wrapper(params,delayCb))
            if(rtnmsg.respCode == SUCCESS){
                this.updapteValueSet(rtnmsg.codeSet)
                this.showBackendErrors(rtnmsg.fieldErrors)
            }
            loading.close()
            return rtnmsg
        },
        async checkAll(params) {
            const loading = this.loading("正在校验数据")
            const rtnmsg = await Api.post(this.declareParams.trnName + "/checkAll", this.wrapper(params))
            if(rtnmsg.respCode == SUCCESS){
                this.updapteValueSet(rtnmsg.codeSet)
            }
            loading.close()
            return rtnmsg
        },
        async pedding(params) {
            const loading = this.loading("正在暂存数据")
            const rtnmsg = await  Api.post(this.declareParams.trnName + "/pending", this.wrapper(params))
            if(rtnmsg.respCode == SUCCESS){
                this.updapteValueSet(rtnmsg.codeSet)
            }
            loading.close()
            return rtnmsg
        },
        async restoreDisplay() {
            let inr = this.$route.query.trn
            if (!inr)
                return
            const loading = this.loading("快照数据加载中")
            let data = await Api.post("display/" + inr)
            Utils.copyValueFromVO(this.model, JSON.parse(data.data))
            loading.close()
        },
        async executeNotify(params) {
            const rtnmsg = await  Api.post(this.declareParams.trnName + "/executeNotify", this.wrapper(params))
            if(rtnmsg.respCode == SUCCESS){
                this.updapteValueSet(rtnmsg.codeSet)
            }
            return rtnmsg
        },
    }
}