<template>
  <view class="bind">
    <view class="bind-content">
      <view v-if="show_btn == 1" class="btn-list">
        <view @click="_getGift" class="btn">领取玩具</view>
      </view>
      <view v-if="show_btn == 2" class="btn-list">
        <view class="disabled">{{ error_msg }}</view>
      </view>
      <view class="tip">
        <view>领取规则：</view>
        <view style="margin-top: 10rpx">下单添加客服免费领取玩具，随刊赠送</view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      show_btn: 0,
      kailin_uid: null,
      work_userid: null,
    };
  },
  onLoad(param) {
    if (!this.$checkAccess.alterLogin()) {
      return;
    }

    this.kailin_uid = param.kailin_uid;
    this.work_userid = param.work_userid;
    if (param.kailin_uid && param.work_userid) {
      this.$http.request("api/work_bind/user", { work_userid: param.work_userid, kailin_uid: param.kailin_uid }).then((re) => {
        // 成功渲染数据
        console.log("api/work_bind/user", re);
        if (re.code == "success") {
          this.show_btn = 1;
        } else {
          this.show_btn = 2;
          this.error_msg = re.data.error || "登记失败，请联系客服";
        }
      });
    }
  },
  methods: {
    _getGift() {
      this.$http.request("api/orders_receive/receive").then((re) => {
        // 成功渲染数据
        console.log("api/work_bind/get_gift", re);
        if (re.code == "success") {
          console.log("领取成功");
          uni.showModal({
            content: "您已成功领取",
            showCancel: false,
            confirmText: "查看订单",
            success: () => {
              uni.redirectTo({
                url: "/pages/orders/index",
              });
            },
          });
        } else {
          console.log("领取失败");
          this.show_btn = 2;
          this.error_msg = re.msg;
          uni.showModal({
            content: re.msg,
            showCancel: false,
            confirmText: "回到首页",
            success: () => {
              uni.switchTab({
                url: "/pages/index/index",
              });
            },
          });
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.bind {
  width: 100vw;
  height: 100vh;
  position: relative;
  background-image: url("https://baokan-mp.oss-cn-shenzhen.aliyuncs.com/static/wanju_bgimg.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  box-sizing: border-box;
  .bind-content {
    position: absolute;
    bottom: 50rpx;
    .btn-list {
      display: flex;
      flex-direction: column;
      gap: 30rpx;
      align-items: center;

      .btn {
        width: 300rpx;
        height: 80rpx;
        display: flex;
        justify-content: center;
        align-items: center;
        box-sizing: border-box;
        background-color: #f46f2a;
        border-radius: 60rpx;
        color: #fff;
        font-weight: bold;
        font-size: 36rpx;
      }
      .disabled {
        color: #f46f2a;
        font-size: 36rpx;
        font-weight: bold;
      }
    }
    .tip {
      width: 100vw;
      box-sizing: border-box;
      padding: 0 80rpx;
      color: #f46f2a;
      margin-top: 40rpx;
      font-size: 30rpx;
      font-weight: bold;
    }
  }
}
</style>
