// pages/luckDraw/recordPrize.js
import LuckDraw from '../../api/luck-draw'
import { parseTime } from '../../utils/util'
import {getMobileCache, getPhoneNumberNew as getPhoneNumberSync} from '../../utils/user'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        page: 1,
        pageSize: 10,
        lock: false,
        noResult: false,
        noMore: false,
        isLogin: false,
        mobileTop: 'TONY WU',
        userDrawItemList: [],
        showCustomPreview: false,

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (!options.activityId) {
            this.popMessage ('入参错误');
            wx.navigateTo({
              url: './index',
            })
            return
        }
        this.data.activityId = options.activityId
        this.setData({
            isLogin: getMobileCache() != ''
        })
        if(this.data.isLogin){
            this.getUserDrawRecord();
        }
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getUserDrawRecord: function() {
        LuckDraw.getUserDrawRecord({
            page: this.data.page,
            pageSize: this.data.pageSize,
            mobile: getMobileCache(),
            activityId: this.data.activityId,
        }).then(res => {
            if (res.code == 200) {
                this.uaerPrizeListView(res.data)
            }
            this.data.lock = false
        }).catch(_ => {
            console.log(_)
            this.data.lock = false
        })
    },

    uaerPrizeListView: function(data) {
        if (!Array.isArray(data) || data.length == 0) {
            console.log("抽奖记录数据为空");
            if (this.data.page == 1) {
                this.setData({
                    noResult: true
                })
            } else {
                this.setData({
                    noMore: true
                })
            }
            return
        }
        data.forEach(v => {
            let beginTime = v.couponBeginTimestamp
            let endTime = v.couponEndTimestamp
            let createTime = v.createTime
            v.couponBeginTimestamp = parseTime(beginTime, "{y}.{m}.{d}")
            v.couponEndTimestamp = parseTime(endTime, "{y}.{m}.{d}")
            v.createTime = parseTime(createTime, "{y}.{m}.{d} {h}:{i}")
        })

        this.data.userDrawItemList = this.data.userDrawItemList.concat(...data)
        this.setData({
            userDrawItemList: this.data.userDrawItemList
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.lock || this.data.noMore) {
            return
        }
        this.data.lock = true
        this.data.page++
        this.getUserDrawRecord()
    },
    

    toLookHitPhoto(e) {
        console.log(e)
        let url = e.currentTarget.dataset.src;
        this.setData({
            showCustomPreview: true,
            previewImgSrc: url,
        })
    },
    closePreviewImage() {
        this.setData({
            showCustomPreview: false
        })
    },
    turnPrize: function() {
        console.log("开始查看")
        if (this.isLock || !this.data.isLogin) {
            return
        }
    },
    // 授权手机号
    getPhoneNumber(e) {
        getPhoneNumberSync(e, _ => {
            this.setData({
                isLogin: true,
                mobileTop:getMobileCache(),
            })
            console.log(this.data.mobileTop)
            this.getUserDrawRecord()
        })
    },
})