import Api from "~/service/Api"
import { rejrow, reprow } from "~/service/business/trnrel"
import Utils from "~/utils/index"
export default {
    async onSeaown() {
        const that = this.root;
        that.executeRule("searow").then(res => {
            //TODO 处理数据逻辑
            that.$refs.inftrnps.stmData.data = res.data.trncorco_trnstm.rows;
            this.model.trncorco.trnstm = res.data.trncorco_trnstm;
        })
    },
    async onSeajbh() {
        const that = this.root;
        that.executeRule("seajbh").then(res => {
            //TODO 处理数据逻辑
            that.$refs.inftrnps.stmData.data = res.data.trncorco_trnstm.rows;
        })
    },
    async onSeactr() {
        let rtnmsg = await this.executeRule("seactr")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    async onSearow() {
        let rtnmsg = await this.executeRule("searow")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    async onDsp() {
        let rtnmsg = await this.executeRule("dsp")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    async onImgmodNewimg() {
        let rtnmsg = await this.executeRule("imgmod_newimg")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    async onImgmodNewhisimg() {
        let rtnmsg = await this.executeRule("imgmod_newhisimg")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    async onDiaconButdia() {
        let rtnmsg = await this.executeRule("diacon_butdia")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    async onImgmodHisimg() {
        let rtnmsg = await this.executeRule("imgmod_hisimg")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    async onHandle(idx, row) {

        let trnName = row.交易代码.toLowerCase();
        let viewurl = "/review/"+trnName;

        console.log(idx);
        console.log(row);

        const operateId = await this.$store.dispatch('Transaction/setOperateFuns', {
            pass: this.onRelrow.bind(this, idx),
            refuse: this.onReprow.bind(this, idx),
        });
        // this.$router.push({ path: viewurl, query: { trn: row['INR'],  operateId: operateId } });
        this.$router.push({ name: 'Review' + trnName.charAt(0).toUpperCase() + trnName.substring(1), query: { trn: row['INR'],  operateId: operateId }, params: { prePageId: this.model.pageId } });
    },

    async onWaitDetail(idx, row) {
        let viewurl = "business/inftrnpsDetail";
        const selIds = [idx + 1];
        const selDst = "trncorco.trnstm" //列表对应后台模型中的stream
        let params = { selDst, selIds };
        this.executeRule("dsp",params).then((res) => {
            if ((res.respCode == SUCCESS)) {
                Utils.copyValueFromVO(this.model, res.data);
                sessionStorage.setItem('InftrnpsDetail', JSON.stringify(this.model));
                // this.$router.push({ path: "business/inftrnpsDetail", query: { idx: idx}});
                this.$router.push({ path: viewurl, query: {} });
            }else{
                const h = this.$createElement;
                const msg = res.respMsg||'请求执行失败!'
                this.$notify.error({ title: '错误', message: h('p',{style: 'word-break:break-all;'}, msg) })
            }
        });
    },

    async onFinishDetail(idx, row) {
        let viewurl = "business/trnfndsDetail";
        const selIds = [idx + 1];
        const selDst = "trncorco.trnstm" //列表对应后台模型中的stream
        let params = { selDst, selIds };
        this.executeRule("dsp",params).then((res) => {
            if ((res.respCode == SUCCESS)) {
                Utils.copyValueFromVO(this.model, res.data);
                sessionStorage.setItem('InftrnpsDetail', JSON.stringify(this.model));
                // this.$router.push({ path: "business/inftrnpsDetail", query: { idx: idx}});
                this.$router.push({ path: viewurl, query: {} });
            }else{
                const h = this.$createElement
                const msg = res.respMsg||'请求执行失败!'
                this.$notify.error({ title: '错误', message: h('p',{style: 'word-break:break-all;'}, msg) })
            }
        });
    },

    async onRelrow(idx) {
        this.$confirm('您确定复核该笔交易?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(async () => {
            const selIds = [idx + 1]; //rowno选中行
            const selDst = "trncorco.trnstm" //列表对应后台模型中的stream

            let params = { selDst: selDst, selIds: selIds };

            let rtnmsg = await this.executeRule("relrow", params)
            if (rtnmsg.respCode == SUCCESS) {

                let errorMsg = "";
                let fieldErrorsFlag = (JSON.stringify(rtnmsg.fieldErrors) == "{}");
                if (!fieldErrorsFlag) {
                    let errorMsgkey = "";
                    let errorMsgVal = "";
                    for (const key in rtnmsg.fieldErrors) {
                        errorMsgkey = key;
                        errorMsgVal = rtnmsg.fieldErrors[key];
                    }
                    errorMsg = errorMsgkey + ':' + errorMsgVal;
                    this.$notify.error({ title: '错误', message: '复核失败!错误信息[' + errorMsg + ']' });
                } else {
                    this.$notify({
                        title: '成功',
                        message: '复核成功',
                        type: 'success'
                    });
                    // const that = this.root;
                    // that.executeRule("searow").then(res => {
                    //     //TODO 处理数据逻辑
                    //     that.$refs.inftrnps.stmData.data = res.data.trncorco_trnstm.rows;
                    //     this.model.trncorco.trnstm = res.data.trncorco_trnstm;
                    // })
                    this.$store.dispatch("TagsView/delView", this.$route)
                    this.$router.history.push("/taskList", () => {
                        this.$store.commit("setTaskListTabVal", 'trnrel');
                        this.handleSearch()
                    });
                }
            }
            else {
                let errorMsg = "";
                let fieldErrorsFlag = (JSON.stringify(rtnmsg.fieldErrors) == "{}");
                if (!fieldErrorsFlag) {
                    let errorMsgkey = "";
                    let errorMsgVal = "";
                    for (const key in rtnmsg.fieldErrors) {
                        errorMsgkey = key;
                        errorMsgVal = rtnmsg.fieldErrors[key];
                        errorMsg = errorMsg + errorMsgkey + ':' + errorMsgVal + ";";
                    }
                   
                } else if (rtnmsg.respMsg) {
                    errorMsg = rtnmsg.respMsg;
                }
                this.$notify.error({ title: '错误', message: '复核失败!错误信息[' + errorMsg + ']' });
            }




        })

    },

    sleep(ms) {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve()
            }, ms)
        })
    },
    async onPrinte() {
        let rtnmsg = await this.executeRule("printe")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    async onReprow(idx) {
        this.$confirm('您确定退回该笔交易?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(async () => {
            const selIds = [idx + 1]; //rowno选中行
            const selDst = "trncorco.trnstm" //列表对应后台模型中的stream

            let params = { selDst: selDst, selIds: selIds };

            let rtnmsg = await reprow(this.wrapper(params))
            if (rtnmsg.respCode == SUCCESS) {

                let errorMsg = "";
                let fieldErrorsFlag = (JSON.stringify(rtnmsg.fieldErrors) == "{}");
                if (!fieldErrorsFlag) {
                    let errorMsgkey = "";
                    let errorMsgVal = "";
                    for (const key in rtnmsg.fieldErrors) {
                        errorMsgkey = key;
                        errorMsgVal = rtnmsg.fieldErrors[key];
                    }
                    errorMsg = errorMsgkey + ':' + errorMsgVal;
                    this.$notify.error({ title: '错误', message: '退回失败!错误信息[' + errorMsg + ']' });
                } else {
                    this.$notify({
                        title: '成功',
                        message: '退回成功',
                        type: 'success'
                    });
                    this.$store.dispatch("TagsView/delView", this.$route)
                    this.$router.history.push("/taskList", () => {
                        this.$store.commit("setTaskListTabVal", 'trnrel');
                        this.handleSearch()
                    });
                }
            }
            else {
                let errorMsg = "";
                let fieldErrorsFlag = (JSON.stringify(rtnmsg.fieldErrors) == "{}");
                if (!fieldErrorsFlag) {
                    let errorMsgkey = "";
                    let errorMsgVal = "";
                    for (const key in rtnmsg.fieldErrors) {
                        errorMsgkey = key;
                        errorMsgVal = rtnmsg.fieldErrors[key];
                        errorMsg = errorMsg + errorMsgkey + ':' + errorMsgVal + ";";
                    } 
                } else if (rtnmsg.respMsg) {
                    errorMsg = rtnmsg.respMsg;
                }
                this.$notify.error({ title: '错误', message: '退回失败!错误信息[' + errorMsg + ']' });
            }
        })
    },
    async onRejrow() {
        console.log(this.getSelectedData())

        this.model.trncorco.selinr['rows'] = this.getSelectedData();
        let rtnmsg = await rejrow(this.wrapper())
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑
            // var cacheFileName = rtnmsg.data.cacheFileName;
            // let rejrowRtnmsg = await Api.post("trncor/init", {params:{"cacheFileName":cacheFileName}})
            // if(rejrowRtnmsg.respCode == SUCCESS){
            //     this.$message({
            //         type: 'success',
            //         message: 'Reject成功!'
            //     });
            //     await this.sleep(2000);
            //     this.$router.go(0);
            // }

            this.$message({
                type: 'success',
                message: 'Reject成功!'
            });
            // await this.sleep(2000);
            // this.$router.go(0);

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    async onRelcor() {
        let rtnmsg = await this.executeRule("relcor")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    async onExi() {
        let rtnmsg = await this.executeRule("exi")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },

    async onRecpanDet() {
        let rtnmsg = await this.executeRule("recpan_det")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    async onSyswrnButshw() {
        let rtnmsg = await this.executeRule("syswrn_butshw")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    async onRecpanNotmap() {
        let rtnmsg = await this.executeRule("recpan_notmap")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    async onRecpanButspt() {
        let rtnmsg = await this.executeRule("recpan_butspt")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    async onRecpanInc() {
        let rtnmsg = await this.executeRule("recpan_inc")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    async onRecpanButord() {
        let rtnmsg = await this.executeRule("recpan_butord")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    async onImgmodImage() {
        let rtnmsg = await this.executeRule("imgmod_image")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    async onRecpanIncben() {
        let rtnmsg = await this.executeRule("recpan_incben")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    async onInftxtButtxmsel() {
        let rtnmsg = await this.executeRule("inftxt_buttxmsel")
        if (rtnmsg.respCode == SUCCESS) {
            //TODO 处理数据逻辑

        }
        else {
            this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
    },
    onSeainf() {

    },
    async handleSearch() {
        const that = this.root;
        // if (this.model.searchAllUsers) {
        //     that.executeRule("seajbh").then(res => {
        //         //TODO 处理数据逻辑
        //         that.$refs.inftrnps.stmData.data = res.data.trncorco_trnstm.rows;
        //     })
        // } else {
        // that.executeRule("searow").then(res => {
        //     //TODO 处理数据逻辑
        //     that.$refs.inftrnps.stmData.data = res.data.trncorco_trnstm.rows;
        //     this.model.trncorco.trnstm = res.data.trncorco_trnstm;
        // })
        // }
        if(this.model.trncorco.relflg == ''){
            this.model.trncorco.relflg = '0';
        }
        const res = await this.executeRule("inftrnps,searow")
        that.$refs.inftrnps.stmData.data = res.data.trncorco_trnstm.rows;
        this.model.trncorco.trnstm = res.data.trncorco_trnstm;
        this.$store.commit("setTaskList", {key: "trnrel", val: this.stmData.data.length});
        // if (res.data.trncorco_trnstm.rows.length == 0) {
        //     this.$notify({
        //         title: '成功',
        //         message: '搜索完毕,无满足条件记录',
        //         type: 'success'
        //     });
        // }
        this.$refs.paramsForm.clearValidate();
        Utils.positioningErrorMsg(res.fieldErrors, this.$refs.paramsForm.fields)
    },
    async trnfndhandleSearch() {
        const that = this.root;
        if(this.model.trncorco.relflg == ''){
            this.model.trncorco.relflg = '1';
        }
        const res = await this.executeRule("inftrnps,searow")
        that.$refs.inftrnps.stmData.data = res.data.trncorco_trnstm.rows;
        this.model.trncorco.trnstm = res.data.trncorco_trnstm;
 
        this.$refs.paramsForm.clearValidate();
        Utils.positioningErrorMsg(res.fieldErrors, this.$refs.paramsForm.fields)
    },

    async handleReset() {
        this.model.bchcon = "";
        this.model.trncorco.ownref = "";
        this.model.trncorco.relflg = "ALL";
        this.model.usrcon = "";
        this.model.trncorco.inidatfro = "";
        this.model.trncorco.inidattil = "";
        this.model.atp.cod = "";
        this.model.trncorco.dflg = "ALL";
        this.model.atptxt = "";
        this.model.searchAllUsers = false;
    },



}