import { exportCardLog } from '@/service/api/cardApi.js';
import { formatDateTimeByType } from '@/utils/index.js';
export default {
  data() {
    return {
      loading: false,
      dateTime: [],
      defaultAvatar: require('../../assets/img/head_default.png'),
      formatDateTimeByType,
      tableData: [],
      sortColumn: '',
      sortType: '',
      total: 0,
      checkedM: false,
      checkedT: false
    };
  },
  methods: {
    getDateRange(setEndDate) {
      const start = new Date();
      let end = new Date();
      end = setEndDate(end);
      function getDate(date) {
        const result = [];
        result.push(date.getFullYear());
        result.push((date.getMonth() + 1).toString().padStart(2, '0'));
        result.push(
          date
            .getDate()
            .toString()
            .padStart(2, '0')
        );
        return result.join('-');
      }
      // yyyy-MM-dd
      return [`${getDate(start)} 23:59:59`, `${getDate(end)} 00:00:00`];
    },
    // 当月
    currentM(e) {
      if (e) {
        this.checkedT = false;
        this.dateTime = [];
        let setDate;
        setDate = endDate => {
          endDate.setMonth(endDate.getMonth());
          endDate.setDate(1);
          return new Date(endDate);
        };
        const [start, end] = this.getDateRange(setDate);
        this.dateTime.push(end);
        this.dateTime.push(start);
      } else {
        this.dateTime = [];
      }
      this.refresh();
    },
    // 近三个月
    reThree(e) {
      if (e) {
        this.checkedM = false;
        this.dateTime = [];
        let setDate;
        setDate = endDate => {
          endDate.setMonth(endDate.getMonth() - 2);
          endDate.setDate(1);
          return new Date(endDate);
        };
        const [start, end] = this.getDateRange(setDate);
        this.dateTime.push(end);
        this.dateTime.push(start);
      } else {
        this.dateTime = [];
      }
      this.refresh();
    },
    refresh() {
      if (!this.dateTime) {
        this.checkedM = false;
        this.checkedT = false;
      }
      this.params.currentPage = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.params.currentPage = 1;
      this.params.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.params.currentPage = val;
      this.getList();
    },
    // 排序
    sortChange(column) {
      this.sortColumn = column.prop;
      this.sortType = column.order == 'ascending' ? 'asc' : 'desc';
      this.params.sortBy = `${this.sortColumn} ${this.sortType}`;
      this.getList();
    },
    dataTimeFormat() {
      if (this.dateTime) {
        this.params.writeOffBeginTime = formatDateTimeByType(this.dateTime[0], 'yyyy-MM-dd HH-mm-ss');
        this.params.writeOffEndTime = formatDateTimeByType(this.dateTime[1], 'yyyy-MM-dd HH-mm-ss');
      } else {
        this.params.writeOffBeginTime = '';
        this.params.writeOffEndTime = '';
      }
    },
    exportResult() {
      const paras = {
        search: this.params.search,
        writeOffBeginTime: this.params.writeOffBeginTime,
        writeOffEndTime: this.params.writeOffEndTime,
        sortBy: this.params.sortBy
      };
      exportCardLog(paras).then(res => {
        if (res.result.errorCode == '0') {
          let blob = new Blob([res.data], {
            type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
          });
          // 3.创建一个临时的url指向blob对象
          let objectUrl = window.URL.createObjectURL(blob);
          // 4.创建url之后可以模拟对此文件对象的一系列操作，例如：预览、下载
          let a = document.createElement('a');
          a.setAttribute('href', objectUrl);
          a.setAttribute('download', `卡券核销数据-${new Date().toLocaleDateString()}`);
          a.click();
          // 5.释放这个临时的对象url
          window.URL.revokeObjectURL(objectUrl);
        }
      });
    }
  }
};
