<template>
  <view class="container">
    <!-- 搜索框 -->
    <view class="search">
      <search tips="搜索商品" @event="$navTo('pages/search/index')" />
    </view>

    <!-- 分类+商品 -->
    <commodity ref="mescrollItem" :list="list" :query="query" />

  </view>
</template>

<script>
  import store from '@/store'
  import MescrollCompMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mixins/mescroll-comp'
  import { setCartTabBadge } from '@/core/app'
  import * as CategoryApi from '@/api/category'
  import Search from '@/components/search'
  import Commodity from './components/commodity'

  // 最后一次刷新时间
  let lastRefreshTime;

  export default {
    components: {
      Search,
      Commodity
    },
    mixins: [MescrollCompMixin],
    data() {
      return {
        list: [],
        isLoading: true,
        query: { categoryId1: undefined, categoryId2: undefined }
      }
    },
    onLoad() {
      this.onRefreshPage()
    },
    onShow() {

      // 监听query参数
      store.dispatch('OnceQueryParam')
        .then(res => {
          if (res !== null) {
            this.query = res
          }
          // console.log('onShow',  this.query, res )
        })

      // 每间隔5分钟自动刷新一次页面数据 testzc
     /*  const curTime = new Date().getTime()
      if ((curTime - lastRefreshTime) > 5 * 60 * 1000) {
        this.onRefreshPage()
      } */
    },

    methods: {
      // 刷新页面
      onRefreshPage() {
        // 记录刷新时间
        lastRefreshTime = new Date().getTime()
        // 获取页面数据
        this.getPageData()
        // 更新购物车角标
        setCartTabBadge()
      },

      // 获取页面数据
      getPageData() {
        const app = this
        app.isLoading = true
        Promise.all([
            // 获取分类列表
            // CategoryApi.list(), // testorigin
            CategoryApi.apiGetAppCategoryList()

          ])
          .then(result => {
            // 初始化分类模板设置 testorigin
            // app.initSetting(result[0])
            // 初始化分类列表数据
            app.initCategory(result[0])
          })
          .finally(() => app.isLoading = false)
      },

      /**
       * 初始化分类列表数据
       * @param {Object} result
       */
      initCategory(result) {
        this.list = result.data
      },


    },

    /**
     * 设置分享内容
     */
    onShareAppMessage() {
      const app = this
      return {
        title: _this.templet.shareTitle,
        path: '/pages/category/index?' + app.$getShareUrlParams()
      }
    },

    /**
     * 分享到朋友圈
     * 本接口为 Beta 版本，暂只在 Android 平台支持，详见分享到朋友圈 (Beta)
     * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
     */
    onShareTimeline() {
      const app = this
      return {
        title: _this.templet.shareTitle,
        path: '/pages/category/index?' + app.$getShareUrlParams()
      }
    }

  }
</script>

<style>
  page {
    background: #fff;
  }
</style>
<style lang="scss" scoped>
  // 搜索框
  .search {
    position: fixed;
    top: var(--window-top);
    left: var(--window-left);
    right: var(--window-right);
    z-index: 9;
  }
</style>