import {
  getProducts,
  getTasks,
  submitTask
} from '~/api/global'
import {
  buyNum,
  buyVip,
  getMyInfo,
  getVipInfo
} from '~/api/user'
const app = getApp()

Component({
  data: {
    //弹窗显示控制 
    showModalStatus: false,
    isIos: app.globalData.isIOS,
    userInfo: {},
    productNum: {},
    productVip: {},
    userInfo: {},
    vipTime: '',
  },
  lifetimes: {
    attached() {
      this.getProducts()
      this.setUserInfo()
    },
  },
  methods: {
    // 提交任务
    async submitTask({
      currentTarget
    }) {
      let id = currentTarget.dataset.type
      await submitTask({
        id
      })
      wx.showToast({
        title: id == '1' ? '签到成功!' : id == 3 ? "观看成功!" : "",
        icon: "none"
      })
      this.setUserInfo()
    },
    async getProducts() {
      let products = await getProducts()
      let productVip = products.find(item => {
        return item.type == 1
      })
      let productNum = products.find(item => {
        return item.type == 2
      })
      this.setData({
        productNum,
        productVip
      })
    },
    // 设置用户信息及vip状态和任务完成情况
    async setUserInfo() {
      let userInfo = await getMyInfo()
      let vipTime = await getVipInfo()
      this.getTasks()
      this.setData({
        userInfo,
        vipTime,
      })
    },
    // 调起广告
    rewardedVideo() {
      if (this.data.tasks.length != 3 || this.data.tasks[2].completed) {
        return
      }
      this.selectComponent('#advert').rewardedVideo();
    },
    async getTasks() {
      let tasks = await getTasks()
      this.setData({
        tasks
      })
    },
    //购买vip和购买次数不是一个接口 type 1001是vip,1010是次数
    async toBuy({
      currentTarget
    }) {
      let that = this
      let productId = currentTarget.dataset.type
      wx.showLoading({
        title: '提交中',
        mask: true
      })
      let res = ''
      if (productId == '1001') {
        res = await buyVip({
          productId
        }).finally(() => {
          wx.hideLoading()
        })
      } else if (productId == '1010') {
        res = await buyNum({
          productId
        }).finally(() => {
          wx.hideLoading()
        })
      } else {
        wx.hideLoading()
        wx.showToast({
          title: "支付失败,请重试",
          icon: "none"
        })
      }
      let {
        timeStamp,
        nonceStr,
        signType,
        paySign
      } = res
      // package保留字
      wx.requestPayment({
        timeStamp,
        nonceStr,
        package: res.package,
        signType,
        paySign,
        success(res) {
          wx.showToast({
            title: "支付成功",
            duration: 2500
          })
          setTimeout(() => {
            that.setUserInfo()
          }, 1500)
        },
        fail(res) {
          wx.showToast({
            title: "支付失败",
            icon: "none"
          })
        }
      })
    },
    //底部弹出框
    showModal: function () {
      // 背景遮罩层
      var animation = wx.createAnimation({
        duration: 200,
        timingFunction: "linear",
        delay: 0
      })
      animation.translateY(300).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: true
      })
      setTimeout(function () {
        animation.translateY(0).step()
        this.setData({
          animationData: animation.export()
        })
      }.bind(this), 200)
    },

    //点击背景面任意一处时,弹出框隐藏
    hideModal: function () {
      //弹出框消失动画
      var animation = wx.createAnimation({
        duration: 200,
        timingFunction: "linear",
        delay: 0
      })
      animation.translateY(300).step()
      this.setData({
        animationData: animation.export(),
      })
      setTimeout(function () {
        animation.translateY(0).step()
        this.setData({
          animationData: animation.export(),
          showModalStatus: false
        })
      }.bind(this), 200)
    },
  }
})