// packageF/storeSign/storeSignDetail/storeSignDetail.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        show: false,
        points: "",
        info: [],
        withdraw_type: "",
        list: [],

        //more
        isLoadMore: true,
        page: 1,
        total_page: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.setNavigationBarTitle({
            title: '签到明细',
        })
    },
    initPage() {
        this.setData({
            list: []
        })
        this.data.page = 1;
        this.data.total_page = 0;
        this.isLoadMore = true;
    },
    getList() {
        let urls = '';
        if (this.data.active == 0) {
            urls = "plugin.store-attendance.frontend.member.detailed";
        } else if (this.data.active == 1) {
            urls = "plugin.store-attendance.frontend.member.record";
        } else if (this.data.active == 2) {
            urls = "plugin.store-attendance.frontend.member.withdraw";
        }
        let urlStr = app.getNetAddresss(urls);
        app._getNetWork({
            url: urlStr,
            data: {
                page: this.data.page,
            },
            success: (resdata) => {
                let res = resdata.data
                if (res.result == 1) {
                    this.data.isLoadMore = true;
                    this.data.total_page = res.data.last_page;
                    if (!this.data.total_page) {
                        this.data.total_page = 0;
                    }
                    this.setData({
                        list: res.data
                    })
                } else {
                    wx.showToast({
                        icon: 'none',
                        title: res.msg,
                        duration: 1500
                    });
                }
            },
            fail: function (res) {
                console.log(res);
            }
        });
    },
    tapinp(e) {
        this.setData({
            points: e.detail.value
        })
    },
    withdrawablePoints() {
        if (app._isTextEmpty(this.data.points)) {
            app.tips("请输入签到分")
            return
        }
        if (this.data.points > this.data.info.withdraw_points) {
            app.tips("输入签到分大于可提现签到分")
            return;
        }
        let urlStr = app.getNetAddresss("plugin.store-attendance.frontend.member.withdrawablePoints");
        app._getNetWork({
            url: urlStr,
            data: {
                points: this.data.points
            },
            success: (resdata) => {
                let res = resdata.data
                if (res.result == 1) {
                    app.tips(res.msg)
                    this.setData({
                        show: false,
                        points: ''
                    })
                    this.getData();
                } else {
                    wx.showToast({
                        icon: 'none',
                        title: res.msg,
                        duration: 1500
                    });
                }
            },
            fail: function (res) {
                console.log(res);
            }
        });
    },
    getData() {
        let urlStr = app.getNetAddresss("plugin.store-attendance.frontend.member.index");
        app._getNetWork({
            url: urlStr,
            success: (resdata) => {
                let res = resdata.data
                if (res.result == 1) {
                    this.setData({
                        info: res.data.statistics,
                        withdraw_type: res.data.withdraw_type
                    })
                } else {
                    wx.showToast({
                        icon: 'none',
                        title: res.msg,
                        duration: 1500
                    });
                }
            },
            fail: function (res) {
                console.log(res);
            }
        });
    },
    changeTap(e) {
        let ind = e.detail.index;
        this.setData({
            active: ind
        })
        this.initPage();
        this.getList();
    },
    showClose() {
        this.setData({
            show: false
        })
    },
    showTap() {
        this.setData({
            show: true
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.initPage();
        this.getData();
        this.getList();
    },

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

    },

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

    },

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

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.isLoadMore) {
            this._getMoreData();
        } else {
            console.log('没有更多数据');
        }
    },
    getMoreData() {
        let url = "";
        this.data.isLoadMore = false; // 防止多次请求分页数据
        if (this.data.page >= this.data.total_page) {
            return;
        } else {
            this.data.page = this.data.page + 1;
            if (this.data.active == 0) {
                url = app.getNetAddresss("plugin.store-attendance.frontend.member.detailed");
            } else if (this.data.active == 1) {
                url = app.getNetAddresss("plugin.store-attendance.frontend.member.record");
            } else if (this.data.active == 2) {
                url = app.getNetAddresss("plugin.store-attendance.frontend.member.withdraw");
            }
            app._getNetWork({
                url: url,
                data: {
                    page: this.data.page
                },
                success: (resdata) => {
                    let res = resdata.data
                    if (res.result == 1) {
                        this.data.isLoadMore = true;
                        this.setData({
                            list: res.data
                        })
                    } else {
                        this.data.page = this.data.page - 1;
                        this.isLoadMore = false
                        wx.showToast({
                            icon: 'none',
                            title: res.msg,
                            duration: 1500
                        });
                    }
                },
                fail: function (res) {
                    console.log(res);
                }
            });
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})