<template>
  <div>
    <u-popup :show="show" mode="center" bgColor="transparent">
      <view class="popupLocale-box">
        <view class="item">
          <image style="width: 600upx; height: 600upx" :src="detail.collectionImg"></image>
        </view>
        <view style="
            color: rgb(0, 0, 0);
            margin-top: 10upx;
            margin-bottom: 10upx;
            font-size: 40upx;
          ">
          {{ detail.name }}
        </view>
        <view style="color: #6c6c6c; margin-top: 10upx; margin-bottom: 10upx" v-html="detail.intro">
        </view>
      </view>
      <view class="btn-box">
        <u-button :text="detail.isReceive
            ? '已被领取'
            : detail.saleType === 'homebuy'
              ? '¥' + detail.price + '购买'
              : $t('to.accept')
          " @click="recive" style="
            width: 460rpx;
            height: 56rpx;
            border-radius: 40rpx;
            margin-top: 30rpx;
            background: #699a70;
            border: none;
            color: #fff;
          "></u-button>
        <view style="margin-top: 30rpx; text-align: center" @click="close">{{
          $t("buyVerfy.cancel")
        }}</view>
      </view>
    </u-popup>
  </div>
</template>
<script>
// const jweixin = require("jweixin-module");
// import jweixin from "weixin-js-sdk";

export default {
  data() {
    return {
      show: false,
    };
  },
  watch: {
    isShow: {
      immediate: true,
      handler(newValue) {
        this.show = newValue;
      },
    },
  },
  props: ["detail", "collectionsUID", "num", "isShow"],
  methods: {
    close() {
      this.show = false;
    },

    async recive() {
      if (this.detail.isReceive) return;
      if (this.detail.saleType === 'homebuy') {
        this.payHandle()
      } else {
        await this.$api.series
          .collectionsReceive({
            collectionsUID: this.collectionsUID,
            num: this.num,
          })
          .then((res) => {
            console.log(res);
            if (res.status === 0) {
              this.show = false;
            } else {
              this.$common.msgToast(res.msg, null, "error");
            }
          })
          .catch(() => {
            uni.showToast({
              title: this.$t("load.failed"),
              icon: "none",
            });
          });
      }


    },
    async payHandle() {
      await this.$api.series
        .collectionsBuy({
          collectionsUID: this.collectionsUID,
          num: this.num,
          openId: uni.getStorageSync("openId"),
        })
        .then((res) => {
          if (res.status === 0) {
            this.vxPay(res.data);
          }
        })
        .catch(() => {
          uni.showToast({
            title: this.$t("load.failed"),
            icon: "none",
          });
        });
    },
    vxPay(data) {
      jweixin.config({
        debug: false,
        appId: "wx72ffc6670d5ddb12",
        timestamp: 1682402430,
        nonceStr: "123",
        signature: "123",
        jsApiList: ["chooseWXPay"],
      });
      jweixin.chooseWXPay({
        prepay_id: data.prepay_id,
        appId: data.appId,
        timestamp: data.timeStamp,
        nonceStr: data.nonceStr,
        package: data.package,
        signType: data.signType,
        paySign: data.paySign,
        success: () => {
          this.$common.msgToast("支付成功", null, "success");
          this.close();
        },
        fail: (res) => {
          this.$common.msgToast(res.errMsg, null, "error");
        },
      });
      jweixin.error(function (res) {
        this.$common.msgToast("用户取消支付", null, "error");
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.popupLocale-box {
  background: rgba(255, 255, 255, 0.8);
  width: 582rpx;
  overflow: hidden;
  padding: 30rpx 42rpx;

  .u-button {
    width: 260rpx;
    height: 56rpx;
    border-radius: 40rpx;
  }

  .item {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .btn-box {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
}
</style>