/*
 * @Descripttion: HJ
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: HJ
 * @Date: 2021-06-25 08:54:20
 * @LastEditors: HJ
 * @LastEditTime: 2021-06-25 09:32:42
 */
import { fhGetStoreInfo } from "../../../api/store/index";
const pageOptions = {
  // 页面数据
  data: {
    isFirstOnShow: true, // 是否为首次执行onShow
    imgs: [
      {
        title: "商家资质信息公示",
        imgs: [
          {
            label: "营业执照",
            field: "businessLicenseImgs",
            img: "",
          },
          {
            label: "食品经营许可证",
            field: "manageLicenseImags",
            img: "",
          },
        ],
      },
    ],
    // imgs: {
    //   商家资质信息公示: [],
    //   食品安全监督公示: [],
    // },
    loading: true,
  },
  // 页面载入时
  onLoad(e) {
    this.init(e);
  },
  // 页面初始化
  init(e) {
    this.getStoreInfo();
  },
  // 页面准备好时
  onReady() {},
  // 页面显示时
  onShow() {
    const { isFirstOnShow } = this.data;

    if (isFirstOnShow) {
      // 首次执行时
      this.setData({
        isFirstOnShow: false,
      });
      return;
    }
  },
  // 页面隐藏时
  onHide() {},
  // 页面卸载时
  onUnload() {},
  // 下拉页面时
  onPullDownRefresh() {},
  // 到达页面底部时
  onReachBottom() {},
  // 页面滚动时
  onPageScroll() {},
  // 分享时，注：onShareAppMessage不能为async异步函数，会导致不能及时取得返回值，使得分享设置无效
  onShareAppMessage() {
    /* const title = ''
    const path = ''
    const imageUrl = ``

    return {
      title,
      path,
      imageUrl,
    } */
  },
  // 自定义事件
  // 获取门店信息
  async getStoreInfo() {
    this.setData({
      loading: true,
    });
    try {
      const { results } = await fhGetStoreInfo();
      let { imgs } = this.data;
      imgs = imgs.map((i) => {
        const { imgs } = i;
        const tempImgs = [];
        const tempImgUrls = [];
        imgs.forEach((j) => {
          const { field } = j;
          const tempUrl = results[field];
          tempUrl &&
            (tempImgs.push({ ...j, img: tempUrl }), tempImgUrls.push(tempUrl));
        });
        i.imgs = tempImgs;
        i.imgUrls = tempImgUrls;
        return i;
      });
      this.setData(
        {
          imgs,
        },
        () => {
          this.setData({
            loading: false,
          });
        }
      );
    } catch (e) {
      console.log("debug axios", e);
      this.setData({
        loading: false,
      });
    }
  },
  // 预览图片
  previewImage({ currentTarget: { dataset } }) {
    if (dataset) {
      this.setData(
        {
          isPreview: true,
        },
        () => {
          wx.previewImage(dataset);
        }
      );
    }
  },
};

Page(pageOptions);
