import Api from '~/service/Api';
import moment from 'moment';

export default {
  methods: {
    async handleSearch() {
       if ((this.model.infcon.bpownref == '' || this.model.infcon.bpownref == null) && this.model.infcon.opndatfrom == null) {
        this.$notify.error({ title: '错误', message: '开始日期必输!' });
        return;
        }
        if ((this.model.infcon.bpownref == '' || this.model.infcon.bpownref == null) && this.model.infcon.opndatto == null) {
          this.$notify.error({ title: '错误', message: '截止日期必输!' });
          return;
        }
        if (this.model.infcon.opndatfrom != null && this.model.infcon.opndatto != null) {
          if (new Date(this.model.infcon.opndatfrom).getTime() > new Date(this.model.infcon.opndatto).getTime()) {
            this.$notify.error({ title: '错误', message: '开始日期应小于或等于截止日期!' });
            return
          }
        }
        if (this.model.infcon.matdatfrom != null && this.model.infcon.matdatto != null) {
          if (new Date(this.model.infcon.matdatfrom).getTime() > new Date(this.model.infcon.matdatto).getTime()) {
            this.$notify.error({ title: '错误', message: '开始日期应小于或等于截止日期!' });
            return
          }
        }
      this.load = true;
      let params = {
        ...this.model.infcon,
        fintypLst: ['D','P','I'],
        opndatfrom : this.model.infcon.opndatfrom ? moment(this.model.infcon.opndatfrom).format("YYYY-MM-DD") : '',
        opndatto : this.model.infcon.opndatto ? moment(this.model.infcon.opndatto).format("YYYY-MM-DD") : '',
        branch: JSON.parse(sessionStorage.getItem('currentOrg')).departmentNumber,
        pageNumber: this.pagination.pageIndex,
        pageSize: this.pagination.pageSize,
      };
      delete params.markSet
      delete params.modifySet
      let rtnmsg = await Api.post('/Financing/bptsel/listbp', params);
      if (rtnmsg.respCode == SUCCESS) {
        this.stmData.data = [];
        this.stmData.data = rtnmsg.data.list;
        this.pagination.total = rtnmsg.data.total;
      } else {
        this.$notify.error({ title: '错误', message: '服务请求失败!' });
      }
      this.load=false
    },

    async handleReset() {
      this.model.infcon = {}
    },

    // pageSize改变
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.handleSearch();
    },
    // 页码改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pagination.pageIndex = val;
      this.handleSearch();
    },

 // 详情
 async details(row) {
  const params = {
    ownref: row.ownref,
  };
  const res = await Api.post('/manager/trn/getTrnListByOwnref', params);
  if (res.respCode === SUCCESS) {
    if (res.data.length === 1) {
      if(this.oldRefId &&  this.$refs[this.oldRefId]){
        this.$refs[this.oldRefId].showPopper = false;
      }
      this.display(res.data[0])
    }else{
      this.trnData.data = res.data;
      this.oldRefId = 'popover_' + row.inr
      this.$refs[this.oldRefId].showPopper = true;
    }
  }
},
closeDisplayDialog(){
  if(this.oldRefId){
    this.$refs[this.oldRefId].showPopper = false;
  }
},
    // 关闭详情弹框
    closeDetailsDialog(refId) {
      this.$refs[refId].doClose();
      console.log('close');
    },

    // 处理
    async handler(row) {
      this.handleModel = row;
      this.trnUrl = 'Financing';
      this.inifrm = 'bptsel';
      this.initdialog = true;
    },

    //双击表格数据
    async TableDblRow(row) {
      this.handler(row)
    },

    //点击处理弹窗中按钮
    async handleClick(btn, row) {
      let obj = {
        objtyp:'BPD',
        objinr:row.inr,
        pntinr:row.pntinr,
        pnttyp:row.pnttyp,
        ledinr:row.ledinr,
        trnName:btn.code.toLowerCase(),
      }
     let isPush =  await this.$refs.lockAndPending.checkLockAndPending(obj)
      if(!isPush){
        return
      }
      this.routerPush({
        path: "/business/" + btn.code.toLowerCase(),
        query: { inr: row.inr, pntinr: row.pntinr }
      });
      this.initdialog = false;
    },
    //修改弹窗状态
   changeBtn(isVisible){
    this.initdialog = isVisible;
  },
    // 关闭处理弹框
    closeHandlerDialog() {
      this.initdialog = false;
    },

    /**
     * 打开详情页面
     * @param {string} inr
     */
    display(row) {
      if (this.oldRefId) {
        this.$refs[this.oldRefId].doClose();
      }
      // 历史快照
      if (row.inr.length == 8) {
        this.routerPush({
          path: '/business/HistoryRecord',
          query: {
            businessInr: row.inr,
            businessType: 'TRN',
            type: 'view'
          }
        })
      } else if (row.inr.length == 16) {
        this.routerPush({
          path: `/display/${row.inifrm.toLowerCase()}`,
          query: {
            businessInr: row.inr,
            businessType: 'TRN'
          }
        });
      }
    },
    toBptopn() {
      // 点击开立,清空从待经办进来的时候带的行参数
      // localStorage.setItem('row_letopn', null)
			// localStorage.setItem('review_letopn',null)
			this.routerPush({
				path: '/business/Bptopn',
        query: {
          fintyp: "I"
        }
			});
    },
     //Info
     toInfo(row,objtyp,subobjtyp){
      this.routerPush({
        path: "/business/" +  this.activeTab.toLowerCase() + "dinf",
        query: { inr: row.inr,objtyp:objtyp,pntinr:row.pntinr,subobjtyp:subobjtyp}
      });
    },
    changeOwnref(){
      if(this.model.infcon.bpownref){
        this.model.infcon.opndatfrom = null
      }
    },
  },
};