import Api from '~/service/Api';
import commonFunctions from '~/mixin/commonFunctions.js';
import Utils from "~/utils/index";
import Pts from '~/components/business/commonModel/Pts';
import moment from 'moment';
import BigNumber from 'bignumber.js';
import commonDepend from "~/mixin/commonDepend";
export default {
	mixins: [commonFunctions, commonDepend],
	methods: {
		async queryOwnref() {
			let params = this.model.bddgrp.rec;
			const loading = this.loading();
			let res = await Api.post('/Domlc/ref/bdd', params);
			if (res.respCode == SUCCESS) {
				loading.close();
				this.model.bddgrp.rec.ownref = res.data;
			} else {
				loading.close();
			}

		},
		//前端模拟分页
		paginate(pageNumber,pageSize){
			const startIndex=(pageNumber-1)*pageSize;
			const endIndex=startIndex+pageSize;
			return this.model.imgInvmod.invlst.slice(startIndex,endIndex);
		},
		async getBfdInfo() {
			if (this.model.bddgrp.rec.ownref == '') {
        this.$message({type: "warning",message: "请先输入交易Reference编号"});
        return;
      }
			let datas = {
				ownref: this.model.bddgrp.prb.pts.ref,
				bddownref: this.model.bddgrp.rec.ownref,
			}
			const loading = this.loading();
			let res = await Api.post('/Domlc/ditdckRule/getBfdInfo', datas);
			if (res.respCode == SUCCESS) {
				loading.close();
				if (res.data.flag == "Y") {
					if (this.model.bddgrp.rec.modifySet &&this.model.bddgrp.rec.modifySet.includes("invamt")) {
					}
					else{
						this.model.bddgrp.rec.invamt = res.data.bfdgrp.rec.invamt;
					}
					// if(this.model.bddgrp.prb.pts.bankno==""&&res.data.bfdgrp.rec.docprbrol!=""){
					// 	this.model.bddgrp.rec.docprbrol = res.data.bfdgrp.rec.docprbrol;
					// 	this.calcPrb();
					// }
					if (this.model.spt && this.model.spt.channel!= "") {
						if (this.model.bddgrp.cbs.max.amt == "" || this.model.bddgrp.cbs.max.amt == 0) {
							this.model.bddgrp.cbs.max.amt = res.data.bfdgrp.cbs.max.amt;
						}
					}
					else {
						this.model.bddgrp.cbs.max.amt = res.data.bfdgrp.cbs.max.amt;
					}
					if (this.model.bddgrp.blk.docdis == "") {
						this.model.bddgrp.blk.docdis = res.data.bfdgrp.blk.docdis;
						this.customAddModify(this.model.bddgrp.blk, 'docdis');
					}
					if (this.model.bddgrp.blk.comcon == "") {
						this.model.bddgrp.blk.comcon = res.data.bfdgrp.blk.comcon;
					}
					if (this.model.bddgrp.blk.accrmk == "") {
						this.model.bddgrp.blk.accrmk = res.data.bfdgrp.blk.accrmk;
					}
					if (this.model.bddgrp.rec.invref == "") {
						this.model.bddgrp.rec.invref = res.data.bfdgrp.rec.invref;
					}
					if (this.model.bddgrp.rec.invtyp == "") {
						this.model.bddgrp.rec.invtyp = res.data.bfdgrp.rec.invtyp;
					}
					if (this.model.bddgrp.rec.matdat == "" || this.model.bddgrp.rec.matdat == null) {
						this.model.bddgrp.rec.matdat = res.data.bfdgrp.rec.matdat;
					}
					if (this.model.bddgrp.rec.shpdat == "" || this.model.bddgrp.rec.shpdat == null) {
						this.model.bddgrp.rec.shpdat = res.data.bfdgrp.rec.shpdat;
					}
					if (this.model.bddgrp.rec.predat == "" || this.model.bddgrp.rec.predat == null) {
						this.model.bddgrp.rec.predat = res.data.bfdgrp.rec.predat;
					}
					if(this.model.spt && this.model.spt.channel == '' && this.model.invflg != "N"){
						this.model.imgInvmod.invlst = this.model.imgInvmod.invlst.concat(res.data.invuseList);
						this.model.imgInvmod.pageTable.total=this.model.imgInvmod.invlst.length
						this.model.imgInvmod.pageTable.tableData = this.paginate(this.model.imgInvmod.pageTable.currentPage,this.model.imgInvmod.pageTable.pageSize);
						this.model.imgInvmod.imguseList = this.model.imgInvmod.imguseList.concat(res.data.imguseList);
						this.model.imgInvmod.isOneSelf = true
						this.model.invflg = "N"
					}
					if (res.data.bfdgrp.blk.prsdoc != "") {
						if (!this.model.bdtp.docgrdm.docgrd || this.model.bdtp.docgrdm.docgrd.length == 0 || (this.model.bdtp.docgrdm.docgrd.length == 1 && !this.model.bdtp.docgrdm.docgrd[0].cmail1 && !this.model.bdtp.docgrdm.docgrd[0].cmail2)) {
							this.model.bdtp.docgrdm.docgrd=[];
							let tempItem = res.data.bfdgrp.blk.prsdoc.split("\r\n");
							for (var i = 0; i < tempItem.length; i++) {
								let tempItem2 = tempItem[i].split("\t")
								if (tempItem2.length >= 3) {
									this.model.bdtp.docgrdm.docgrd.push({
										'cmail1': (tempItem2[0] == "" || tempItem2[0] == null || tempItem2[0] == "null") ? "" : tempItem2[0],
										'cmail2': (tempItem2[1] == "" || tempItem2[1] == null || tempItem2[1] == "null") ? "" : tempItem2[1],
										'docnam': (tempItem2[2] == "" || tempItem2[2] == null || tempItem2[2] == "null") ? "" : tempItem2[2],
									});
								}
							}
						}
					}
					if(res.data.bfdgrp.skh){
						if(this.model.bddgrp.skh.pts.bankno==""){
							this.model.bddgrp.skh.pts.bankno=res.data.bfdgrp.skh.pts.bankno;
							this.model.bddgrp.skh.pts.jigomc=res.data.bfdgrp.skh.pts.jigomc;
						}
					}
					this.$notify({
						title: '成功',
						message: "获取寄单信息成功",
						type: 'success',
					});
				}
				else if (res.data.flag == "F") {
					this.$notify({
						title: '失败',
						message: "非本机构数据",
						type: 'error',
					});
				}
				else {
					this.$notify({
						title: '失败',
						message: "未找到该笔寄单",
						type: 'error',
					});
				}
			} else {
				loading.close();
			}

		},
		calcPrb() {
			let ptsref = this.model.bddgrp.prb.pts.ref;
			switch (this.model.bddgrp.rec.docprbrol) {
				case "ADV":
					Utils.copyValueFromVoData(this.model.bddgrp.prb.pts, this.model.didgrp.adv.pts);
					this.model.bddgrp.prb.pts.rol = "PRB";
					break;
				case "BEN":
					Utils.copyValueFromVoData(this.model.bddgrp.prb.pts, this.model.didgrp.ben.pts);
					this.model.bddgrp.prb.pts.rol = "PRB";
					this.model.bddgrp.prb.pts.bankno = this.model.didgrp.beb.pts.bankno;
					this.model.bddgrp.prb.pts.jigomc = this.model.didgrp.beb.pts.jigomc;
					this.model.bddgrp.prb.pts.dizhii = this.model.didgrp.beb.pts.dizhii;
					break;
				case "PRB":
					this.model.bddgrp.prb.pts = new Pts().data;
					this.model.bddgrp.prb.pts.rol = "PRB";
					break;
				default:
					this.model.bddgrp.prb.pts = new Pts().data;
					this.model.bddgrp.prb.pts.rol = "PRB";
					break;
			}
			if (ptsref != "") {
				this.model.bddgrp.prb.pts.ref = ptsref;
			}
		},
		maxamtChange() {
			this.model.bddgrp.cbs.opn1.amt = this.model.bddgrp.cbs.max.amt
			this.model.imgInvmod.docamt = this.model.bddgrp.cbs.max.amt
			this.root.$refs["modelForm"].validateField([`bddgrp.rec.invamt`]);
		},
		invamtChange(){
			this.root.$refs["modelForm"].validateField([`bddgrp.cbs.max.amt`]);
		},
		prbrefChange() {
			// if(this.model.bddgrp.prb.pts.extkey.replace(/^\s*|\s*$/g,"").indexOf("EVERCNBJ")==0&&this.model.bddgrp.prb.pts.ref!=""){
			this.getBfdInfo();
			// }
		},
		prbChange() {
			// if(this.model.bddgrp.prb.pts.extkey.replace(/^\s*|\s*$/g,"").indexOf("EVERCNBJ")==0&&this.model.bddgrp.prb.pts.ref!=""){
			this.getBfdInfo();
			// }
		},
		rcvdatChange() {
			this.rcvdatDefault();
			this.root.$refs["modelForm"].validateField([`bddgrp.rec.matdat`]);
		},
		matdatChange() {
			this.matdatDefault();
		}

	}
}