import Api from '~/service/Api';
import { getTrnNameByInr } from "~/service/business/common";
import moment from 'moment';
import Utils from "~/utils"; 
export default {
  methods: {
    async handleSearch() {
      let opndatfrom = this.model.infcon.opndatfrom;
      
      if ((!opndatfrom || opndatfrom == '') && !this.$route.query.trn) {
        this.$notify.error({ title: '错误', message: '查询开始日期必输!' });
        return;
      }
      let opndatto = this.model.infcon.opndatto;
      if ((!opndatto || opndatto == '') && !this.$route.query.trn) {
        this.$notify.error({ title: '错误', message: '查询结束日期必输!' });
        return;
      }

      let params = {
        ...this.model.infcon,
        fenlishi: this.model.fenlishi,
        seapurpos: this.model.seapurpos,
        seagtyp: this.model.seagtyp,
        fromflg: this.model.fromflg,
        cmtflg: this.model.cmtflg,
        fingua: this.model.fingua,
        pageIndex: this.pagination.pageIndex,
        pageSize: this.pagination.pageSize,
        opndatfrom:!opndatfrom ? null : moment(opndatfrom).format('YYYY-MM-DD'),
        opndatto:!opndatto ? null :moment(opndatto).format('YYYY-MM-DD'),
      };
      let rtnmsg = await Api.post('/service/inflid/getList', 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: '服务请求失败!' });
      }
    },
    async handleReset() {
      this.model.infcon.seaownref = '';
      this.model.infcon.opndatfrom = new Date();
      this.model.infcon.opndatto = new Date();
      this.isGuarantee = '';
      this.model.infcon.nam = '';
      this.model.infcon.pty.extkey = '';
      this.model.infcon.seapty = '';
      this.model.infcon.searef = '';
      this.model.infcon.pty.nam = '';
      this.model.infcon.searol = '';
      this.model.infcon.usr.extkey = '';
      this.model.infcon.seasta = '';
      this.model.infcon.seacur = '';
      this.model.infcon.seaamtfr = '';
      this.model.infcon.seaamtto = '';
      this.model.seagtyp = '';
      this.model.infcon.relflg = '';
      this.model.infcon.hndtyp = '';
      this.model.infcon.cxmflg = '';
      this.model.seapurpos = '';
      this.model.seahndtyp = '';
      this.model.infcon.segtyp = '';
      this.model.fromflg = '';
      this.model.fenlishi = '';
      this.model.cmtflg = '';
      this.model.fingua = '';
    },
    // 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();
    },
    toLitpop() {
      this.$router.history.push('/business/litpop');
    },
    toLitopn() {
      // 点击开立,清空从待经办进来的时候带的行参数
      localStorage.setItem('row_litopn', null)
      localStorage.setItem('review_litopn',null)
      this.$router.history.push('/business-new/litopn');
    },
    toLetopn() {
      this.$router.history.push('/business-new/letopn');
    },
    // 详情
    async details(row) {
      const params = {
        //根据xx字段 查询详情表的数据
        inr:row.inr,
        userId: window.sessionStorage.userId || 'ZL',
        ownref: row.ownref,
      };
      const res = await Api.post('/service/inflid/getDetailByOwnref', params);
      if (res.respCode === SUCCESS) {
        this.trnData.data = res.data;
      }
    },
    // 关闭详情弹框
    closeDetailsDialog(refId) {
      this.$refs[refId].doClose();
      console.log('close');
    },
    // 处理
    async handler(row) {
      this.initdialog = true;
      this.currentHandleRow = row
      const params = {
        //根据xx字段 查询处理的数据
        seaownref: row.seaownref,
      };
      const res = await Api.post('/service/inflid/dealWithByOwnref', params);
      if (res.respCode === SUCCESS) {
        if (res.data) {
          this.handlerDataList = []
          Object.keys(res.data).map((item) => {
            this.handlerDataList.push({
              label: item,
              value: res.data[item]
            })
          })
        }
      }
    },
    async handleClick (btn) {
      if (btn.value === 'N') {
        return
      }
      if ( btn.label == '到单' ) {
      const inr = this.currentHandleRow.inr
      const res = await Api.post(`/service/litdck/findLitdck?inr=${inr}`);
      this.dckData = res.data;
      if( res.data.length > 1) {
        this.$confirm('该笔信用证有多笔通知到单单据,是否选择其中一条?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.litdckdialog = true
          
        }).catch(() => {
          this.litdckdialog = false     
        });
        return
      }
      }
     let filterRoute = this.btnRouteMap.filter((item) => {
        return item.label === btn.label
      })
      this.$router.history.push({
        path: filterRoute[0].route,
        query: {
          inr: this.currentHandleRow.inr
        }
      });
      this.initdialog = false;
    },
    // 关闭处理弹框
    closeHandlerDialog() {
      this.initdialog = false;
    },
    /**
     * 打开详情页面
     * @param {string} inr 
     */
    display(inr) {
      getTrnNameByInr({ inr }).then((res) => {
        if (res.respCode == SUCCESS) {
          const trnName = res.data.toLowerCase();
          let viewurl = "/#/display/" + trnName + "?trn=" + inr
          const serial = Utils.generateUUID();
          window.open(viewurl, serial, 'height=1500,width=1200,top=100,left=100,toolbar=no,resizable=no,menubar=no,location=no, status=no');
        }
      });
    },
    checkData(row){
      this.$router.history.push({
        path: '/business-new/litdck',
        query: {
          inr: this.currentHandleRow.inr,brdinr:row.inr
        }
      });
      this.litdckdialog = false  
      this.$options.methods.closeHandlerDialog.call(this);
    }
  },

};