<template>
  <view class="wrap">
    <view class="tab flex-row" v-if="tabs.length">
      <view
        class="tab"
        v-for="(tab, idx) in tabs"
        :key="tab.val"
        :class="tabidx === idx ? 'cur' : ''"
        @click="handleTabChange(idx)"
      >
        <text>{{ tab.name }}</text>
      </view>
    </view>
    <view v-if="list && list.length" class="list">
      <navigator
        class="item"
        v-for="item in list"
        :key="item.id"
        :url="`/pages/shop/detail?id=${item.id}`"
      >
        <image class="item-img" :src="item.banner" mode="aspectFit"></image>
        <view class="item-body">
          <view class="item-name">{{ item.name }}</view>
          <view class="item-desc" v-if="item.address">{{ item.address }}</view>
        </view>
      </navigator>
    </view>
  </view>
</template>
<script>
import login from "@/lib/utils/login";
import { getShopList } from "@/lib/api/shop";

export default {
  name: "PageHome",
  data() {
    return {
      statusBarHeight: 0, // 自定义导航
      titleBarHeight: 44, // 自定义导航
      BarHeight: 0, // 自定义导航
      userInfo: {},
      list: [],
      tabStyle: "",
      tabs: [],
      tabidx: 0,
      // showTask: false,
    };
  },
  async onLoad() {
    this.userInfo = await login.getUserInfo();
    uni.showLoading({
      title: "加载中",
    });
    this.loadList().then(() => {
      uni.hideLoading();
    });
  },
  // onShow() {
  //   // this.getSingInfo();
  // },
  onShareAppMessage() {
    return {
      title: "云果国潮会员中心",
      imageUrl:
        "https://yggc.oss-cn-beijing.aliyuncs.com/images/81d939394aea637ac0f6a3026d1a38e6.jpg",
      path: "/pages/index/index",
    };
  },
  onShareTimeline() {
    return {
      title: "云果国潮会员中心",
      imageUrl:
        "https://yggc.oss-cn-beijing.aliyuncs.com/images/81d939394aea637ac0f6a3026d1a38e6.jpg",
      path: "/pages/index/index",
    };
  },
  async onPullDownRefresh() {
    await this.loadList().catch((e) => console.log(e));
    uni.stopPullDownRefresh();
  },
  methods: {
    handleTabChange(i) {
      this.tabidx = i;
      this.loadList();
    },
    async loadList() {
      const tab = this.tabs[this.tabidx];
      try {
        const data = await getShopList(tab ? tab.val : void 0);
        if (data.errno == 10000) {
          this.list = data.data;
          this.tabs = data.tabs;
          // this.endTime = data.diff_time;
          // this.runCd();
        } else {
          uni.showToast({
            icon: "none",
            title: data.errmsg,
            duration: 3000,
          });
        }
      } catch (e) {
        uni.showToast({
          icon: "none",
          title: "出错啦，请稍后重试",
          duration: 3000,
        });
      }
    },

    // async getSingInfo() {
    //   if (!this.userInfo.session) {
    //     this.showTask = true;
    //     return Promise.reject();
    //   }
    //   const data = await getSingInfo(this.userInfo.session);
    //   this.showTask = !data.is_current_signin;
    // },

    jumpModuleAll(url) {
      if (!url) return;

      if (/^https?:\/\//i.test(url)) {
        uni.navigateTo({
          url: `../webview/webview?url=${encodeURIComponent(url)}`,
        });
        return;
      }
      if (
        [
          "/pages/index/index",
          "/pages/shop/index",
          "/pages/activity/index",
          "/pages/my/my",
        ].includes(url)
      ) {
        uni.switchTab({
          url,
        });
      } else {
        uni.navigateTo({
          url,
        });
      }
    },
  },
};
</script>
<style lang="scss">
@import "./list.scss";
</style>
