/*
 * @Descripttion: HJ
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: HJ
 * @Date: 2021-04-05 18:50:35
 * @LastEditors: HJ
 * @LastEditTime: 2021-04-20 17:45:53
 */
const { fhGetEvaluation } = require("../../../api/store/index");
const { Loading, hLoading, Tips } = getApp();
const componentOptions = {
  // 组件选项
  options: {
    multipleSlots: true,
  },
  properties: {
    // 高度
    height: {
      type: [String, Number],
      value: 0,
    },
    // 门店的信息
    storeInfo: {
      type: Object,
      value: {},
    },
  },
  // 组件数据
  data: {
    isPageHidden: false, // 页面是否处于隐藏状态
    // 查询条件
    query: {
      pageSize: 10,
      pageNumber: 1,
      scoreOption: "0",
    },
    // 加载
    evaluationsLoading: false,
    // 所有的评论
    evaluations: [],
    // 是否是进入预览图片
    isPreview: false,
    // 筛选条件
    labellist: [
      {
        name: "全部",
        id: "0",
      },
      {
        name: "好评",
        id: "1",
      },
      {
        name: "中评",
        id: "2",
      },
      {
        name: "差评",
        id: "3",
      },
      {
        name: "有图",
        id: "4",
      },
    ],
  },
  // 组件方法
  methods: {
    init() {
      this.fhGetEvaluation();
    },
    // 获取评论
    async fhGetEvaluation() {
      Loading();
      this.setData({
        evaluationsLoading: true
      })
      try {
        const { evaluations, query } = this.data;
        const { results } = await fhGetEvaluation(query);
        const { records, pages = 0 } = results || {};
        this.page = pages;
        let tempEvaluation = records.map((i) => {
          const { images } = i;
          i.images = typeof images === "string" ? images.split(",") : [];
          return i;
        });
        if (Array.isArray(records)) {
          this.setData({
            evaluations: evaluations.concat(tempEvaluation),
          });
        }
      } catch (e) {
  console.error("debug axios", e);
      }
      hLoading();
      this.setData({
        evaluationsLoading: false
      })
    },
    // 触底
    scrollTolower() {
      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()
        );
    },
  },
  // 组件生命周期
  lifetimes: {
    attached() {
      this.init();
    },
  },
  // 页面生命周期
  pageLifetimes: {
    // 页面被展示
    show() {
      const { isPageHidden } = this.data;

      // show事件发生前，页面不是处于隐藏状态时
      if (!isPageHidden) {
        return;
      }

      // 重新执行定时器等操作
    },
    // 页面被隐藏
    hide() {
      this.setData({
        isPageHidden: true,
      });

      // 清除定时器等操作
    },
  },
};

Component(componentOptions);
