import Api from "~/service/Api"
import Utils from "~/utils"
import moment from 'moment';

export default {
	methods:{
		async handleSearch(orderObj) {

			// 前端检验
			// this.$refs['paramsForm'].validate(async (validStatic) => {
			// 	if (validStatic) {
        if (this.model.opndatfrom == null) {
          this.$notify.error({ title: '错误', message: '查询开始日期必输!' });
          return;
        }
        if (this.model.opndatto == null) {
          this.$notify.error({ title: '错误', message: '查询结束日期必输!' });
          return;
        }
        if(new Date(this.model.opndatfrom).getTime() > new Date(this.model.opndatto).getTime()){
          this.$notify.error({ title: '错误', message: '开始日期应小于结束日期!' });
          return
        } 
      //  if(this.model.seasta != '' && this.model.seaauthorized == ''){
      //   this.$notify.error({ title: '错误', message: '选择状态时请先选择是否已授权!' });
      //   return
      //  }
       if(this.model.searol != '' && (this.model.ptyExtkey == '' && this.model.seapty == '') ){
        this.$notify.error({ title: '错误', message: '选择角色时请先输入当事人编号或者当事人BIC编号!' });
        return
       }
       if(this.model.seacur == '' && this.model.seaamt > 0){
        this.$notify.error({ title: '错误', message: '输入金额前请先选择币种!' });
        return
       }
      //  if((this.model.seaamtfr > 0 && this.model.seaamtto > 0) && (this.model.seaamtfr > this.model.seaamtto)){
      //   this.$notify.error({ title: '错误', message: '起始金额不能大于结束金额!' });
      //   return
      //  }
        const loading = this.loading();
        if (orderObj) {
          if(orderObj.order){
            this.model.column = orderObj.column
          }else{
            this.model.column = ''
          }
          
          this.model.order = orderObj.order === 'descending' ? 'desc' : 'asc'
        }
          this.model.branch = JSON.parse(sessionStorage.getItem('currentOrg')).departmentNumber
          this.model.hndtyps = ['OC','OT','OL']
					let rtnmsg = await Api.post("/business/infgcd/queryInfgcd",
						{
							...this.model,
							pageNum: this.pagination.pageNum,
							pageSize: this.pagination.pageSize
						});
					if (rtnmsg.respCode == SUCCESS) {
						this.load = false;
						this.tableData = rtnmsg.data.list || [];
						this.pagination = {
							pageNum: rtnmsg.data.pageNum || 1,
							pageSize: rtnmsg.data.pageSize || 10,
							total: rtnmsg.data.total
						};
          }
          loading.close();
			// 	} else {
			// 		// 前端校验失败
			// 		this.$notify({
			// 			title: '失败',
			// 			message: '校验失败',
			// 			type: 'error',
			// 		});
			// 	}
			// })




		},
		 handleReset() {
			this.model.seaownref = "";
			this.model.isGuarantee = "";
      this.model.gidopentyps = [];
      this.model.cmtflg = "";
      this.model.fingua ="";
			this.model.nam = "";
			this.model.ptyExtkey = "";
			this.model.seapty = "";
			this.model.searef = "";
			this.model.ptyNam = "";
			this.model.searol = "";
			this.model.usrExtkey = "";
			this.model.seasta = "";
			this.model.seacur = "";
			this.model.seaamt = "";
			this.model.seaamtfr = "";
			this.model.seaamtto = "";
			this.model.seagtyp = "";
			this.model.relflg = "";
      this.model.segtyp = "";
      this.model.seaauthorized = "";
      this.ptyExtkeyChange();
      this.seaptyChange();
    },
    async exportExcel(){
			const loading = this.loading();
			let res = await Api.post("/business/infgcd/exportExcel",{
				...this.model,
				// pageNum: this.pagination.pageNum,
				// pageSize: this.pagination.pageSize
			});
			if (res.respCode == SUCCESS) {
        let  objdata  = res.data
        for(let a = 0; a < objdata.length; a ++ ){
            objdata[a].gcdmaxamt= parseFloat(objdata[a].gcdmaxamt)
            objdata[a].gcdopnamt = parseFloat(objdata[a].gcdopnamt)
        }
      let name=moment(new Date()).format('yyyy-MM-DD HH:mm:ss') ;
      let obj = []
      let arr =[]
      for (let i = 0; i< this.columns.length;i++){
        arr.push(this.columns[i].label)
      }
      obj.push(arr);
      for(let m = 0; m< res.data.length; m++){
        let array =  []
        for(let n = 0; n < this.columns.length; n++){
          array.push(res.data[m][this.columns[n].prop])
        }
        obj.push(array)
        
      }
				Utils.exportToExcel (obj, "保函索赔登记薄_"+name+".xlsx", "保函索赔登记薄") ;
			}
			loading.close();
		  },
    async queryNam(){
    if(this.model.ptyExtkey){
      let rtnmsg = await Api.post('/business/infgcd/queryNam', this.model.ptyExtkey);
      if(rtnmsg.respCode == SUCCESS){
        if(rtnmsg.data){
        this.model.ptyNam = rtnmsg.data.nam
        }
      }
    }else{
      this.model.ptyNam = ""
    }
    },
    columnSetting(cols) {
      this.showDig = false;
      this.digColumns = [];
      this.columnGroup = [];
      this.selectAll = false;
      // 点击自定义按钮
      this.digColumns = cols;
      this.showDig = true;
      this.columnGroup = cols.filter(item => item.isShow).map(it => it.label);
      this.selectAll = cols.length === this.columnGroup.length;
    },
    async handleColumnChange() {
      this.selectAll = this.digColumns.length === this.columnGroup.length;
    },
    setAll(val) {
      this.columnGroup = val ? this.digColumns.map(item => item.label) : [];
    },
    saveColumnEvent() {
      // 保存
      const list = [];
      if(this.columnGroup.length < 1){
        this.$notify.error({ title: '错误', message: '请至少选择1项结果列!' });
        return;
     }
      this.digColumns.forEach(item => {
        if (!this.columnGroup.includes(item.label)) {
          list.push({
            ...item,
            isShow: false
          });
        } else {
          list.push({ ...item, isShow: true });
        }
      });
      this.tableColumns = list;
			this.$nextTick(() => {
				this.$refs.infgidBSTableRef.doLayout()
			})
      this.showDig = false;
    },
    sortChange(obj) {
      let orderObj = {
          order: obj.order,
          column: obj.prop
      }
      this.handleSearch(orderObj);
  },
	}
}