import Utils from '~/utils';
import commonFunctions from '~/mixin/commonFunctions.js';
import BigNumber from "bignumber.js";
import commonDepend from "~/mixin/commonDepend";
import moment from "moment";
export default {
	mixins: [commonFunctions, commonDepend],
	methods: {


		/**
		 *   source:letdrw.0040.script
		 */
		defaultBedgrpAplN1000(){

			// APL address always reset from LE, but reference has to be used from BE, if already set
			Utils.copyValueFromVoData(this.model.bedgrp.apl.pts, this.model.ledgrp.apl.pts);

		},

		/**
		 *   source:letdrw.0044.script
		 */

		defaultBedgrpBenN1000(){

			// always get ben from parent LE
			Utils.copyValueFromVoData(this.model.bedgrp.ben.pts, this.model.ledgrp.ben.pts);
			if(this.model.bedgrp.rec.docprbrol===''){
				this.model.bedgrp.rec.docprbrol = 'BEN';
			}

		},

		/**
		 *   source:letdrw.0045.script
		 */
		defaultBedgrpIssN1000(){
			// always get ISS from parent LE
			Utils.copyValueFromVoData(this.model.bedgrp.iss.pts, this.model.ledgrp.iss.pts);
		},

		/**
		 *   source:letdrw.0046.script
		 */
		defaultBedgrpRmbN1000(){
			// always get RMB from parent LE
			Utils.copyValueFromVoData(this.model.bedgrp.rmb.pts, this.model.ledgrp.rmb.pts);
		},

		max2AmtChange(){
			this.model.bedgrp.cbs.opn2.amt =  new BigNumber(this.model.oldbedgrp.cbs.opn2.amt)
				.plus(new BigNumber(this.model.bedgrp.cbs.max2.amt))
				.minus(new BigNumber(this.model.oldbedgrp.cbs.max2.amt)).toFixed(2);
			this.defaultBedgrpBlkDocdis();
		},

		defaultBedgrpBlkDocdis() {

			// BEDGRP\BLK\DOCDIS
			// it is on purpose that we don`t use translatable literals, because the ultimate
			// destination of this text is a foreign bank and usually via SWIFT !

			let descr = "";
			let CR = '\n';
			if (this.model.bedgrp.rec.shpdat != null && this.model.ledgrp.rec.shpdat != null
				&& moment(this.model.bedgrp.rec.shpdat).diff(moment(this.model.ledgrp.rec.shpdat), 'days') > 0) {
				descr = descr + 'Late shipment' + CR;
			}

			if (this.model.bedgrp.rec.rcvdat != null && this.model.ledgrp.rec.expdat != null
				&& moment(this.model.bedgrp.rec.rcvdat).diff(moment(this.model.ledgrp.rec.expdat), 'days') > 0) {
				descr = descr + 'Documents received after expiry of the LC.' + CR;
			}

			if (this.model.bedgrp.cbs.max.amt != '' && this.model.ledgrp.cbs.opn1.amt != ''
				&& new BigNumber(this.model.bedgrp.cbs.max.amt).comparedTo(this.model.ledgrp.cbs.opn1.amt) > 0) {
				let exces = this.model.bedgrp.cbs.max.cur + " " + this.formatCurrency(new BigNumber(this.model.bedgrp.cbs.max.amt).minus(this.model.ledgrp.cbs.opn1.amt), this.model.bedgrp.cbs.max.cur)
				descr = descr + 'The LC is overdrawn by ' + exces + CR;
			}

			if (this.model.bedgrp.cbs.max2.amt != '' && this.model.ledgrp.cbs.opn2.amt != ''
				&& new BigNumber(this.model.bedgrp.cbs.max2.amt).comparedTo(this.model.ledgrp.cbs.opn2.amt) > 0) {
				let exces = this.model.bedgrp.cbs.max.cur + " " + this.formatCurrency(new BigNumber(this.model.bedgrp.cbs.max2.amt).minus(this.model.ledgrp.cbs.opn2.amt), this.model.bedgrp.cbs.max2.cur)
				descr = descr + 'The additional amount is overdrawn by ' + exces + CR;
			}

			if (this.model.ledgrp.rec.shppar === 'N' && (this.model.ledgrp.rec.utlnbr != '' && new BigNumber(this.model.ledgrp.rec.utlnbr).comparedTo(1) >= 0)) {
				descr = descr + 'Partial shipment effected.' + CR;
			}

			if (this.model.bedgrp.rec.doctypcod === 'P') {
				if (this.model.ledgrp.rec.avbby === 'A' || this.model.ledgrp.rec.avbby === 'D') {
					descr = descr + 'Sight draft presented instead of usance.' + CR;
				}
				if (this.model.ledgrp.rec.avbby === 'M') {
					descr = descr + 'Only sight draft presented instead of mixed payment.' + CR;
				}
			}

			if (this.model.bedgrp.rec.doctypcod === 'A' || this.model.bedgrp.rec.doctypcod === 'D') {
				if (this.model.ledgrp.rec.avbby === 'P') {
					descr = descr + 'Usance draft presented instead of sight.' + CR;
				}
				if (this.model.ledgrp.rec.avbby === 'M') {
					descr = descr + 'Only usance drafts presented instead of mixed payment.' + CR;
				}
			}

			if (this.model.bedgrp.rec.doctypcod === 'M') {
				if (this.model.ledgrp.rec.avbby === 'P') {
					descr = descr + 'Mixed payment drafts presented instead of sight.' + CR;
				}
				if (this.model.ledgrp.rec.avbby === 'A' || this.model.ledgrp.rec.avbby === 'D') {
					descr = descr + 'Mixed payment drafts presented instead of usance.' + CR;
				}
			}

			this.model.bedgrp.blk.docdis = descr;
		},

		formatCurrency(value, cur) {
			if (value !== null && value !== undefined && (value === "" || value.toString())) {
				if (value === "") {
					value = "0";
				}

				value = value.toString();

				let sign = true;
				let array = [];
				if (value.charAt(0) == "-") {
					sign = false;
					array = value.slice(1).split(".");
				} else {
					array = value.split(".");
				}
				// 增加逗号
				var temp = "";
				while (array[0].length > 3) {
					temp =
						"," +
						array[0].substring(array[0].length - 3, array[0].length) +
						temp;
					array[0] = array[0].substring(0, array[0].length - 3);
				}
				temp = array[0] + temp;
				if (this.curPrecision(cur) == 0) {
					return sign ? temp : "-" + temp;
				}
				if (this.curPrecision(cur) <= 0) {
					temp = temp;
				} else if (array.length === 1) {
					// 没有小数位数,补全 0
					temp += ".";
					for (var i = 0; i < this.curPrecision(cur); i++) {
						temp += "0";
					}
				} else if (array[1].length < this.curPrecision(cur)) {
					temp += "." + array[1];
					// 精度不足补全 0
					for (var i = array[1].length; i < this.curPrecision(cur); i++) {
						temp += "0";
					}
				} else {
					temp += "." + array[1].substring(0, this.curPrecision(cur));
				}

				return sign ? temp : "-" + temp;
			}
		},

		curPrecision(cur) {
			if (cur === "KRW" || cur === "JPY") {
				return 0;
			} else {
				return 2;
			}
		},

		defaultFolwupopt() {
			// folwupopt is not modified
			if (!this.model.betp.modifySet || this.model.betp.modifySet == null || (this.model.betp.modifySet != null && !this.model.betp.modifySet.includes('folwupopt'))) {
				if (this.model.bedgrp.blk.docdis!=null && this.model.bedgrp.blk.docdis!='') {
					this.model.betp.folwupopt = "S";//# Send Advice of Discrepancy
				} else {
					this.model.betp.folwupopt = "W"; // # Wait for Response
				}
			}

		},

	},
}