import store from './store';
import { getOrderDetail, getVisitDetail, queryDetail, getPayDetail } from '../../components/utils/service';
import { mapToArray, replaceStar } from '../../components/utils/index';
const config = {
  order: {
    title: '预约记录',
    service: async ({
      queryId
    }) => {
      const [err, result] = await getOrderDetail({
        orderId: queryId
      });
      return [err, { ...result,
        viewCardNum: replaceStar(result.cardNum, 3, 4)
      }];
    }
  },
  visit: {
    title: '就诊记录',
    service: async ({
      queryId
    }) => {
      const [err, result] = await getVisitDetail({
        visitId: queryId
      });
      return [err, { ...result,
        viewCardNum: replaceStar(result.cardNum, 3, 4)
      }];
    }
  },
  charge: {
    title: '充值记录',
    service: async ({
      queryId
    }) => {
      const [err, result] = await getPayDetail({
        type: 2,
        outTradeNo: queryId
      });
      return [err, { ...result,
        customInfo: mapToArray(result.customInfo),
        viewCardNum: replaceStar(result.cardNum, 3, 4)
      }];
    }
  },
  report: {
    title: '报告详情',
    service: ({
      queryId
    }) => queryDetail({
      recordId: queryId
    })
  }
};
Page(store.register({
  data: {
    type: '',
    // 根据不同的类型展示不同的页面
    queryId: '',
    detail: {},
    // 页面展示的数据
    options: {} // 兼容框架bug取不到 options 的问题

  },

  onLoad(options) {
    const {
      type
    } = options;
    const {
      title
    } = config[type]; // 只需要服务首页上报，注释掉组件内部的调用
    // reportCmPV_YL({ title, query: options });

    my.setNavigationBar({
      title
    });
    this.setData({
      options
    }, () => this.onFetchDetail());
  },

  // 获取数据
  async onFetchDetail() {
    const {
      options
    } = this.data;
    const {
      service
    } = config[options.type];
    const [, result] = await service(options);

    if (result) {
      this.setData({
        type: options.type,
        queryId: options.queryId,
        detail: result
      });
    }
  },

  navigateBack() {
    my.navigateBack();
  },

  onReachBottom() {},

  onShareAppMessage() {
    const {
      type,
      queryId
    } = this.data;
    return {
      title: this.data.pageName || '',
      path: `${this.route}?type=${type}&queryId=${queryId}`
    };
  }

}));