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

export default {
  methods: {
    
    formSubmit() {
      this.column = ''
      this.order  = ''
      this.handleSearch();
    },	
    
       //双击表格数据
       async TableDblRow(row) {
        this.toDetail(row)
    },
      async handleSearch(orderObj) {
        if (this.model.infcon.opndatfrom == null || this.model.infcon.opndatfrom == '') {
          this.$notify.error({ title: '错误', message: '开始日期必输!' });
          return;
          }
          if (this.model.infcon.opndatto == null || this.model.infcon.opndatto == '') {
            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 (new Date(this.model.infcon.opndatto).getTime() - new Date(this.model.infcon.opndatfrom).getTime() > 731 * 24 * 60 * 60 * 1000) {
          this.$notify.error({ title: '错误', message: '请将查询条件中时间范围控制在2年内(含)。' });
          return
        }
        if(this.model.infcon.seacur == '' && (this.model.infcon.seaamtfr != '' || this.model.infcon.seaamtto != '')){
          this.$notify.error({ title: '错误', message: '输入金额前请先选择币种!' });
          return
        }
        if(this.model.infcon.seaamtfr != '' && this.model.infcon.seaamtto != ''){
          if(Number(this.model.infcon.seaamtfr) > Number(this.model.infcon.seaamtto) ){
            this.$notify.error({ title: '错误', message: '金额下限应小于等于金额上限!' });
            return
          }
        }
        this.load = true;
        if (orderObj) {
          if(orderObj.order){
            this.column = orderObj.column
          }else{
            this.column = ''
          }
          this.order = orderObj.order === 'descending' ? 'desc' : 'asc'
        }
        let params = {
          ...this.model.infcon,
          column:this.column,
          order:this.order,
          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('/Domlc/infbfd/queryInfbfd', 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 = {}
        this.model.infcon.opndatfrom = new Date()
        this.model.infcon.opndatto = new Date()
      },
      // 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 toDetail(row) {
        this.routerPush({
          path: "/business/bfdinf",
          query: { inr: row.inr,objtyp:'BFD',pntinr:row.pntinr,subobjtyp:'BPD'}
        });
      },
      async exportExcel(){
        // const loading = this.loading();
        let params = {
          ...this.model.infcon,
          branch : JSON.parse(sessionStorage.getItem('currentOrg')).departmentNumber,
          pageNumber: this.pagination.pageIndex,
          pageSize:this.pagination.pageSize,
        };
        delete params.markSet
        delete params.modifySet
        let res = await Api.post("/Domlc/infbfd/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.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++){
            if(this.columns[n].prop === 'stagod'){
              array.push(this.getCodelabel(res.data[m][this.columns[n].prop],'stagod'))
            }else{
              array.push(res.data[m][this.columns[n].prop])
            }
          }
          obj.push(array)
          
        }
        Utils.exportToExcel (obj, "国内证卖方单据登记薄_"+name+".xlsx", "国内证卖方单据登记薄") ;
         }
        // loading.close();
        },
        async getdbCode(codeType, uil, codeNam) {
          let params = {
            codeType: codeType,
            uil: uil ? uil : 'EN'
          }
          let rtnmsg = await Api.post("/manager/dic/listDicInfo", params)
          if (rtnmsg.respCode === SUCCESS) {
            let curList = rtnmsg.data.map(item => ({
              value: item.codeValue,
              label: item.codeName
            }));
            this.model.dbCodes[codeNam] = curList
          }
        },
        getCodelabel(value,codenam) {
          const codeobj = this.model.dbCodes[codenam].find(obj => obj.value === value)
          return codeobj ? codeobj.label : value;
        },
        sortChange(obj) {
          let orderObj = {
              order: obj.order,
              column: obj.prop
          }
          this.handleSearch(orderObj);
      },
  
      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.stmData.columns = list;
        this.$nextTick(() => {
          this.$refs.configTable.doLayout()
        })
        this.showDig = false;
      },
      buttonClick(key) {
        let orInr = this.handleModel.gidinr || this.handleModel.inr;
        let name =
          key.toUpperCase().substring(0, 1) + key.toLowerCase().substring(1);
        this.routerPush({ name, params: { orInr } });
        console.log(this.$route.params.orInr,4444);
        this.handleVisible=false
      },
  },
};