<template>
  <view class="page">
    <view class="item" v-for="item in list" :key="item.id">
      <image
        :src="item.img"
        @click="handleUse(item)"
        class="card"
        mode="widthFix"
      ></image>
      <image
        :src="require('@/assets/detail/card-checked.png')"
        v-if="item.is_use"
        class="checked"
      ></image>
    </view>
  </view>
</template>

<script>
import login from "@/lib/utils/login";
import { getUserInfo } from "@/lib/api/user";
import { getCardStyleList, useCardStyle } from "@/lib/api/vip";

export default {
  name: "VipCard",
  data() {
    return {
      id: 0,
      userInfo: null,
      list: [],
    };
  },

  async onShow() {
    this.userInfo = await login.getUserInfo();
    if (this.userInfo && this.userInfo.session) {
      this.getUserInfo();
      this.getList();
    }
  },
  methods: {
    async getUserInfo() {
      const res = await getUserInfo();
      this.userInfo = { ...this.userInfo, ...res.data };
    },
    async getList() {
      const res = await getCardStyleList();
      this.list = res.data;
    },
    async handleUse(item) {
      if (item.is_use) return;
      const res = await useCardStyle({ card_id: item.id });
      this.list = res.data;
    },
  },
};
</script>

<style lang="scss">
page {
  // background: #000;
  background: url("~@/assets/vip/bg.png") no-repeat center top #000;
  background-size: contain;
}
.page {
  padding: 40rpx 20rpx;
  color: rgb(251, 222, 190);
  box-sizing: border-box;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
}
.item {
  box-sizing: border-box;
  width: 50%;
  padding: 20rpx;
  position: relative;
  .card {
    width: 100%;
  }
  .checked {
    position: absolute;
    right: 20rpx;
    top: 20rpx;
    width: 40rpx;
    height: 40rpx;
  }
}
</style>
