import { formatDateTimeByType } from '@/utils/index.js';
export default {
  data() {
    return {
      loading: false,
      dateTime: [Date.now() - 30 * 24 * 60 * 60 * 1000, Date.now()],
      defaultAvatar: require('../../assets/img/head_default.png'),
      formatDateTimeByType,
      tableData: [],
      sortColumn: '',
      sortType: '',
      total: 0,
      checkedM: false,
      checkedT: false,
      pickerOptions() {
        // let pickerMinDate;
        return {
          onPick(obj) {
            // pickerMinDate = obj.minDate;
          },
          disabledDate(time) {
            let start = new Date();
            start.setFullYear(start.getFullYear() - 1);
            start.setDate(start.getDate() + 1);
            start = new Date(`${start.getFullYear()}-${start.getMonth() + 1}-${start.getDate()}`);
            let end = new Date();
            end = new Date(`${end.getFullYear()}-${end.getMonth() + 1}-${end.getDate()} 23:59:59`);
            return time.getTime() < start.getTime() || time.getTime() > end;
          }
        };
      }
    };
  },
  mounted() {
    this.dateTime = [`${formatDateTimeByType(this.dateTime[0], 'yyyy-MM-dd')}  00:00:00`, `${formatDateTimeByType(this.dateTime[1], 'yyyy-MM-dd')}  23:59:59`];
  },
  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(e) {
      if (e || !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 = '';
      }
    }
  }
};
