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

    /**
     * 页面的初始数据
     */
    data: {
        page: 1,
        pageSize: 10,
        lock: false,
        noResult: false,
        noMore: false,
        isLogin: false,
        hidden1: false,
        hidden2: false,
        mobileTop: 'TONY WU',
        couponNum: 0,
        productNum: 0,
        goodsType: 1,
        goodsItemList: []

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            isLogin: getMobileCache() != '',
            mobileTop:getMobileCache(),
        })
        if(this.data.isLogin){
            this.setData({
                hidden1: false,
                hidden2: true,
            })
            this.getUserPrizeList();
        }
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },
    getUserPrizeList: function() {
        LuckDraw.getUserPrizeList({
            page: this.data.page,
            pageSize: this.data.pageSize,
            mobile: getMobileCache(),
            goodsType: this.data.goodsType
        }).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.goodsItemList) || data.goodsItemList.length == 0) {
            console.log("数据为空");
            if (this.data.page == 1) {
                this.setData({
                    mobileTop:getMobileCache(),
                    couponNum: data.couponNum,
                    productNum: data.productNum,
                    noResult: true
                })
            } else {
                this.setData({
                    mobileTop:getMobileCache(),
                    noMore: true
                })
            }
            return
        }
        data.goodsItemList.forEach(v => {
            let beginTime = v.couponBeginTimestamp
            let endTime = v.couponEndTimestamp
            v.couponBeginTimestamp = parseTime(beginTime, "{y}.{m}.{d}")
            v.couponEndTimestamp = parseTime(endTime, "{y}.{m}.{d}")
            if(v.couponType == 'D' && v.discount >0){
                v.discount /= 10
            }
            if(v.couponType == 'C' && v.discount >0){
                v.reduceCost /= 100
            }
        })

        if (this.data.page == 1) {
            this.setData({
                couponNum: data.couponNum,
                productNum: data.productNum
            })
          }
        this.data.goodsItemList = this.data.goodsItemList.concat(...data.goodsItemList)
        this.setData({
            mobileTop:getMobileCache(),
            goodsItemList: this.data.goodsItemList
        })
    },

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

    toPrize(e) {
        const url = "prize?id=" + e.currentTarget.dataset.id
        wx.redirectTo({
            url
        })
    },
    getProfile(e) {
        this.setData({
            hidden1: false,
            hidden2: true,
            page: 1,
            goodsType: 1,
            noMore: false,
            noResult: false
        })
        this.data.goodsItemList = [];
        this.getUserPrizeList()
    },
    getPrize(e) {

        this.setData({
            hidden1: true,
            hidden2: false,
            page: 1,
            goodsType: 2,
            noMore: false,
            noResult: false
        })
        this.data.goodsItemList = [];
        this.getUserPrizeList()
    },
    turnPrize: function() {
        console.log("开始查看")
        if (this.lock || !this.data.isLogin) {
            return
        }
        this.lock = true
    },
    // 授权手机号
    getPhoneNumber(e) {
        getPhoneNumberSync(e, _ => {
            this.setData({
                isLogin: true,
                mobileTop: getMobileCache(),
                hidden1: false,
                hidden2: true,
            })
            this.getUserPrizeList()
        })
    },
    toRecordPrize(e) {
        const url = "recordPrize?id=" + e.currentTarget.dataset.id
        wx.redirectTo({
            url
        })
    },
})