const util = require('../../../utils/util.js');
Component({
  options: {
    addGlobalClass: true
  }, //使用全局css样式
  properties: {
    show: {
      type: Boolean,
      observer: function (newVal) {
        this.setData({
          show: newVal
        });
        if (newVal) {
          this.stat()
        }
      }
    },
    aid: {
      type: String,
      observer: function (newVal) {
        this.setData({
          aid: newVal
        });
      }
    },
    status: {
      type: Number,
      observer: function (newVal) {
        console.log('status', newVal)
        this.setData({
          status: newVal
        });
      }
    },
    presellOpen: {
      type: Number,
      observer: function (newVal) {
        console.log('presellOpen', newVal)
        this.setData({
          presellOpen: newVal
        });
      }
    }
  },
  data: {
    disabled: false,
    status: 0,
    presellOpen: 0,
    aid: '',
    show: false,
    checkedTime: 5,
    vip_info: {},
    timeList: [{
        time: 5,
        timeContent: '5分钟'
      },
      {
        time: 10,
        timeContent: '10分钟'
      },
      {
        time: 15,
        timeContent: '15分钟'
      },
      {
        time: 20,
        timeContent: '20分钟'
      },
    ],
    needLevel: [{
        time: 5,
        level: 2,
        levelName: '白银',
        consume_coin: '50',
      },
      {
        time: 10,
        level: 3,
        levelName: '黄金',
        consume_coin: '100',
      },
      {
        time: 15,
        level: 4,
        levelName: '一钻',
        consume_coin: '200',
      },
      {
        time: 20,
        level: 5,
        levelName: '二钻',
        consume_coin: '500',
      },
    ]
  },
  methods: {
    jumpPage() {
      wx.navigateTo({
        url: '/pages/account/monetaryCenter/ruleInstract',
      })
    },
    // 判断选中时间是否可兑换
    getOptionchecked(option) {
      let disabled = false,
        vip_level = this.data.vip_info;
      this.data.needLevel.forEach(el => {
        if (option == el.time && ((el.level > vip_level.level) || (el.consume_coin > vip_level.allCoin))) {
          disabled = true;
        }
      });
      this.setData({
        disabled
      })
    },
    selectTime(e) {
      let time = e.currentTarget.dataset.time;
      this.setData({
        checkedTime: time
      })
      this.getOptionchecked(time)
    },
    close() {
      this.triggerEvent('close')
    },
    // 获取宝贝币相关参数
    stat() {
      let that = this;
      util.stat().then(res => {
        let checkedTime = 0;
        let data = util.getCoinLevel(res.allCoin)
        data.allCoin = res.sumCoin;
        if (data.level < 2) {
          checkedTime = 5;
        } else if (data.level > 5) {
          checkedTime = 20;
        } else {
          that.data.needLevel.forEach(el => {
            if (el.level == data.level) {
              checkedTime = el.time;
            }
          });
        }
        that.setData({
          vip_info: data,
          stat: res,
          checkedTime
        })
        that.getOptionchecked(checkedTime)
      })
    },
    sub() {
      let that = this;
      if (that.data.presellOpen == 2 || (that.data.presellOpen == 0 && that.data.status == 6)) {
        that.triggerEvent('showPrompt', {
          checkedTime: that.data.checkedTime
        })
      } else {
        util.showTips('已到达活动开售时间，无法进行兑换');
      }
    }
  }
})