/*
 * @Descripttion: HJ
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: HJ
 * @Date: 2021-06-24 15:46:32
 * @LastEditors: HJ
 * @LastEditTime: 2021-07-01 10:19:35
 */
import { fhGetStoreInfo, fhGetEvaluation } from "../../../api/store/index";
import { splitStrToArray } from "../../../utils/util";
const { Tips } = getApp();
const pageOptions = {
  // 页面数据
  data: {
    isFirstOnShow: true, // 是否为首次执行onShow
    // 门店信息
    storeInfo: {},
    // 环境图片列表
    environmentImages: [],
    // 评论查询条件
    query: {
      pageSize: 10,
      pageNumber: 1,
      scoreOption: "0",
    },
    // 加载
    evaluationsLoading: false,
    // 所有的评论
    evaluations: [],
    // 是否是进入预览图片
    isPreview: false,
    // 评论菜单
    commentsTabs: {
      0: {
        title: "全部",
        value: [],
        field: "0",
      },
      1: {
        title: "好评",
        value: [],
        field: "1",
      },
      3: {
        title: "差评",
        value: [],
        field: "3",
      },
      4: {
        title: "有图",
        value: [],
        field: "4",
      },
    },
  },
  // 页面载入时
  onLoad(e) {
    this.init(e);
  },
  // 页面初始化
  init(e) {
    this.getStoreInfo();
    this.fhGetEvaluation();
  },
  // 页面准备好时
  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() {
    try {
      const { results } = await fhGetStoreInfo();
      let { environmentImages } = results;
      environmentImages = splitStrToArray(environmentImages);
      this.setData({
        storeInfo: results,
        environmentImages,
      });
    } catch (e) {
      console.log("debug axios", e);
    }
  },
  // 切换标签
  tabChange({ detail: { name } }) {
    this.setData(
      {
        "query.scoreOption": name,
        [`commentsTabs.${name}.value`]: [],
      },
      () => {
        this.fhGetEvaluation();
      }
    );
  },
  // 获取评论
  async fhGetEvaluation() {
    this.setData({
      evaluationsLoading: true,
    });
    try {
      const { commentsTabs, query } = this.data;
      const { scoreOption } = query;
      const { results } = await fhGetEvaluation(query);
      const { records, pages = 0 } = results || {};
      this.page = pages;

      let evaluations = commentsTabs[scoreOption].value;
      const tempEvaluation = records.map((i) => {
        const { images } = i;
        i.images = splitStrToArray(images);
        return i;
      });
      if (Array.isArray(records)) {
        this.setData({
          [`commentsTabs.${scoreOption}.value`]:
            evaluations.concat(tempEvaluation),
        });
      }
    } catch (e) {
      console.error("debug axios", e);
    }
    this.setData({
      evaluationsLoading: false,
    });
  },
  // 触底
  onReachBottom() {
    const { query } = this.data;
    let { pageNumber } = query;

    if (++pageNumber > this.page) {
      Tips("没有更多评论了");
      return;
    }

    this.setData(
      {
        query: {
          ...query,
          pageNumber,
        },
      },
      () => this.fhGetEvaluation()
    );
  },
  // 预览图片
  previewImage({ currentTarget: { dataset } }) {
    if (dataset) {
      this.setData(
        {
          isPreview: true,
        },
        () => {
          wx.previewImage(dataset);
        }
      );
    }
  },
  // 筛选
  queryEvaluation({ currentTarget: { dataset } }) {
    const { scoreOption } = dataset;
    if (scoreOption)
      this.setData(
        {
          "query.scoreOption": scoreOption,
          "query.pageNumber": 1,
          evaluations: [],
        },
        () => this.fhGetEvaluation()
      );
  },
  // 跳转
  nav({ currentTarget: { dataset } }) {
    const { path } = dataset;
    wx.$routerTo(path);
  },
  // 打电话
  call({ currentTarget: { dataset } }) {
    const { phone } = dataset;
    wx.makePhoneCall({
      phoneNumber: phone,
    });
  },
};

Page(pageOptions);
