// pages/index/index.js
import recordSource from '../../utils/recordSource';
const getReq = require('../../config.js').getReq;
let app = getApp();

Page({
    /**
     * 页面的初始数据
     */
    data: {
        tabs: [],
        prop_special: [],
        surplus_special: [],
        special_datas: {},
        summery: [],
        isScroll: false,
        isSendProp: false,
        firstLoad: true,
        fromSource: '',
        tipsFlag: false,
        tipsDatas: null,
        daliy_bonus: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log('index page onLoad options:', options)
        if (options && options.relay_id) {
            app.globalData.relay_id = options.relay_id
            console.log('options.relay_id:', options.relay_id)
        }
        if (options && options.channel) {
            app.globalData.channel_num = options.channel
            console.log('options.channel:', options.channel)
        }
        this.getDatas(1040)
        // this.getDatas(1038)
    },
    onWxButtonEvent:function (e) {
        console.log('index: onWxButtonEvent',e); 
        if(e.detail.event_type == 'getUserInfo') {
            if (e.detail.authen == 'success') {
                wx.showModal({
                    title: '您已经领新人优惠券',
                    confirmText: "知道了",
                    showCancel: false,
                    success: function (res) {
                        app.switchtab("/pages/home/home");
                    }
                })
            }
            else {
                app.switchtab("/pages/home/home");
            }
        }
    },

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (!this.data.firstLoad) {
            app.setFromSource(this.data.fromSource)
        }
    },

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

    },

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

    },
    getDatas(special_id) {
        let fromSource = recordSource(app, 'act=special&op=index&special_id=0&page=10&curpage=1&client_type=mini')
        wx.showLoading({
            title: '加载中'
        });
        let self = this;
        console.log('fromSource =' + fromSource)
        getReq(app, {
            act: 'special',
            op: 'index',
            special_id: special_id
        }, function (res) {
            wx.stopPullDownRefresh()
            if (res.code == 200) {
                console.log(res)
                let prop_special = res.datas.special_list;
                let sent_down_special = prop_special.splice(0, 8);
                self.setData({
                    surplus_special: prop_special,
                    prop_special: sent_down_special,
                    special_datas: res.datas,
                    summery: res.datas.summary,
                    firstLoad: false,
                    fromSource: app.globalData.fromSource
                });
            }
            setTimeout(function () {
                wx.hideLoading()
            }, 1000)
        })
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.getDatas()
    },
    onPageScroll(e) {
        clearTimeout(this.showTop);
        this.showTop = setTimeout(() => {
            let scrollTop = e.scrollTop;
            if (scrollTop >= 300) {
                this.setData({
                    isScroll: true
                })
            }
            else {
                this.setData({
                    isScroll: false
                })
            }
        }, 100);
    },
    backTop() {
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 300
        })
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.isSendProp) {
            return;
        }

        let surplus_special_num = this.data.surplus_special.length;
        if (surplus_special_num > 0) {
            wx.showLoading({
                title: '加载中'
            });
            this.setData({
                isSendProp: true
            });
            this.data.isSendProp = true;
            let prop_special = this.data.surplus_special;
            let sent_down_special = prop_special.splice(0, 8);
            this.setData({
                surplus_special: prop_special,
                prop_special: this.data.prop_special.concat(sent_down_special)
            })
            this.setData({
                isSendProp: false
            });
            setTimeout(function () {
                wx.hideLoading()
            }, 1500)
        }
        else {
            return;
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return app.cardShareInfo();
    }
})