import commonFunctions from '~/mixin/commonFunctions.js';
import commonDepend from "~/mixin/commonDepend"
import moment from 'moment';
import BigNumber from "bignumber.js";
import Api from "~/service/Api";

export default {
	mixins: [commonFunctions, commonDepend],
	methods: {
		YPTDefault(){
			if(this.model.spt && this.model.spt.channel=='YPT'){
				if (this.model.didgrp.rec.shppar=="Y") {
					this.model.didgrp.rec.shppar="ALWD";
				}
				if (this.model.didgrp.rec.shppar=="N") {
					this.model.didgrp.rec.shppar="NALW";
				}
				//转运key值变化
				if (this.model.didgrp.rec.shptrs=="Y") {
					this.model.didgrp.rec.shptrs="ALWD";
				}
				if (this.model.didgrp.rec.shptrs=="N") {
					this.model.didgrp.rec.shptrs="NALW";
				}
				if(this.model.ifMoreApl!='Y'){
					this.getAplInfo()
				}
				if(this.model.didgrp.blk.lcrgod!=""){
					this.customAddModify(this.model.didgrp.blk, 'lcrgod');
				}
				if(this.model.ifMoreApl=='Y'){
					this.$refs["modelForm"].validateField([`didgrp.apl.pts.extkey`]);
				}
			}
		},
		async sptBenDefault(){
			if(this.model.spt && this.model.spt.channel=='YPT'&&this.model.didgrp.ben.pts.cnnam!=""){
				let data = {
					adrelc: this.model.didgrp.ben.pts.cnadr,
					flg: false,
					language:"CN",
					namelc:this.model.didgrp.ben.pts.cnnam,
					rol:'ben',
					ptainr:""
				}
				let ress = await Api.post('/Domlc/ptspta/ptsTmpAdrAddAuto', data);
				if (ress.respCode === SUCCESS) {
					if(ress.data){
						this.model.didgrp.ben.cnnam = ress.data.pts.cnnam
						this.model.didgrp.ben.cnadr = ress.data.pts.cnadr
						this.model.didgrp.ben.pts.extkey = ress.data.extkey
						this.model.didgrp.ben.pts.rol = 'BEN'
						this.model.didgrp.ben.pts.ptainr = ress.data.pts.ptainr
						this.model.didgrp.ben.pts.ptyinr = ress.data.pts.ptyinr
						this.model.didgrp.ben.ptytyp = ress.data.ptytyp
						this.$set(this.codes, 'bendisabled', true);
						this.getBenInfo()
					}
				}
			}
		},
		userEsxtkeyDefault(){
			if (this.model.didgrp.rec.ownusr == "") {
				this.model.didgrp.rec.ownusr = window.sessionStorage.userName
			}
		},
		msgtypDefault() {
			if (this.model.didgrp.rec.elcflg == "Y") {
				this.model.ditp.msgtyp = "elcs.101.001.02"
			}
			else {
				this.model.ditp.msgtyp = ""
			}
		},
		// 将数字金额转换为大写金额
		smallToBig() {
			var money = this.model.didgrp.cbs.nom1.amt;
			if(isNaN(new BigNumber(money)) ){
				return
			}
			var cnNums = new Array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"); //汉字的数字
			var cnIntRadice = new Array("", "拾", "佰", "仟"); //基本单位
			var cnIntUnits = new Array("", "万", "亿", "兆"); //对应整数部分扩展单位
			var cnDecUnits = new Array("角", "分", "毫", "厘"); //对应小数部分单位
			var cnInteger = "整"; //整数金额时后面跟的字符
			var cnIntLast = "元"; //整数完以后的单位
			var integerNum; //金额整数部分
			var decimalNum; //金额小数部分
			//输出的中文金额字符串
			var chineseStr = "";
			var parts; //分离金额后用的数组,预定义
			if (money == "") {
				this.model.ditp.amt = "";
				return;
			}

			if (new BigNumber(money) == 0) {
				chineseStr = cnNums[0] + cnIntLast + cnInteger;
				this.model.ditp.amt = "人民币" + chineseStr;
				return;
			}
			//转换为字符串
			integerNum = money.substr(0, money.length - 3);
			decimalNum = money.substr(money.length - 2);
			//获取整型部分转换
			if (parseInt(integerNum, 10) > 0) {
				var zeroCount = 0;
				var IntLen = integerNum.length;
				for (var i = 0; i < IntLen; i++) {
					var n = integerNum.substr(i, 1);
					var p = IntLen - i - 1;
					var q = p / 4;
					var m = p % 4;
					if (n == "0") {
						zeroCount++;
					} else {
						if (zeroCount > 0) {
							chineseStr += cnNums[0];
						}
						//归零
						zeroCount = 0;
						chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
					}
					if (m == 0 && zeroCount < 4) {
						chineseStr += cnIntUnits[q];
					}
				}
				chineseStr += cnIntLast;
			}
			//小数部分
			if (decimalNum != "") {
				var decLen = decimalNum.length;
				for (var i = 0; i < decLen; i++) {
					var n = decimalNum.substr(i, 1);
					if (n != "0") {
						chineseStr += cnNums[Number(n)] + cnDecUnits[i];
					}
				}
			}
			if (chineseStr == "") {
				chineseStr += cnNums[0] + cnIntLast + cnInteger;
			} else if (decimalNum == "" || /^0*$/.test(decimalNum)) {
				chineseStr += cnInteger;
			}
			this.model.ditp.amt = "人民币" + chineseStr;
			this.defaultMaxamt();
		},
		defaultMaxamt() {
			if(this.model.didgrp.cbs.max.amt==""){
				this.model.didgrp.cbs.max.amt=0;
			}
			if(this.model.didgrp.cbs.nom1.amt==""){
				this.model.didgrp.cbs.nom1.amt=0;
			}
			if(isNaN(new BigNumber(this.model.didgrp.cbs.nom1.amt))||isNaN(new BigNumber(this.model.didgrp.rec.nomtop))){
				return
			}
			this.model.didgrp.cbs.max.amt = new BigNumber(this.model.didgrp.cbs.nom1.amt).multipliedBy(new BigNumber(1).plus(new BigNumber(this.model.didgrp.rec.nomtop).dividedBy(100))).toFormat(2).replace(/,/g, "");
			this.model.didgrp.cbs.opn1.amt = this.model.didgrp.cbs.max.amt;
		},
		preperDefault() {
			if((this.$route.query.businessInr&&this.$route.query.businessType=='TRN')||this.isInDisplay){
				return;
			}
			if(this.model.spt&&this.model.spt.channel=="YPT"){
				return;
			}
			if (this.model.didgrp.blk.preperflg != "X") {
				let days;
				if (this.model.didgrp.rec.expdat == "" || this.model.didgrp.rec.expdat == null || this.model.didgrp.rec.shpdat == "" || this.model.didgrp.rec.shpdat == null) {
					days = "15";
				}
				else {
					days = moment(this.model.didgrp.rec.expdat).diff(moment(this.model.didgrp.rec.shpdat), 'days')
				}
				switch (this.model.didgrp.rec.mytype) {
					case "H":
					case "":
						this.model.didgrp.blk.preper = "货物装运日后" + days + "天";
						break;
					case "F":
						this.model.didgrp.blk.preper = "服务提供日后" + days + "天"
						break;
					case "3":
						this.model.didgrp.blk.preper = "货物/服务提供日后" + days + "天"
						break;
				}
			}
			this.adlcndDefault();
		},
		lcrdocDefault() {
			let CR = "\n";
			if((this.$route.query.businessInr&&this.$route.query.businessType=='TRN')||this.isInDisplay){
				return true;
			}
			if (this.model.didgrp.blk.modifySet && this.model.didgrp.blk.modifySet.includes('lcrdoc')) {
				return;
			}
			else if(this.model.spt&&this.model.spt.channel=="YPT"){
				return;

			}
			else {
				if (this.model.didgrp.rec.elcflg == 'Y') {
					switch (this.model.didgrp.rec.mytype) {
						case "H":
						case "":
							this.model.didgrp.blk.lcrdoc = "1.税务部门统一监制的原始正本发票(包括发票联和抵扣联)。" + CR + "2.申请人出具的货物收据正本一份,显示收货人为开证申请人。"
							break;
						case "F":
							this.model.didgrp.blk.lcrdoc = "1.税务部门统一监制的原始正本发票(包括发票联和抵扣联)。" + CR + "2.申请人出具的劳务确认书正本一份。";
							break;
						case "3":
							this.model.didgrp.blk.lcrdoc = "1.税务部门统一监制的原始正本发票(包括发票联和抵扣联)。" + CR + "2.申请人出具的货物收据/劳务确认书正本一份。";
							break;
						default:
							this.model.didgrp.blk.lcrdoc = "1.税务部门统一监制的原始正本发票(包括发票联和抵扣联)。" + CR + "2.申请人出具的货物收据/劳务确认书正本一份。";
							break;
					}
				}
				else {
					if (this.model.didgrp.rec.mytype == "H" || this.model.didgrp.rec.mytype == "") {
						this.model.didgrp.blk.lcrdoc = "1.税务部门统一监制的原始正本发票(包括发票联和抵扣联)。" + CR + "2.申请人出具的货物收据正本一份,显示收货人为开证申请人。";
					}
					else {
						this.model.didgrp.blk.lcrdoc = "1.税务部门统一监制的原始正本发票(包括发票联和抵扣联)。" + CR + "2.申请人出具的劳务确认书正本一份。";
					}
				}
			}
		},
		adlcndDefault() {
			let nomton;
			let nomtop;
			let txt = "";
			let CR = "\n";
			if((this.$route.query.businessInr&&this.$route.query.businessType=='TRN')||this.isInDisplay){
				return true;
			}
			if(this.model.spt&&this.model.spt.channel=="YPT"){
				return;
			}
			else if (this.model.didgrp.blk.modifySet && this.model.didgrp.blk.modifySet.includes('adlcnd')) {
				return;
			}
			else {
				if (this.model.didgrp.rec.nomton != 0) {
					nomton = new BigNumber(this.model.didgrp.rec.nomton).toFormat(2)
				}
				if (this.model.didgrp.rec.nomtop != 0) {
					nomtop = new BigNumber(this.model.didgrp.rec.nomtop).toFormat(2)
				}
				switch (this.model.didgrp.rec.mytype) {
					case "H":
					case "":
						txt = "";
						if (this.model.didgrp.rec.nomton == 0 && this.model.didgrp.rec.nomtop == 0) {
							txt = txt + "2.货物数量及信用证金额无浮动范围。"
						}
						else {
							if (this.model.didgrp.rec.nomton != 0 && this.model.didgrp.rec.nomtop != 0) {
								txt = txt + "2.货物数量及信用证金额均可有上浮" + nomtop + "%和下浮" + nomton + "%的浮动范围。"
							}
							else {
								if (this.model.didgrp.rec.nomton != 0) {
									txt = txt + "2.货物数量及信用证金额均可有下浮" + nomton + "%的浮动范围。"
								}
								else {
									txt = txt + "2.货物数量及信用证金额均可有上浮" + nomtop + "%的浮动范围。"
								}
							}
						}
						break;
					case "F":
						txt = "";
						if (this.model.didgrp.rec.nomton == 0 && this.model.didgrp.rec.nomtop == 0) {
							txt = txt + "2.服务数量及信用证金额无浮动范围。"
						}
						else {
							if (this.model.didgrp.rec.nomton != 0 && this.model.didgrp.rec.nomtop != 0) {
								txt = txt + "2.服务数量及信用证金额均可有上浮" + nomtop + "%和下浮" + nomton + "%的浮动范围。"
							}
							else {
								if (this.model.didgrp.rec.nomton != 0) {
									txt = txt + "2.服务数量及信用证金额均可有下浮" + nomton + "%的浮动范围。"
								}
								else {
									txt = txt + "2.服务数量及信用证金额均可有上浮" + nomtop + "%的浮动范围。"
								}
							}
						}
						break;
					case "3":
						txt = "";
						if (this.model.didgrp.rec.nomton == 0 && this.model.didgrp.rec.nomtop == 0) {
							txt = txt + "2.货物/服务数量及信用证金额无浮动范围。"
						}
						else {
							if (this.model.didgrp.rec.nomton != 0 && this.model.didgrp.rec.nomtop != 0) {
								txt = txt + "2.货物/服务数量及信用证金额均可有上浮" + nomtop + "%和下浮" + nomton + "%的浮动范围。"
							}
							else {
								if (this.model.didgrp.rec.nomton != 0) {
									txt = txt + "2.货物/服务数量及信用证金额均可有下浮" + nomton + "%的浮动范围。"
								}
								else {
									txt = txt + "2.货物/服务数量及信用证金额均可有上浮" + nomtop + "%的浮动范围。"
								}
							}
						}
						break;
					default:
						txt = "";
						if (this.model.didgrp.rec.nomton == 0 && this.model.didgrp.rec.nomtop == 0) {
							txt = txt + "2.货物数量及信用证金额无浮动范围。"
						}
						else {
							if (this.model.didgrp.rec.nomton != 0 && this.model.didgrp.rec.nomtop != 0) {
								txt = txt + "2.货物数量及信用证金额均可有上浮" + nomtop + "%和下浮" + nomton + "%的浮动范围。"
							}
							else {
								if (this.model.didgrp.rec.nomton != 0) {
									txt = txt + "2.货物数量及信用证金额均可有下浮" + nomton + "%的浮动范围。"
								}
								else {
									txt = txt + "2.货物数量及信用证金额均可有上浮" + nomtop + "%的浮动范围。"
								}
							}
						}
						break;
				}
			}
			this.model.didgrp.blk.adlcnd = "1.单据必须自" + this.model.didgrp.blk.preper + "内提交,但不能晚于信用证有效期;" + CR + txt + CR + "3.开证行以外的所有银行费用由受益人承担。" + CR + "4.发票出具日期早于信用证开立日期不接受。" + CR + "5.增值税专用发票必须是原始正本,影印件、传真件不接受,即使注明正本字样。" + CR + "6.交单行或议付行(如有)应注明此次交单是在正本信用证项下进行并已在信用证正本背面批注交单情况。" + CR + "7.此证不允许保兑。" + CR + "8.其他。"
		},
		stametDefault(){
			if(this.model.didgrp.blk.stamet==""){
				this.model.didgrp.blk.stamet="本信用证为依据《国内信用证结算办法》开立的不可撤销国内信用证"
			}
		},
		benDisabled(){
			if(this.model.didgrp.ben.ptytyp=="F"){
				this.$set(this.codes, 'bendisabled', true);
			}
		}
	},
}