import Api from '~/service/Api';
import commonFunctions from '~/mixin/commonFunctions.js';
import moment from 'moment';
export default {
	mixins: [commonFunctions],
	methods: {
    // 点击获取按钮拉取当前key字段下的表单数据
    queryFormData (key) {},
    // 业务信息=》基本信息=》保函编号-----获取保函编号
		queryOwnref () {
      let params = this.model.nidgrp.rec;
      params.ownref = this.model.nitp.tempOwnref;
      if (!this.model.nidgrp.apl.pts.ptainr || !this.model.nidgrp.rec.hndtyp || !this.model.nidgrp.rec.gartyp) {
        return
      }
      let ptainr = this.model.nidgrp.apl.pts.ptainr;
			const loading = this.loading();
			let trnName = this.root.trnName.toUpperCase()
      Api.post(`/business/ref/nid?ptaInr=${ptainr}&trnName=${trnName}`, params).then(res => {
				loading.close();
        if (res.respCode == SUCCESS) {
          this.model.nidgrp.rec.ownref = res.data;
          this.model.nitp.tempOwnref = res.data;
        } else {
          this.$message.error(res.respMsg);
        }
      }).catch(() => {
        loading.close();
      });
    },
    setOtherData(){
      if(moment(this.model.nidgrp.rec.opndat).diff(moment(new Date()), 'days')<0){
        this.model.nidgrp.rec.opndat = moment(new Date()).format('YYYY-MM-DD');
        this.$confirm('保函开立日期不得早于开立当天日期', '提示', {
					type: 'warning',
				});
      }
      if(moment(this.model.nidgrp.rec.inudat).diff(moment(this.model.nidgrp.rec.opndat), 'days')<0){
				this.$confirm('保函生效日期不得早于保函开立日期', '提示', {
					type: 'warning',
				});
				this.model.nidgrp.rec.inudat = moment(this.model.nidgrp.rec.opndat).add(4, 'day').format('YYYY-MM-DD')
			}
    },
    changeBencnt(){
      let list = this.model.ptyList;
      if(this.model.nidgrp.ben.pts.extkey){
          this.model.nidgrp.bencnt = 1 + list.length;
      }else{
          this.model.nidgrp.bencnt = list.length;
			}
			// 已选海关移至其他海关中未添加列
			if(this.model.nidgrp.ben.pts.extkey){
				if(this.codes.markList) {
					this.codes.markList.forEach((item) => {
						if(this.model.nidgrp.ben.pts.extkey == item.extkey && this.checkedList.includes(item.extkey)) {
							let curIndex = this.checkedList.indexOf(item.extkey)
							this.checkedList.splice(curIndex, 1);
						}
					})
				}
				this.model.nidgrp.rec.ptyhgq = this.formatCheckedToName()
			}
		},
		formatCheckedToName () {
			let nameStr = ''
			let nameList = []
			if(this.codes.markList) {
				this.codes.markList.forEach(item => {
					if(this.checkedList.includes(item.extkey)) {
						nameList.push(item.extkey);
					}
				})
			}
			nameStr = nameList.join('@')
			return nameStr
		},
		formatNameToChecked (ptyhgq) {
			this.checkedList = []
			if(ptyhgq){
				this.checkedList = ptyhgq.split('@');
			}
		},
		onBccls1(){
      if (this.model.addbcb.bccls1 == 'N') {
        this.model.addbcb.expdat = this.model.nidgrp.rec.expdat;
      } else if (this.model.addbcb.bccls1 == 'Y') {
        this.model.addbcb.preexpdat = this.model.nidgrp.rec.expdat;
      }
    },
		// 保函模板转文本
		templateToText () {
			let gtxinr = this.model.nidgrp.rec.gtxinr
			let textTempData = this.model.textTempData
			this.formatTxtp(this.model.nidgrp.blk.gtxgidtxt, gtxinr, 'gtx', textTempData, 'nidgrp')
		},
		changeTxt () {
      if(!this.model.nidgrp.blk.modifySet || !this.model.nidgrp.blk.modifySet.includes("gidtxt")){
        this.templateToText()
      }
    },
    async agreementSigning() {
			let data = {
				ftynum: this.model.nitp.yptgimod.img[0].ftynum
			}
			let res = await Api.post('/business/nitopnRule/agreementSigning', data);
			if (res.respCode == "AAAAAA") {
				if (res.data && res.data.data == "Y") {
					this.model.nitp.xyflg = res.data.data;
					this.$notify({
						title: "成功",
						message: "协议盖章成功!",
						type: "success",
					});
				}
				else {
					this.$notify({
						title: '失败',
						message: '协议盖章调用失败,错误信息:' + res.data.respMsg,
						type: 'error',
					});
				}
			}
			else {
				this.$notify({
					title: '失败',
					message: '协议盖章调用失败!',
					type: 'error',
				});
			}
		},
		async showText() {
			let data = this.model.nitp.yptgimod.img;
			let res = await Api.post('/business/nitopnRule/getTextResultVo', data);
			if (res.respCode == "AAAAAA") {
				//res.data 内容为 TextResultVo 
				if (res.data.data) {
					let type, code = res.data;
					switch (res.data.extension) {
						case "pdf": {
							type = "application/pdf"
							this.base64ToBlob(code,type)
							break;
						}
						case "html": {
							type = "text/html"
							this.base64ToBlob(code,type)
							break;
						}
						case "doc": {
							type = "application/msword"
							this.base64ToBlob(code,type)
							break;
						}
						case "docx": {
							type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
							this.base64ToBlob(code,type)
							break;
						}
						case "xls": {
							type = "application/vnd.ms-excel"
							this.base64ToBlob(code,type)
							break;
						}
						case "xlsx": {
							type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
							this.base64ToBlob(code,type)
							break;
						}
						case "txt": {
							type = "text/plain"
							this.base64ToBlob(code,type)
							break;
						}
						case "gif": {
							type = "image/gif"
							this.base64ToBlob(code,type)
							break;
						}
						case "jpeg": {
							type = "image/jpeg"
							this.base64ToBlob(code,type)
							break;
						}
						case "jpg": {
							type = "image/jpeg"
							this.base64ToBlob(code,type)
							break;
						}
						case "png": {
							type = "image/png"
							this.base64ToBlob(code,type)
							break;
						}
						case "bmp": {
							type = "image/bmp"
							this.base64ToBlob(code,type)
							break;
						}
						case "aac": {
							type = "audio/aac"
							this.base64ToBlob(code,type)
							break;
						}
						case "abw": {
							type = "application/x-abiword"
							this.base64ToBlob(code,type)
							break;
						}
						case "arc": {
							type = "application/x-freearc"
							this.base64ToBlob(code,type)
							break;
						}
						case "avi": {
							type = "video/x-msvideo"
							this.base64ToBlob(code,type)
							break;
						}
						case "jar": {
							type = "application/java-archive"
							this.base64ToBlob(code,type)
							break;
						}
						case "js": {
							type = "text/javascript"
							this.base64ToBlob(code,type)
							break;
						}
						case "json": {
							type = "application/json"
							this.base64ToBlob(code,type)
							break;
						}
						case "mp3": {
							type = "audio/mpeg"
							this.base64ToBlob(code,type)
							break;
						}
						case "ppt": {
							type = "application/vnd.ms-powerpoint"
							this.base64ToBlob(code,type)
							break;
						}
						case "pptx": {
							type = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
							this.base64ToBlob(code,type)
							break;
						}
						case "rar": {
							type = "application/x-rar-compressed"
							this.base64ToBlob(code,type)
							break;
						}
						case "svg": {
							type = "image/svg+xml"
							this.base64ToBlob(code,type)
							break;
						}
						case "tar": {
							type = "application/x-tar"
							this.base64ToBlob(code,type)
							break;
						}
						case "zip": {
							type = "application/zip"
							this.base64ToBlob(code,type)
							break;
						}
						case "7z": {
							type = "application/x-7z-compressed"
							this.base64ToBlob(code,type)
							break;
						}
						default: {
							break;
						}
					}
					this.$notify({
						title: "成功",
						message: "文件下载成功!",
						type: "success",
					});
				}
				else {
					this.$notify({
						title: '失败',
						message: '文件下载失败!',
						type: 'error',
					});
				}
			}
		},
		base64ToBlob(code, type) {
			if(type == 'application/pdf') {
				code.data = code.data.replace(/[\n\r]/g, "")
			}
			const raw = window.atob(code.data);
			const rawLength = raw.length;
			const uInt8Array = new Uint8Array(rawLength);
			for (let i = 0; i < rawLength; ++i) {
				uInt8Array[i] = raw.charCodeAt(i);
			}
			const blob = new Blob([uInt8Array], { type: type });
			// const fileURL = URL.createObjectURL(blob);
			// window.open(fileURL)
			let fileName = code.filename;
			var downloadElement = document.createElement('a');
      var href = window.URL.createObjectURL(blob);
      downloadElement.href = href;
      downloadElement.download = fileName;
      document.body.appendChild(downloadElement);
      downloadElement.click();
      document.body.removeChild(downloadElement);
      window.URL.revokeObjectURL(href);
		},
	}
};