export default {
  data() {
    return {
      dateKey: 0,
      dateLis: [
        { key: 0, value: '近7天' },
        { key: 1, value: '近30天' },
        { key: 2, value: '近三个月' },
        { key: 3, value: '近半年' },
        { key: 4, value: '近一年' }
      ],
      dateDefault: [],
      pickerOptions() {
        let pickerMinDate;
        return {
          onPick(obj) {
            pickerMinDate = obj.minDate;
          },
          disabledDate(time) {
            let start = new Date();
            start = new Date(`${start.getFullYear()}-${start.getMonth() + 1}-${start.getDate()}`);
            let start2 = new Date(pickerMinDate);
            let end = new Date();
            end = new Date(`${end.getFullYear()}-${end.getMonth() + 1}-${end.getDate()} 23:59:59`);
            let end2 = new Date(pickerMinDate);
            if (pickerMinDate) {
              start = start.setFullYear(start.getFullYear() - 2);
              start2 = start2.setDate(start2.getDate() - 59);
              start = start > start2 ? start : start2;
              end2 = end2.setDate(end2.getDate() + 59) + 24 * 60 * 60 * 1000 - 1000; // end2算到晚上23:59:59
              end = end.getTime() > end2 ? end2 : end.getTime();
            } else {
              start = start.setFullYear(start.getFullYear() - 2);
              end = end.getTime();
            }
            return time.getTime() < start || time.getTime() > end;
          }
        };
      }
    };
  },
  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), getDate(end)];
    },
    btnChange(index) {
      let that = this;
      that.dateKey = index;
      that.dateDefault = [];
      let setDate;
      if (index == 0) {
        // 近7天
        setDate = endDate => {
          endDate.setDate(endDate.getDate() - 6);
          return new Date(endDate);
        };
      } else if (index == 1) {
        // 近30天
        setDate = endDate => {
          endDate.setDate(endDate.getDate() - 29);
          return new Date(endDate);
        };
      } else if (index == 2) {
        // 近三个月
        setDate = endDate => {
          endDate.setMonth(endDate.getMonth() - 2);
          endDate.setDate(1);
          return new Date(endDate);
        };
      } else if (index == 3) {
        // 近半年
        setDate = endDate => {
          endDate.setMonth(endDate.getMonth() - 5);
          endDate.setDate(1); // 以1号为开端

          return new Date(endDate);
        };
      } else {
        // 近一年
        setDate = endDate => {
          endDate.setFullYear(endDate.getFullYear() - 1);
          endDate.setMonth(endDate.getMonth() + 1);
          endDate.setDate(1);

          return new Date(endDate);
        };
      }
      const [start, end] = that.getDateRange(setDate);
      that.dateDefault.push(end);
      that.dateDefault.push(start);
      that.getChartList();
    }
  }
};
