/* eslint-disable */
// 顶部滑动日历
const app = getApp();
Component({
  mixins: [],
  // minxin 方便复用代码
  data: {
    width: 100,
    Dates: [],
    dateListArray: ['日', '一', '二', '三', '四', '五', '六'],
    clientHeight: 0,
    clientWidthCount: 0,
    scrollLeft: 0 // 设置滚动条定位的位置

  },
  // 组件内部数据
  props: {
    initDate: false,
    // 是否需要初始化回调函数
    currentIndex: 0,
    timeRange: [],
    showTip: true,
    onGetTime: dateItem => {},
    calendar: [],
    maxCount: 14 // 横向日期默认显示的日期数量

  },

  didMount() {
    this.init();
  },

  didUpdate(prevProps, prevData) {
    if (this.props.timeRange.length && prevProps.timeRange !== this.props.timeRange) {
      this.init();
    }
  },

  didUnmount() {},

  methods: {
    // 自定义方法
    init() {
      this.setData({
        Dates: []
      });
      const {
        timeRange
      } = this.props;

      for (let i = 0; i < timeRange.length; i++) {
        const dateItem = timeRange[i];
        this.data.Dates.push(this.formatDate(dateItem.date, dateItem.remain, i));
      }

      this.setData({
        calendar: this.data.Dates,
        scrollLeft: Math.floor(this.props.currentIndex / 7) * 350
      }); // initDate

      if (this.props.initDate) {
        const {
          currentIndex
        } = this.props;
        this.props.onGetTime({ ...this.data.Dates[currentIndex],
          currentIndex
        });
      }
    },

    formatDate(dateObj, remain, index) {
      const date = new Date(dateObj);
      const year = date.getFullYear();
      const mon = date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1;
      const day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate();
      const week = this.data.dateListArray[date.getDay()];
      const obj = {
        // index,
        id: `${year}-${mon}-${day}`,
        week,
        mon,
        date: day,
        remain
      };
      return obj;
    },

    select(e) {
      this.setData({
        currentIndex: e.target.dataset.index
      });
      this.props.onGetTime({ ...e.target.dataset.date,
        currentIndex: e.target.dataset.index
      });
    }

  }
});