Component({
  props: {
    timeList: [],
    onChange: () => null,
    setDayTime: ''
  },
  data: {
    show: false,
    showList: [],
    isFirst: true,
    active: undefined,
    activePage: undefined
  },

  didMount() {
    const timeList = this.props.timeList.testTimeList;

    if (Array.isArray(timeList)) {
      const showList = timeList.slice(0, 2);
      this.setData({
        showList
      });
    }

    if (this.props.timeList.testTimeList && this.props.timeList.testTimeList.length !== 0) {
      this.onDefaultTime();
    }
  },

  methods: {
    onClosePopup() {
      this.setData({
        show: false
      });
    },

    onConfirmTime(e) {
      const {
        item,
        index,
        type
      } = e.target.dataset;

      if (!item.full) {
        // 如果是弹框选择的，需改变显示顺序
        if (type === 'popup') {
          this.onChangeShow(item, index);
        } else {
          this.setData({
            show: false,
            active: index,
            activePage: index
          });
        }

        this.props.onChange(item);
      } else {
        my.showToast({
          content: '当前时间已约满',
          duration: 1000
        });
      }
    },

    onChangeShow(item, index) {
      const timeList = this.props.timeList.testTimeList;
      const {
        setDayTime
      } = this.props;
      const showList = [timeList[index]];
      let activePage = 0; // 如果以天为维度

      if (timeList[index + 1] && setDayTime === '天') {
        showList.push(timeList[index + 1]);
      } // 如果以半天为维度


      if (setDayTime !== '天') {
        if (timeList[index - 1] && item.testTime.slice(0, 11) === timeList[index - 1].testTime.slice(0, 11)) {
          showList.unshift(timeList[index - 1]);
          activePage = 1;
        } else if (timeList[index + 1]) {
          showList.push(timeList[index + 1]);
        }
      }

      this.setData({
        show: false,
        active: index,
        showList,
        isFirst: false,
        activePage
      });
    },

    handleOpenMore() {
      this.setData({
        show: true
      });
    },

    onDefaultTime() {
      const timeList = this.props.timeList.testTimeList;
      const findItem = timeList.find(item => !item.full);
      const findIndex = timeList.findIndex(item => !item.full);
      this.props.onChange(findItem);
      this.onChangeShow(findItem, findIndex);
    }

  }
});