<template>
  <view class="page-container">
    <full-adv ref="fullAdv" @close="sign()" />
    <reward ref="reward" @close="back()" />
  </view>
</template>

<script>
import fullAdv from '@/components/full-adv'
import reward from '@/components/reward'
import { app, user } from '@/utils/api'

export default {
  components: {
    fullAdv,
    reward
  },
  data() {
    return {
      isLogin: false
    }
  },
  async onShow() {
    await this.$onLaunched
    const token = getApp().globalData.userToken
    this.isLogin = token ? true : false
    this.getData()
  },
  methods: {
    getData() {
      uni.showLoading({
        title: '正在加载',
        mask: true
      })
      app.fullAdv().then(res => {
        uni.hideLoading()
		console.log(res);
        const resData = res.data
        if (resData) {
          this.$refs.fullAdv.type = 'sign'
          this.$refs.fullAdv.adv = resData[Math.floor(Math.random() * resData.length)]
          this.$refs.fullAdv.showPopup = true
        }else {
          this.sign()
        }
      }).catch(e => {
        uni.showToast({
          icon: 'none',
          title: '服务器异常，请稍后重试',
          duration: 3000
        })
        setTimeout(() => {
          this.back()
        }, 1500)
      })
    },
    sign() {
      if (this.isLogin) {
        uni.showLoading({
          title: '正在加载',
          mask: true
        })
        user.sign().then(res => {
          if (res.code == 0) {
            uni.showToast({
                icon: 'none',
                title: res.msg,
                duration: 3000
            })
            setTimeout(() => {
              this.back()
            }, 1000)
          } else if(res.code == 1) {
            uni.hideLoading()
            this.$refs.reward.onshow(res.data.task_coin)
          }
        })
      } else {
        uni.redirectTo({
          url: '/pages/user/login'
        })
      }
    }
  }
}
</script>

<style lang="scss">
.page-container {
  height: 100%;
  background-color: #000;
}
</style>