import * as utils from '../../../utils';

const getTimeItem = name => ({
  name,
  min: 0,
  remain: 0,
  list: [],
  date: '',
  week: ''
});

const restDate = date => date.replace(/-/g, '/');

Component({
  props: {
    item: {}
  },
  data: {
    rows: []
  },

  didMount() {
    const {
      item
    } = this.props;
    this.setData({
      rows: this.formatTimeRange(item)
    });
  },

  methods: {
    /* 筛选数据 */
    filterItems(items, list) {
      list.forEach(order => {
        /* 时间段以开始时间为准 */
        let {
          endTime,
          startTime
        } = order;
        endTime = restDate(endTime);
        startTime = restDate(startTime);
        const {
          time: eTime
        } = utils.cuttingDate(endTime);
        const {
          type,
          time: sTime
        } = utils.cuttingDate(startTime);
        const row = items[type];

        if (!row.date) {
          row.week = utils.getWeek(startTime);
          row.date = this.getOrderDate(startTime);
        }

        row.list.push({ ...order,
          sTime,
          eTime
        });
      });
      return items;
    },

    /* 处理时间段 */
    formatTimeRange(item) {
      const {
        orderSources = []
      } = item;
      const items = this.filterItems({
        am: getTimeItem('上午'),
        pm: getTimeItem('下午')
      }, orderSources);
      const {
        length: amLength
      } = items.am.list;
      const {
        length: pmLength
      } = items.pm.list;
      /* 获取上午或者下午的最小价格 */

      items.am.min = this.getMinCost(items.am.list);
      items.pm.min = this.getMinCost(items.pm.list);

      if (amLength <= 0) {
        delete items.am;
      } else {
        /* 获取上午的剩余挂号数 */
        items.am.remain = this.countRemain(items.am.list);
      }

      if (pmLength <= 0) {
        delete items.pm;
      } else {
        /* 获取下午的剩余挂号数 */
        items.pm.remain = this.countRemain(items.pm.list);
      }
      /* 同时存在上午和下午 */


      if (items.am && items.pm) {
        const {
          am,
          pm
        } = items;
        /* 如果上午跟下午的最小价格相等，那么就为全天 */

        if (am.min === pm.min) {
          items.all = { ...am,
            name: '全天',
            remain: items.am.remain + items.pm.remain,
            list: [...am.list, ...pm.list]
          };
          delete items.am;
          delete items.pm;
        }
      }

      return Object.values(items);
    },

    /* 计算总的余号量 */
    countRemain(list) {
      return list.map(({
        remain
      }) => remain).reduce((pre, cur) => pre + cur);
    },

    /* 获取价格的最小值 */
    getMinCost(items) {
      const costs = [...new Set(items)].map(({
        cost
      }) => cost);
      return costs.length > 0 ? Math.min(...costs) : 0;
    },

    /* 获取日期 */
    getOrderDate(time) {
      const date = new Date(time);
      const year = date.getFullYear();
      const month = date.getMonth() + 1;
      const day = date.getDate();
      return [year, this.checkDate(month), this.checkDate(day)].join('-');
    },

    checkDate(v) {
      return `${v}`.length < 2 ? `0${v}` : v;
    },

    /* 根据日期获取时间 */
    getTimeByDate(date) {
      return utils.cuttingDate(date).time;
    }

  }
});