<template>
  <view class="shop">
    <view class="fixtitle">当前积分：{{ userInfo.source || 0 }}</view>
    <view v-if="modules && modules.length" class="base-list">
      <list-item
        v-for="item in modules"
        :key="item.id"
        :item="item"
        :btn="true"
        class="base-list-item"
        @click="handleDetail(item)"
      />
    </view>
  </view>
</template>
<script>
import login from "@/lib/utils/login";
import { getUserInfo } from "@/lib/api/user";
import { getScoreList, exchangeScore } from "@/lib/api/shop";
import listItem from "@/components/list-item/list-item.vue";

export default {
  name: "PageHome",
  components: {
    listItem,
  },
  data() {
    return {
      userInfo: {},
      modules: [],
      tabStyle: "",
      showTask: false,
    };
  },
  onLoad() {
    const uinfoStr = uni.getStorageSync("userinfo");
    if (uinfoStr) {
      this.userInfo = JSON.parse(uinfoStr);
    }
    uni.showLoading({
      title: "加载中",
    });
    this.loadModules().then(() => {
      uni.hideLoading();
    });
  },
  onShow() {
    const uinfoStr = uni.getStorageSync("userinfo");
    if (uinfoStr) {
      this.userInfo = JSON.parse(uinfoStr);
      this.getUserInfo();
    } else {
      this.userInfo = {};
    }
  },
  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.loadModules().catch((e) => console.log(e));
    uni.stopPullDownRefresh();
  },
  // mounted() {},
  methods: {
    async getUserInfo() {
      const res = await getUserInfo();
      this.userInfo = { ...this.userInfo, ...res.data };
      uni.setStorageSync("userinfo", JSON.stringify(this.userInfo));
    },
    async loadModules() {
      try {
        const data = await getScoreList();
        if (data.errno == 10000) {
          this.modules = data.data;
          // 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,
        });
      }
    },

    login() {
      return new Promise((resolve, reject) => {
        login.wxLogin((userInfo) => {
          if (userInfo.session) {
            this.userInfo = userInfo;
            this.getUserInfo().then(resolve);
          } else reject();
        });
      });
    },

    async handleDetail(item) {
      if (!this.userInfo || !this.userInfo.session) await this.login();

      if (item.score > Number(this.userInfo.score)) {
        uni.showToast({
          icon: "none",
          text: "积分不足",
        });
        return;
      }

      await new Promise((resolve, reject) => {
        uni.showModal({
          title: "提示",
          content: "是否兑换当前商品？",
          success: function (res) {
            if (res.confirm) {
              resolve();
            } else if (res.cancel) {
              reject();
            }
          },
        });
      });

      const res = await exchangeScore({
        sku_id: item.id,
      });

      if (res.errno === 10000) {
        uni.showToast({
          icon: "none",
          text: "兑换成功",
        });
      }
    },
  },
};
</script>
<style lang="scss">
@import "./index.scss";
</style>
