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: {
    ELCDefault() {
      if (this.model.spt && this.model.spt.channel == 'ELC') {
        if (this.model.dedgrp.rec.shppar == "Y") {
          this.model.dedgrp.rec.shppar = "ALWD";
        }
        if (this.model.dedgrp.rec.shppar == "N") {
          this.model.dedgrp.rec.shppar = "NALW";
        }
      }
    },
        //电证来报自开自议
        async dedBydidELC() {
          if(this.isInDisplay || this.model.readflg){
            return true;
          }
          if (this.model.spt.channel== 'ELC' || this.model.spt.channel == 'YPT') {
            const loading = this.loading();
            let res = await Api.post("/Domlc/detopnRule/dedBydidELC", { ...this.model });
            if (res.respCode == SUCCESS) {
              loading.close();
              this.model.dedgrp = res.data.dedgrp;
              this.model.readflg = res.data.flg;
              if(res.data.flg=="Y"){
                this.$notify({
                  title: '成功',
                  message: "获取开证信息成功",
                  type: 'success',
                });
              }
              else if(res.data.flg=="S"){
                this.$notify({
                  title: '失败',
                  message: "非本机构数据",
                  type: 'error',
                });
              }
              else if (res.data.flg=="N"){
                this.$notify({
                  title: '失败',
                  message: "未找到该笔开证",
                  type: 'error',
                });
              }
            } else {
              loading.close();
            }
            this.OnAvbby();
            this.OnTratyp();
            this.OnMytype();
          }
        },
		// 将数字金额转换为大写金额
		smallToBig() {
			var money = this.model.dedgrp.cbs.nom1.amt;
			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.detp.upamt = "";
				return;
			}

			money = parseFloat(money);
			if (money == 0) {
				chineseStr = cnNums[0] + cnIntLast + cnInteger;
				this.model.detp.upamt = "人民币" + chineseStr;
				return;
			}

			//四舍五入保留两位小数,转换为字符串
			money = Math.round(money * 100).toString();
			integerNum = money.substr(0, money.length - 2);
			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.detp.upamt = "人民币" + chineseStr;
			this.OnMaxamt();
		},
    OnMaxamt(){
			this.model.dedgrp.cbs.max.amt = new BigNumber(this.model.dedgrp.cbs.nom1.amt).multipliedBy(new BigNumber(1).plus(new BigNumber(this.model.dedgrp.rec.nomtop).dividedBy(100))).toFormat(2).replace(/,/g, "");
			this.model.dedgrp.cbs.opn1.amt = this.model.dedgrp.cbs.max.amt;
    },
    preperDefault() {
      if(this.isInDisplay){
        return true;
}
      let modifyList = this.model.dedgrp.blk.modifySet
      if(this.model.spt.channel=='ELC'){
      }else{
        if(!modifyList || (!!modifyList && !modifyList.includes('preper'))){
          let days = " ";
          if(this.model.dedgrp.rec.expdat && this.model.dedgrp.rec.shpdat){
            days = moment(this.model.dedgrp.rec.expdat).diff(moment(this.model.dedgrp.rec.shpdat), 'days')
          }
          switch (this.model.dedgrp.rec.mytype) {
            case "H":
              this.model.dedgrp.blk.preper = "单据必须自送货单签发之日起 "+ days+" 天内提交";
              break;
            case "F":
              this.model.dedgrp.blk.preper = "单据必须自服务提供日后 "+ days+" 天内提交"
              break;
            case "3":
              this.model.dedgrp.blk.preper = "单据必须自货物/服务提供日后 "+ days+" 天内提交"
              break;
          }
        }
      }
      this.OnMytype();
    },
	},
}