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 { serviceDate, datePeriod } = order;
        const type = utils.selectDate(datePeriod);
        const row = items[type];
        if (!row.date) {
          row.week = utils.getWeek(serviceDate);
          row.date = serviceDate;
        }
        row.list.push({ ...order });
      });
      return items;
    },

    /* 处理时间段 */
    formatTimeRange(item) {
      const { orderSources = [] } = item;
      const items = this.filterItems(
        {
          allm: getTimeItem("全天"),
          am: getTimeItem("上午"),
          pm: getTimeItem("下午"),
          mm: getTimeItem("午间"),
          nm: getTimeItem("夜间"),
          mnm: getTimeItem("下半夜"),
        },
        orderSources
      );
      const { length: allmLength } = items.allm.list;
      const { length: amLength } = items.am.list;
      const { length: pmLength } = items.pm.list;
      const { length: mmLength } = items.mm.list;
      const { length: nmLength } = items.nm.list;
      const { length: mnmLength } = items.mnm.list;
      /* 获取上午或者下午的最小价格 */
      items.allm.min = this.getMinCost(items.allm.list);
      items.am.min = this.getMinCost(items.am.list);
      items.pm.min = this.getMinCost(items.pm.list);
      items.mm.min = this.getMinCost(items.mm.list);
      items.nm.min = this.getMinCost(items.nm.list);
      items.mnm.min = this.getMinCost(items.mnm.list);

      if (allmLength <= 0) {
        delete items.allm;
      } else {
        /* 获取上午的剩余挂号数 */
        items.allm.remain = this.countRemain(items.allm.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 (mmLength <= 0) {
        delete items.mm;
      } else {
        /* 获取下午的剩余挂号数 */
        items.mm.remain = this.countRemain(items.mm.list);
      }

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

      if (mnmLength <= 0) {
        delete items.mnm;
      } else {
        /* 获取下午的剩余挂号数 */
        items.mnm.remain = this.countRemain(items.mnm.list);
      }
      /* 同时存在上午和下午 */
      if (
        items.allm &&
        items.am &&
        items.pm &&
        items.mm &&
        items.nm &&
        items.mnm
      ) {
        const { am, pm, mm, nm, mnm, allm } = items;
        /* 如果上午跟下午的最小价格相等，那么就为全天 */

        if (
          ((((am.min === pm.min) === mm.min) === nm.min) === mnm.min) ===
          allm.min
        ) {
          items.all = {
            ...am,
            name: "全天",
            remain:
              items.am.remain +
              items.pm.remain +
              items.mm.remain +
              items.nm.remain +
              items.mnm.remain +
              items.allm.remain,
            list: [
              ...am.list,
              ...pm.list,
              ...mm.list,
              ...nm.list,
              ...mnm.list,
              ...allm.list,
            ],
          };
          delete items.am;
          delete items.pm;
          delete items.mm;
          delete items.nm;
          delete items.mnm;
          delete items.allm;
        }
      }
      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;
    },
  },
});
