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

export default {
    data() {
        return {
            codeSet:{} //setvalues的接收码表
        }
    },
    mounted() {
        //this.restoreDisplay()
    },
    methods: {
        updapteValueSet(values){
            if(!values){
                return
            }
            for(let key in values){
                Vue.set(this.codeSet,key,values[key])
            }
        },
        //获取后台setValues、setCodeValues传来的动态码
        getBackendCode(key){
            let arr = codeSet[key]
            if(!arr)
                return undefined
            return arr.map(item=>{
               let itemArr =  item.split("\t")
               if(itemArr.length > 1)
                    return {label:itemArr[0]+"\t"+itemArr[1],value:itemArr[0]}
                else
                    return {label:itemArr[0],value:itemArr[1]}
            })
        },
        async init(params) {
            let rtnmsg = await Api.post(this.declareParams.trnName + "/init", { params })
            if(rtnmsg.respCode == SUCCESS){
                this.updapteValueSet(rtnmsg.codeSet)
            }
            return rtnmsg
        },
        save(params) {
            return Api.post(this.declareParams.trnName + "/saveData", this.wrapper(params))
        },
        async executeCheck(rulePath,params){
            let rtnmsg = await  Api.post(this.declareParams.trnName+"/executeCheck/"+rulePath, this.wrapper(params))
            if(rtnmsg.respCode == SUCCESS){
                this.updapteValueSet(rtnmsg.codeSet)
            }
            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)
            }
            return rtnmsg
        },
        async executeRule(rulePath, params,delayCb) {
            let rtnmsg = await  Api.post(this.declareParams.trnName + "/executeRule/" + rulePath, this.wrapper(params,delayCb))
            if(rtnmsg.respCode == SUCCESS){
                this.updapteValueSet(rtnmsg.codeSet)
            }
            return rtnmsg
        },
        async checkAll(params) {
            const rtnmsg = await Api.post(this.declareParams.trnName + "/checkAll", this.wrapper(params))
            if(rtnmsg.respCode == SUCCESS){
                this.updapteValueSet(rtnmsg.codeSet)
            }
            return rtnmsg
        },
        pedding(params) {
            return Api.post(this.declareParams.trnName + "/pending", this.wrapper(params))
        },
        async restoreDisplay() {
            let inr = this.$route.query.trn
            if (!inr)
                return
            let data = await Api.post("display/" + inr)
            Utils.copyValueFromVO(this.model, JSON.parse(data.data))
        },
        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
        },
    }
}