<template>
  <view class="page">
		<navbar leftClick @leftClick="$utils.navigateBack" />

    <view class="content">
      <image class="avatar" :src="detail.headImage"></image>
      <text class="nick-name">{{ detail.headTitle }}</text>

      <template v-if="isLocked">
        <button class="btn" type="success" @click="onAdd">添加</button>
      </template>
      <template v-else>
        <image class="qr" :src="detail.wxCodeImage" :show-menu-by-longpress="true"></image>
      </template>
    </view>

    <popupUnlock ref="popupUnlock" src="../static/sharing/unlock-user.png"></popupUnlock>

  </view>
</template>

<script>
  import popupUnlock from '../components/popupUnlock.vue'

	export default {
    components: {
      popupUnlock,
    },
		data() {
			return {
        detail: {
          id: null,
          userId: null,
          headImage: null,
          headTitle: null,
          indexImage: null,
          num: 10,
          wxCodeImage: null,
          textDetails: null,
        },
        isLocked: true,
      }
    },
    onLoad(option) {
      const { id } = option
      
      this.fetchDetails(id)
    },
    onShareAppMessage(res) {
      const {
        headTitle,
        indexImage,
      } = this.detail

      let o = {
        title : headTitle,
        imageUrl: indexImage,
        query: `id=${this.id}`,
      }

      // todo: get times and check is unlocked
      this.refreshLockStatus()

      return o
    },
    methods: {
      async fetchDetails(id) {
        try {
          this.detail = await this.$fetch('getShareInfo', { id })
        } catch (err) {
          
        }
      },
      async fetchCheckShare(id) {
        try {
          return await this.$fetch('checkShare', { id }, false)
        } catch (err) {
          return {}
        }
      },
      async refreshLockStatus() {
        const res = await this.fetchCheckShare()
        const { result, message } = res

        if (result) {
          this.isLocked = false
          this.$refs.popupUnlock.close();
          return
        }

        message && uni.showToast({
          title: message,
          icon: 'none'
        })
      },
      openPopup() {
        this.$refs.popupUnlock.open();
      },
      async onAdd() {
        const res = await this.fetchCheckShare()
        console.log('--res', res)

        const { result, message } = res

        // todo: check
        if (result) { // 转发已达标
          this.isLocked = false
          return
        }

        this.openPopup()
      }
    },
  }
</script>

<style scoped lang="scss">
.page {
  position: relative;
  height: 100vh;
}

.content {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.avatar {
  width: 180rpx;
  height: 180rpx;
  margin-top: 127rpx;
}

.nick-name {
  color: #1B1B1B;
  font-size: 32rpx;
  margin-top: 30rpx;
}

.btn, .qr {
  position: absolute;
}

.btn {
  width: calc(100% - 60rpx*2);
  height: auto;
  left: 60rpx;
  bottom: 292rpx;

  background-color: #07C160;
  border: none;
  color: #FFFFFF;
  font-size: 28rpx;
  line-height: 1;
  border-radius: 45rpx;
  padding: 25rpx 0;
  box-sizing: border-box;
}

.qr {
  width: 350rpx;
  height: 350rpx;
  bottom: 269rpx;
}
</style>