const app = getApp()
import {
    getActivities
} from '~/api/global'
import {
    storeBindingsBehavior
} from 'mobx-miniprogram-bindings'
import {
    store
} from '~/store/index'
Component({
    behaviors: [storeBindingsBehavior],
    storeBindings: {
        store,
        fields: {
            userInfo: 'userInfo'
        },
    },
    properties: {
        classify: {
            type: Number,
            value: 1
        }
    },
    /**
     * 组件的初始数据
     */
    data: {
        //1:图片,2:邀新榜,3:热播榜,4:挑战pk榜,5,朗读赛,6,领取勋章,7年包红包8,次数红包,9邀新
        type: '',
        activityList: [],
        isIos: app.globalData.isIOS,
        dsqList: []
    },
    lifetimes: {
        attached() {
            this.getActivities()
        },
        detached() {
            this.data.dsqList.forEach(item => {
                clearInterval(item)
            })
        }
    },
    /**
     * 组件的方法列表
     */
    methods: {
        async getActivities() {
            this.data.dsqList.forEach(item => {
                clearInterval(item)
            })
            let activityList = await getActivities({
                classify: this.properties.classify,
                grade: this.data.userInfo.grade
            })
            this.setData({
                activityList,
            })
            activityList.forEach((item, index) => {
                if (item.bannerType == 4 && item.voucherType) {
                    this.activityTimeOut(item.endTime, index)
                }
            })
        },
        jumpUserInfo({
            currentTarget
        }) {
            if (!currentTarget.dataset.uid) {
                return
            }
            wx.navigateTo({
                url: `/pages/personal/index?uid=${currentTarget.dataset.uid}&type=user`,
            })
        },
        activityTimeOut(oTime, index) {
            let inputTime = new Date(oTime)
            let dsq = setInterval(() => {
                var nowTime = new Date();
                //把剩余时间毫秒数转化为秒
                var times = (inputTime - nowTime) / 1000;
                if (times <= 0) {
                    this.setData({
                        [`activityList[${index}].hour`]: '00',
                        [`activityList[${index}].minute`]: '00',
                        [`activityList[${index}].second`]: '00',
                        [`activityList[${index}].finish`]: true,
                    })
                    return clearInterval(dsq)
                }
                //计算小时数 转化为整数
                var h = parseInt(times / 60 / 60);
                //如果小时数小于 10,要变成 0 + 数字的形式 赋值给盒子
                let hour = h < 10 ? "0" + h : h;
                //计算分钟数 转化为整数
                var m = parseInt(times / 60 % 60);
                //如果分钟数小于 10,要变成 0 + 数字的形式 赋值给盒子
                let minute = m < 10 ? "0" + m : m;
                //计算描述 转化为整数
                var s = parseInt(times % 60);
                //如果秒钟数小于 10,要变成 0 + 数字的形式 赋值给盒子
                let second = s < 10 ? "0" + s : s;
                this.setData({
                    [`activityList[${index}].hour`]: hour,
                    [`activityList[${index}].minute`]: minute,
                    [`activityList[${index}].second`]: second,
                    [`activityList[${index}].finish`]: false,
                })
                times = --times;
            }, 1000);
            this.setData({
                dsqList: [...this.data.dsqList, dsq]
            })
        },
        activityEvent({
            currentTarget
        }) {
            //1:图片,2:邀新榜,3:热播榜,4:挑战pk榜,5,朗读赛,6,领取勋章,9:邀新,10ai头像
            let {
                type,
                id,
                title,
                explain
            } = currentTarget.dataset.info
            if (type == 1) {
                wx.navigateTo({
                    url: `/pages/rankIntro/index?title=${title}&img=${explain}`,
                })
            }
            if (type == 5) {
                wx.navigateTo({
                    url: `/pages/match/index?activityId=${id}`,
                })
            }
            if ([2, 3, 4].includes(type)) {
                wx.navigateTo({
                    url: `/pages/ranking/index?id=${id}&type=${type}`,
                })
            }
            if (type == 9) {
                wx.navigateTo({
                    url: "/pages/invite/index",
                })
            }
            if (type == 10) {
                wx.navigateTo({
                    url: "/pages/aiAvatar/index",
                })
            }
        },
        drawVoucher({
            currentTarget
        }) {
            let info = currentTarget.dataset.info
            if (info.finish) {
                return
            }
            this.selectComponent('#voucher').open({
                type: info.type,
                id: info.id,
                voucherType: info.voucherType,
                preferential: info.price
            })
        }
    }
})