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

export default {
  methods: {
    async handleSearch() {
       if ((this.model.infcon.seaownref == '' || this.model.infcon.seaownref == null) && this.model.infcon.opndatfrom == null) {
        this.$notify.error({ title: '错误', message: '开始日期必输!' });
        return;
        }
        if ((this.model.infcon.seaownref == '' || this.model.infcon.seaownref == 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.seacur && (this.model.infcon.seaamtfr > 0 || this.model.infcon.seaamtto > 0)){
          this.$notify.error({ title: '错误', message: '请先选择币种!' });
          return
        }
      this.load = true;
      let params = {
        ...this.model.infcon,
        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('/Derivative/jstsel/listjs', 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) {
      if(refId){
        this.$refs[refId].showPopper = false;
      }
    },
// 处理
    async handler(row) {
      this.handleModel = row;
      this.trnUrl = 'Derivative';
      this.inifrm = 'jstsel';
      this.initdialog = true;
    },

    //双击表格数据
    async TableDblRow(row) {
      if(row.baozfs === '1' || row.baozfs === '2'){
        this.handler(row)
      }
    },

    //点击处理弹窗中按钮
    async handleClick (btn,row) {
     let obj = {
        objtyp:'JSD',
        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;
    },
    // 关闭处理弹框
    closeHandlerDialog() {
      this.initdialog = false;
    },

    /**
     * 打开详情页面
     * @param {string} inr
     */
    display(row) {
      if(this.oldRefId){
        this.$refs[this.oldRefId].showPopper = false;
      }
      // 历史快照
      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'
          }
        });
      }
    },
    getCodelabel(value,codenam) {
      const codeobj = this[codenam].find(obj => obj.value === value)
      return codeobj ? codeobj.label : value;
    },
    toJstopn() {
      this.$router.history.push('/business/jstopn');
    },
    toJstopm() {
      this.$router.history.push('/business/jstopm');
    },
    toJstopt() {
      this.$router.history.push('/business/jstopt');
    },
  async exportExcel(){
      // const loading = this.loading();
      let params = {
        ...this.model.infcon,
        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,
      };
      delete params.markSet
      delete params.modifySet
      let res = await Api.post('/Derivative/jstsel/exportExcel', params);
			if (res.respCode == SUCCESS) {
      let name=moment(new Date()).format('yyyy-MM-DD HH:mm:ss') ;
      let obj = []
      let arr =[]
      for (let i = 0; i< this.stmData.columns.length;i++){
        arr.push(this.stmData.columns[i].label)
      }
      obj.push(arr);
      for(let m = 0; m< res.data.length; m++){
        let array =  []
        for(let n = 0; n < this.stmData.columns.length; n++){
          if(this.stmData.columns[n].prop === 'seasta'){
            array.push(this.getCodelabel(res.data[m][this.stmData.columns[n].prop],'seastas'))
          } else if(this.stmData.columns[n].prop === 'seatyp'){
            array.push(this.getCodelabel(res.data[m][this.stmData.columns[n].prop],'seatyps'))
          }else{
            array.push(res.data[m][this.stmData.columns[n].prop])
          }
        }
        obj.push(array)
        
      }
      Utils.exportToExcel (obj, "结售汇交易_"+name+".xlsx", "结售汇交易") ;
       }
      // loading.close();
    },
     //Info
     toInfo(row,objtyp,subobjtyp){
      this.routerPush({
        path: "/business/" +  this.activeTab.toLowerCase() + "dinf",
        query: { inr: row.inr,objtyp:objtyp,pntinr:row.pntinr,subobjtyp:subobjtyp,seatyp:row.seatyp}
      });
    },
    changeOwnref(){
      if(this.model.infcon.seaownref){
        this.model.infcon.opndatfrom = null
      }
    }, 
     //修改弹窗状态
    changeBtn(isVisible){
     this.initdialog = isVisible;
   },
  },
};