import Api from '~/service/Api';
import commonFunctions from '~/mixin/commonFunctions.js';
import operationFunc from "~/mixin/commonDepend";
export default {
	mixins: [commonFunctions, operationFunc],
	methods: {
		async getOwnExtkeyCondition() {
			const loading = this.loading();
			let res = await Api.post('/business/nitopnRule/getOwnExtkeyCondition');
			loading.close();
			if (res.respCode === SUCCESS) {
				this.ownExtkeyList = res.data;
			}
		},
		async getDecnum(ownextkey) {
			let res = await Api.post('/report/cfabopsup/getDecnum', ownextkey);
			if (res.respCode === SUCCESS) {
				this.model.cfagit.recgrp.bas.guarantorcode = res.data;
			}
		},
		dclflgC(val) {
			if (!val) {
				this.model.cfagit.recgrp.dcl = [
					{
						exguarancode: '',
						actiontype: '',
						wabachandate: '',
						basere: '',
						remark: '',
						actiondesc: '',
						acp: '',
						sta: ''
					}
				]
			}
		},

		benamen() {
			this.$nextTick(() => {
				this.root.$refs['modelForm'].validateField(['cfagit.recgrp.bas.bename']);
			})
		},
		changeGuedparecom() {
      this.$nextTick(() => {
        this.root.$refs['modelForm'].validateField(['cfagit.recgrp.bas.guedparecompname']);
        this.root.$refs['modelForm'].validateField(['cfagit.recgrp.bas.guedparecompnamen']);
			})
    },
		 //资金用途
		 changeFounds() {
      this.root.$refs['modelForm'].validateField(['cfagit.recgrp.bas.founds1']);
      this.root.$refs['modelForm'].validateField(['cfagit.recgrp.bas.founds2']);
      this.root.$refs['modelForm'].validateField(['cfagit.recgrp.bas.founds3']);
      this.root.$refs['modelForm'].validateField(['cfagit.recgrp.bas.founds4']);
      this.root.$refs['modelForm'].validateField(['cfagit.recgrp.bas.founds5']);
      this.root.$refs['modelForm'].validateField(['cfagit.recgrp.bas.founds6']);
      this.root.$refs['modelForm'].validateField(['cfagit.recgrp.bas.founds7']);
      this.root.$refs['modelForm'].validateField(['cfagit.recgrp.bas.founds8']);
    },
		guednamen() {
			this.$nextTick(() => {
				this.root.$refs['modelForm'].validateField(['cfagit.recgrp.bas.guedname']);
			})
		},
		dclflgChange(val) {
			if (val == '') {
				this.model.cfagit.recgrp.dcl = [
					{
						exguarancode: '',
						actiontype: '',
						wabachandate: '',
						basere: '',
						remark: '',
						actiondesc: '',
						acp: '',
						sta: ''
					}
				]
			} else {
				this.model.cfagit.recgrp.dcl = [
					{
						exguarancode: '',
						actiontype: 'A',
						wabachandate: '',
						basere: '',
						remark: '',
						actiondesc: '',
						acp: 'X',
						sta: ''
					}
				]
			}
		},
		guappnamen() {
			this.$nextTick(() => {
				this.root.$refs['modelForm'].validateField(['cfagit.recgrp.bas.guappname']);
			})
		},
		async commitAdd() {
			this.$refs['modelForm'].validate(async (validStatic) => {
				if (validStatic) {
					const loading = this.loading('正在校验数据');
					const rtnmsg = await Api.post("/report/cfabopsup/cfacheck", this.model.cfagit);
					if (rtnmsg.respCode == SUCCESS) {
						const fieldErrors = rtnmsg.data;
						if (fieldErrors && Object.keys(fieldErrors).length == 0) {
							// 清除之前的校验状态
							this.$refs.modelForm.clearValidate();
							loading.close();
							let rtnmsg = await Api.post("/report/cfabopsup/giaddsav",
								{
									...this.model.cfagit,
									ownOrgDepartmentNumber: this.$store.state.UserContext.currentOrg.departmentNumber
								});
							if (rtnmsg.respCode == SUCCESS) {
								this.$notify({
									title: '成功',
									message: '提交成功',
									type: 'success',
								});
								this.$store.commit('delTagsArry', this.$route.path);
								this.$router.back()
							}
							else {
								this.$notify({
									title: "错误",
									message: res.respMsg,
									type: "error",
								});
							}
							return
						}
						const tab = this.showBackendErrors(fieldErrors);
						if (tab) {
							// 判断校验失败的表单不属于当前的tab,则切换tab到对应报错的tab页面
							if (tab.name !== this.tabVal) {
								this.tabClick(tab, '1');
							}
						}
						loading.close();
					}
				} else {
					// 前端校验失败
					this.$notify({
						title: '失败',
						message: '校验失败',
						type: 'error',
					});
			}
		})
		},
		async commitMod() {
			this.$refs['modelForm'].validate(async (validStatic) => {
				if (validStatic) {
					const loading = this.loading('正在校验数据');
					const rtnmsg = await Api.post("/report/cfabopsup/cfacheck", this.model.cfagit);
					if (rtnmsg.respCode == SUCCESS) {
						const fieldErrors = rtnmsg.data;
						if (fieldErrors && Object.keys(fieldErrors).length == 0) {
							// 清除之前的校验状态
							this.$refs.modelForm.clearValidate();
							loading.close();
							let rtnmsg = await Api.post("/report/cfabopsup/gimodsav",
								{
									...this.model.cfagit,
									ownOrgDepartmentNumber: this.$store.state.UserContext.currentOrg.departmentNumber
								});
							if (rtnmsg.respCode == SUCCESS) {
								this.$notify({
									title: '成功',
									message: '提交成功',
									type: 'success',
								});
								this.$store.commit('delTagsArry', this.$route.path);
								this.$router.back()
							}
							else {
								this.$notify({
									title: "错误",
									message: res.respMsg,
									type: "error",
								});
							}
							return
						}
						const tab = this.showBackendErrors(fieldErrors);
						if (tab) {
							// 判断校验失败的表单不属于当前的tab,则切换tab到对应报错的tab页面
							if (tab.name !== this.tabVal) {
								this.tabClick(tab, '1');
							}
						}
						loading.close();
					}
				} else {
					// 前端校验失败
					this.$notify({
						title: '失败',
						message: '校验失败',
						type: 'error',
					});
				}
			})
		},
		async commitDel() {
			let txt = ''
			let delbas =false
			let deldcl =false
			let delvrf =false
			if(this.model.cfagit.basflg){
				txt ='签约信息'
			}
			let dcl = this.model.cfagit.recgrp.dcl || []
			let vrf = this.model.cfagit.recgrp.vrf || []
			for (let index = 0; index < dcl.length; index++) {
				if(dcl[index].acp){
					deldcl = true
					txt = txt + "变动信息"+Number(index+1)
				}	
			}
			for (let indexs = 0; indexs < vrf.length; indexs++) {
				if(vrf[indexs].acp){
					delvrf = true
					txt = txt + "履约信息"+Number(indexs+1)
				}	
			}
			if(this.model.cfagit.basflg == 'X'){
				delbas =true
			}
			if(!delbas && !(deldcl || delvrf)){
				this.$confirm('没有删除数据,是否退出?', '提示', {
          confirmButtonText: '是',
          cancelButtonText: '否',
          type: 'warning'
        }).then(() => {
					this.$store.commit('delTagsArry', this.$route.path);
					this.$router.back()
        }).catch(() => {         
        });
			}
			if(txt){
				this.$confirm(`系统将删除${txt},是否确认`, '提示', {
          confirmButtonText: '是',
          cancelButtonText: '否',
          type: 'warning'
        }).then(() => {
						this.commit()
        }).catch(() => {         
        });
			}
		},
		async commit(){
			this.$refs['modelForm'].validate(async (validStatic) => {
				if (validStatic) {
					const loading = this.loading('正在校验数据');
					const rtnmsg = await Api.post("/report/cfabopsup/cfacheck", this.model.cfagit);
					if (rtnmsg.respCode == SUCCESS) {
						const fieldErrors = rtnmsg.data;
						if (fieldErrors && Object.keys(fieldErrors).length == 0) {
							// 清除之前的校验状态
							this.$refs.modelForm.clearValidate();
							loading.close();
							let rtnmsg = await Api.post("/report/cfabopsup/gidelsav",
								{
									...this.model.cfagit,
									ownOrgDepartmentNumber: this.$store.state.UserContext.currentOrg.departmentNumber
								});
							if (rtnmsg.respCode == SUCCESS) {
								this.$notify({
									title: '成功',
									message: '提交成功',
									type: 'success',
								});
								this.$store.commit('delTagsArry', this.$route.path);
								this.$router.back()
							}
							else {
								this.$notify({
									title: "错误",
									message: res.respMsg,
									type: "error",
								});
							}
							return
						}
						const tab = this.showBackendErrors(fieldErrors);
						if (tab) {
							// 判断校验失败的表单不属于当前的tab,则切换tab到对应报错的tab页面
							if (tab.name !== this.tabVal) {
								this.tabClick(tab, '1');
							}
						}
						loading.close();
					}
				} else {
					// 前端校验失败
					this.$notify({
						title: '失败',
						message: '校验失败',
						type: 'error',
					});
				}
			})
		},
		check() {
			// 前端检验
			this.$refs['modelForm'].validate(async (validStatic) => {
				if (validStatic) {
					const loading = this.loading('正在校验数据');
					const rtnmsg = await Api.post("/report/cfabopsup/cfacheck", this.model.cfagit);
					if (rtnmsg.respCode == SUCCESS) {
						const fieldErrors = rtnmsg.data;
						if (fieldErrors && Object.keys(fieldErrors).length == 0) {
							// 清除之前的校验状态
							this.$refs.modelForm.clearValidate();
							this.$notify({
								title: "成功",
								message: "校验成功",
								type: "success",
							});
							loading.close();
							return
						}
						const tab = this.showBackendErrors(fieldErrors);
						if (tab) {
							// 判断校验失败的表单不属于当前的tab,则切换tab到对应报错的tab页面
							if (tab.name !== this.tabVal) {
								this.tabClick(tab, '1');
							}
						}
						loading.close();
					}
				} else {
					// 前端校验失败
					this.$notify({
						title: '失败',
						message: '校验失败',
						type: 'error',
					});
				}
			})
		},
		changeDcl() {
			let count = Number(this.model.newdcl)
			let num = Number(this.model.olddcl)
			let sum = Number(this.model.cfagit.recgrp.dcl.length)
			let cha = sum - num
			if (count > cha) {
				for (let i = 0; i < (count - cha); i++) {
					this.model.cfagit.recgrp.dcl.push({
						exguarancode: '',
						actiontype: 'A',
						wabachandate: '',
						basere: '',
						remark: '',
						actiondesc: '',
						acp: 'X',
						sta: ''
					});
				}
			} else {
				//删除多的标签
				this.model.cfagit.recgrp.dcl.splice(num + count, cha - count)
			}
		},
		
		changeVrf() {
			let count = Number(this.model.newvrf)
			let num = Number(this.model.oldvrf)
			let sum = Number(this.model.cfagit.recgrp.vrf.length)
			let cha = sum - num
			if (count > cha) {
				for (let i = 0; i < (count - cha); i++) {
					this.model.cfagit.recgrp.vrf.push({
						exguarancode: '',
						complianceno: '',
						actiontype: 'A',
						actiondesc: '',
						guarantorcode: this.model.cfagit.recgrp.bas.guarantorcode,
						buscode: '',
						bencode: '',
						bename: '',
						benamen: '',
						guperdate: '',
						gupercurr: '',
						guperamount: '',
						pguperamount: '',
						remark: '',
						acp: 'X',
						sta: ''
					});
				}
			} else {
				//删除多的标签
				this.model.cfagit.recgrp.vrf.splice(num + count, cha - count)
			}
		},
		basflgChange(val) {
			if (val == 'X') {
				this.model.cfagit.recgrp.bas.acp = 'X'
					//判断操作类型
			let old_actiontype = ''
			let actiontype = this.model.cfagit.recgrp.bas.actiontype
			let bassta = this.model.cfagit.recgrp.bas.sta
			if (!val || bassta == 'E') {
				old_actiontype = this.model.action
			} else {
				if (actiontype == 'C' || actiontype == 'D' || bassta == 'S') {
					old_actiontype = "C"
					if (bassta == 'S') {
						old_actiontype = 'P'
					}
				} else {
					if (bassta == 'D' || bassta == 'G') {
						old_actiontype = this.model.action
					} else {
						old_actiontype = 'A'
					}
				}
			}
			if (old_actiontype == 'P') {
				this.model.dis = false
				this.model.cfagit.recgrp.bas.actiontype = 'C'
			} else {
				this.model.dis = true
				this.model.cfagit.recgrp.bas.actiontype = old_actiontype
			}
			} else {
				this.model.cfagit.recgrp.bas.acp = ''
				this.model.cfagit.recgrp.bas.actiontype = this.model.action
			}
		
		},
		acpChange(val,curActive){
			if(val == 'X'){
				if(this.model.cfagit.recgrp.dcl[curActive].actiontype == 'D'){
					this.model.cfagit.recgrp.dcl[curActive].actiondesc = ''
				}
				this.model.cfagit.recgrp.dcl[curActive].actiontype = 'C'
			}else {
				this.model.cfagit.recgrp.dcl[curActive].actiontype = this.model.dclAction[curActive].actiontype
				if(this.model.cfagit.recgrp.dcl[curActive].actiontype == 'D') {
					this.model.cfagit.recgrp.dcl[curActive].actiondesc = this.model.dclAction[curActive].actiontdesc
				}
			}
		},
		basflgDel(val) {
			let dclArr = this.model.cfagit.recgrp.dcl || []
			let vrfArr = this.model.cfagit.recgrp.vrf || []
			let dcldelflg = ''
			let vrfdelflg = ''
			for (let i = 0; i < dclArr.length; i++) {
				if ((dclArr[i].sta == 'S' && dclArr[i].actiontype == 'A') ||
					(dclArr[i].sta == 'S' && dclArr[i].actiontype == 'C') ||
					(dclArr[i].sta == 'W' && dclArr[i].actiontype == 'C') ||
					(dclArr[i].sta == 'W' && dclArr[i].actiontype == 'D') ||
					dclArr[i].sta == 'E'
				) {
					dcldelflg = 'X'
					break
				}
			}
			for (let j = 0; j < vrfArr.length; j++) {
				if ((vrfArr[j].sta == 'S' && vrfArr[j].actiontype == 'A') ||
					(vrfArr[j].sta == 'S' && vrfArr[j].actiontype == 'C') ||
					(vrfArr[j].sta == 'W' && vrfArr[j].actiontype == 'C') ||
					(vrfArr[j].sta == 'W' && vrfArr[j].actiontype == 'D') ||
					vrfArr[j].sta == 'E'
				) {
					vrfdelflg = 'X'
					break
				}
			}
			if (val == 'X') {
				if (this.model.cfagit.recgrp.bas.sta == 'E') {
					this.$message.error('此签约信息存在报送错误,未修改前不能删除');
					this.model.cfagit.basflg = ''
					return
				}
				if (vrfdelflg || dcldelflg) {
					this.$message.error('此签约信息存在已报送或待修改的变动信息或履约信息,不能删除!');
					this.model.cfagit.basflg = ''
					return
				} else {
					this.$confirm('删除签约信息,会同时删除未报送的变动信息和履约信息,确定要删除吗?', '提示', {
						confirmButtonText: '是',
						cancelButtonText: '否',
						type: 'warning'
					}).then(() => {
						this.model.cfagit.recgrp.bas.acp = 'X'
						this.model.cfagit.recgrp.bas.actiontype ='D'
						if (dclArr.length > 0) {
							this.model.cfagit.dclflg = 'X'
							for (let z = 0; z < dclArr.length; z++) {
								if (dclArr[z].sta == 'W') {
									dclArr[z].acp = 'X'
								}
							}
						}
						if (vrfArr.length > 0) {
							this.model.cfagit.vrfflg = 'X'
							for (let k = 0; k < vrfArr.length; k++) {
								if (vrfArr[k].sta == 'W') {
									vrfArr[k].acp = 'X'
								}
							}
						}
						this.del(val)
					}).catch(() => {
						this.model.cfagit.basflg = ''
					});
				}


			} else {
				this.model.cfagit.dclflg = ''
				this.model.cfagit.vrfflg = ''
				this.model.cfagit.recgrp.bas.acp = ''
				this.model.cfagit.recgrp.bas.actiontype = this.model.action
			}
			this.$nextTick(() => {
				this.root.$refs['modelForm'].validateField(['cfagit.recgrp.bas.actiondesc']);
			})
		},
		del(val){
			let old_actiontype = ''
			let actiontype = this.model.cfagit.recgrp.bas.actiontype
			let bassta = this.model.cfagit.recgrp.bas.sta
			if(!val){
				old_actiontype = this.model.action
			}else{
				if(actiontype == 'C' || actiontype == 'D' || bassta == 'S'){
					old_actiontype = 'D'
				}else{
					if(bassta == 'D' || bassta == 'G'){
						old_actiontype = this.model.action
					}else{
						old_actiontype = 'A'
					}
				}
			}
				this.model.cfagit.recgrp.bas.actiontype = old_actiontype
		},
		async init(row){
				let rtnmsg = await Api.post("/report/cfabopsup/cfainf",row)
			if (rtnmsg.respCode == SUCCESS){
				this.model.cfagit = rtnmsg.data
				if(row.transName == 'EBAADD'){
					this.model.cfagit.addp = 'X'
					this.model.olddcl = rtnmsg.data.recgrp.dcl.length
					this.model.oldvrf = rtnmsg.data.recgrp.vrf.length
				}
				if(row.transName == 'EBADEL'){
					this.model.action = JSON.parse(JSON.stringify(this.model.cfagit.recgrp.bas.actiontype))
				}
				if(row.transName == 'EBAAME'){
					this.model.action = JSON.parse(JSON.stringify(this.model.cfagit.recgrp.bas.actiontype))
					this.model.dclAction = JSON.parse(JSON.stringify(this.model.cfagit.recgrp.dcl))
				}
			}
				
		},
		//退出
		exit(){
			this.$confirm("确认退出?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$store.commit('delTagsArry', this.$route.path)
        this.$router.back()
      })
		},
	}
}