import { getDepOrderSourceRemain, getDoctors } from '../../utils/service';
import { history } from '../../utils';
Component({
  props: {
    data: {
      bookInfo: {}
    }
  },
  data: {
    timeRange: [],
    doctorList: [],
    date: undefined,
    currentIndex: 0
  },

  didMount() {
    my.setNavigationBar({
      title: '选择医生'
    });
    this.getSourceDate();
  },

  methods: {
    saveRef(ref) {
      this.scrollFetch = ref;
    },

    async getCurSelectTime({
      id,
      currentIndex
    }) {
      if (!id) {
        return;
      }

      await this.setData({
        date: id,
        currentIndex
      });
      this.scrollFetch && this.scrollFetch.fetchList({
        pageIndex: 1,
        date: id
      }, true, true);
    },

    toDoctorInfo(e) {
      const {
        doctor
      } = e.target.dataset;
      const {
        timeRange,
        currentIndex
      } = this.data;
      const {
        bookInfo
      } = this.props.data;
      history.openPage({
        pageType: 'select-number-source',
        data: {
          timeRange,
          bookInfo: { ...doctor,
            ...bookInfo,
            currentIndex
          }
        }
      });
    },

    // 查询号源日历
    async getSourceDate() {
      const [, res] = await getDepOrderSourceRemain({
        depId: this.props.data.bookInfo.depId
      });
      this.setData({
        timeRange: res || []
      });
    },

    // 获取医生列表
    getDoctors(params) {
      const {
        date
      } = this.data;
      const {
        depId
      } = this.props.data.bookInfo;

      if (!date) {
        return [new Error('没有日期')];
      }

      return getDoctors({ ...params,
        depId,
        date
      });
    }

  }
});