// pages/signIn/record/record.js
import SignIn from '../../../api/signIn'
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,
        noCoupon: false,
        noDetail: false,
        noUtility: false,
        isLogin: false,
        hiddenCoupon: true,
        hiddenDetail: true,
        hiddenUtility: true,
        mobileTop: 'TONY WU',
        couponNum: 0,
        productNum: 0,
        couponList: [],
        detailList: [],
        stateList: [],
        notUseNum: 0

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            isLogin: getMobileCache() != ''
        })
        if(this.data.isLogin){
            this.setData({
                hiddenCoupon: false,
                hiddenDetail: true,
                hiddenUtility: true,
                noCoupon: true,
                noDetail: false,
                noUtility: false,
            })
            // this.getUserAwardCouponNum();
            this.getRequestData();
        }
        
    },

    // 类型区分
    getRequestData() {
        if (this.data.noCoupon) {
            this.getUserAwardCouponList();
        }
        if (this.data.noDetail) {
            this.getUserSignInList();
        }
        if (this.data.noUtility) {
            this.getCouponStateList();
        }
    },

    builtPublicParams() {
        return {
            page: this.data.page,
            pageSize: this.data.pageSize,
            mobile: getMobileCache(),
        }
    },
    // 优惠券数据
    getUserAwardCouponList: function() {
        SignIn.getUserAwardCouponList(this.builtPublicParams()).then(res => {
            if (res.code == 200) {
                this.userCouponListView(res.data)
            }
            this.data.lock = false
        }).catch(_ => {
            console.log(_)
            this.data.lock = false
        })
    },

    userCouponListView: function(data) {
        if (!Array.isArray(data) || data.length == 0) {
            console.log("优惠券数据为空");
            if (this.data.page == 1) {
                this.setData({
                    mobileTop:getMobileCache(),
                    noResult: true
                })
            } else {
                this.setData({
                    mobileTop:getMobileCache(),
                    noMore: true
                })
            }
            return
        }

        const couponListT = []
        data.forEach(v => {
            v.coupon.isSn = true
            v.coupon.couponStartTime = v.coupon.couponBeginTimestamp
            v.coupon.couponEndTime = v.coupon.couponEndTimestamp
            couponListT.push(v.coupon)
        })

        this.data.couponList = this.data.couponList.concat(...couponListT)
        this.setData({
            mobileTop:getMobileCache(),
            couponList: this.data.couponList
        })
    },

    //未使用的券数量
    getUserAwardCouponNum: function() {
        SignIn.getUserAwardCouponNum(this.builtPublicParams()).then(res => {
            if (res.code == 200) {
                this.setData({
                    notUseNum: res.data.notUseNum
                })
            }
            this.data.lock = false
        }).catch(_ => {
            console.log(_)
            this.data.lock = false
        })
    },

    // 签到明细
    getUserSignInList: function() {
        SignIn.getUserSignInList(this.builtPublicParams()).then(res => {
            if (res.code == 200) {
                this.userDetailListView(res.data)
            }
            this.data.lock = false
        }).catch(_ => {
            console.log(_)
            this.data.lock = false
        })
    },

    userDetailListView: function(data) {
        if (!Array.isArray(data) || data.length == 0) {
            console.log("获取明细数据为空");
            if (this.data.page == 1) {
                this.setData({
                    mobileTop:getMobileCache(),
                    noResult: true
                })
            } else {
                this.setData({
                    mobileTop:getMobileCache(),
                    noMore: true
                })
            }
            return
        }
        data.forEach(v => {
            let crateTime = v.crateTime
            v.crateTime = parseTime(crateTime, "{y}-{m}-{d} {h}:{i}")
        })

        this.data.detailList = this.data.detailList.concat(...data)
        this.setData({
            mobileTop:getMobileCache(),
            detailList: this.data.detailList
        })
    },

    // 使用失效
    getCouponStateList: function() {
        SignIn.getUserAwardCouponUseStateList(this.builtPublicParams()).then(res => {
            if (res.code == 200) {
                this.userStateListView(res.data)
            }
            this.data.lock = false
        }).catch(_ => {
            console.log(_)
            this.data.lock = false
        })
    },

    userStateListView: function(data) {
        if (!Array.isArray(data) || data.length == 0) {
            console.log("使用失效数据为空");
            if (this.data.page == 1) {
                this.setData({
                    mobileTop:getMobileCache(),
                    noResult: true
                })
            } else {
                this.setData({
                    mobileTop:getMobileCache(),
                    noMore: true
                })
            }
            return
        }
        data.forEach(v => {
            let crateTime = v.crateTime
            v.crateTime = parseTime(crateTime, "{y}-{m}-{d} {h}:{i}")
        })

        this.data.stateList = this.data.stateList.concat(...data)
        this.setData({
            mobileTop:getMobileCache(),
            stateList: this.data.stateList
        })
    },

    // 授权手机号
    getPhoneNumber(e) {
        getPhoneNumberSync(e, _ => {
            this.setData({
                isLogin: true,
                mobileTop: getMobileCache(),
                noCoupon: true,
                hiddenCoupon: false,
                hiddenDetail: true,
                hiddenUtility: true,
            })
            this.getRequestData();
            // this.getUserAwardCouponNum();
        })
    },

    // 优惠券
    getCoupon(e) {
        this.setData({
            hiddenCoupon: false,
            hiddenDetail: true,
            hiddenUtility: true,
            page: 1,
            goodsType: 1,
            noMore: false,
            noResult: false,
            noCoupon: true,
            noDetail: false,
            noUtility: false,
            couponList: [],
            detailList: [],
            stateList: [],
        })
        this.getUserAwardCouponList()
    },
    //明细
    getDetail(e) {
        this.setData({
            hiddenCoupon: true,
            hiddenDetail: false,
            hiddenUtility: true,
            page: 1,
            goodsType: 2,
            noMore: false,
            noResult: false,
            noDetail: true,
            noCoupon: false,
            noUtility: false,
            couponList: [],
            detailList: [],
            stateList: [],
        })
        this.getUserSignInList()
            
    },
   //使用失效
    getUtility(e) {
        this.setData({
            hiddenCoupon: true,
            hiddenDetail: true,
            hiddenUtility: false,
            page: 1,
            goodsType: 2,
            noMore: false,
            noResult: false,
            noUtility: true,
            noCoupon: false,
            noDetail: false,
            couponList: [],
            detailList: [],
            stateList: [],
        })
        this.getCouponStateList()
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    scrollToBottom() {
        if (this.data.lock || this.data.noMore) {
            return
        }
        this.data.lock = true
        this.data.page++
        this.getRequestData();
    },

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

    }
})