
/*
 * 本时生活
 */

const app = getApp()


Page({

    /**
     * 页面的初始数据
     */
    data: {
        swiperCurrent   :0,
        current         :0,  //轮播图当前的下标
        dots            :true,
        cityAll         : '',
        statusBarHeight : getApp().globalData.systInfo.statusBarHeight,
        longitude       : 0,      //经度
        latitude        : 0,      //纬度
        adverts         : [],     //轮播图
        stateType       : "silver",
        cardArr         : [],     //权益数组
        activities      : [],     //周五福利
        isUser          : false, //用户登录状态
        noticeData      : '',   //首页公告
        loading         : true,   //骨架屏加载
        address : {
            city: "",   //市
            area: "",   //区
            city_code: "", //市编号
            area_code: ""  //区编号
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow () {
        this.locaTion()
        // 获取用户登录状态
        this.setData({
            isUser : getApp().globalData.isUser
        })
    },

    /**
     * 获取位置
     */
    locaTion(){
        wx.getLocation({
            success: res => {
                this.setData({
                    latitude    : res.latitude,
                    longitude   : res.longitude
                })
               
            },
            complete: () => {
                // 获取卡权益
                this.indexInfo();
            }
        })
    },

    /**
     * 卡权益
     */
    indexInfo() {
        if(this.data.stateType == 'shaky') {
            wx.$api.index.index(this.data.city, this.data.longitude, this.data.latitude).then(res=>{
                this.setData({
                    adverts     : res.data.adverts,
                    cardArr     : res.data.rights,
                    activities  : res.data.activities,
                    noticeData  : res.data.notice,
                    loading     : false,
                    address     : {
                        city: res.data.location.city_name,
                        area: res.data.location.district_name || "",
                        city_code: res.data.location.city_id,
                        area_code: res.data.location.district_id || ""
                    }
                })
                wx.stopPullDownRefresh()
            }).catch(err=>{
                if(!err.login){
                    // 写入缓存
                    wx.setStorage({
                        key     : 'token',
                        data    : ''
                    })
                }
            })
        } else {
            wx.$api.index.choice(this.data.stateType,this.data.city, this.data.longitude, this.data.latitude).then(res=>{
                this.setData({
                    adverts     : res.data.adverts,
                    cardArr     : res.data.categorys,
                    noticeData  : res.data.notice,
                    cityName    : res.data.city_name,
                    loading     : false,
                    address     : {
                        city: res.data.location.city_name,
                        area: res.data.location.district_name || "",
                        city_code: res.data.location.city_id,
                        area_code: res.data.location.district_id || ""
                    }
                })
    
                wx.stopPullDownRefresh()
            }).catch(err=>{
                if(!err.login){
                    // 写入缓存
                    wx.setStorage({
                        key     : 'token',
                        data    : ''
                    })
                }
            })
        }
    },

    
    /**
     * tabs
     */
    orderTab(e){
        if(this.data.stateType != e.currentTarget.dataset.state){
            this.setData({
                stateType: e.currentTarget.dataset.state
            })
            // 获取卡权益
            this.indexInfo()

            // 重置轮播图
            this.setData({
                current      : 0,
                swiperCurrent: 0
            });
        }
    },

    /**
     * 处理未登录时的转跳
     */
    userNav(e){
        let id   = e.currentTarget.dataset.id
        wx.getStorage({
            key     : 'token',
            success:res=>{
                wx.navigateTo({
                  url: '/pages/classify/classify?id=' + id
                })
            },
            fail: (err) => {
                wx.navigateTo({
                    url: "/pages/login/login"
                })
            }
        })
    },

    /**
     * 点击图片放大
     */
    clickImg(e) {
        if(e.currentTarget.dataset.img == "") return
        let imgUrl = [e.currentTarget.dataset.img]
        wx.previewImage({
            urls        :  imgUrl, //需要预览的图片http链接列表，注意是数组
            current     :  imgUrl[0] // 当前显示图片的http链接，默认是第一个
        })        
    },

    /**
     * 周五福利抢购
     */
    Snapup(e) {
        let canBuy  = e.currentTarget.dataset.can,
            canText = e.currentTarget.dataset.text,
            canId   = e.currentTarget.dataset.id
        if(!canBuy) {
            wx.showToast({
                title   : canText,
                icon    : 'none',
                duration: 2000
            })
            return
        }

        // 跳转分类页
        wx.navigateTo({
            url: '/pages/welfare/welfare?id=' + canId
        })
    },

    /**
     * 活动中心权益跳转详情
     */
    rightNav(e) {
        let id   = e.currentTarget.dataset.id
        wx.getStorage({
            key     : 'token',
            success:res=>{
                wx.navigateTo({
                  url: '/pages/rights/rights?id=' + id
                })
            },
            fail: (err) => {
                wx.navigateTo({
                    url: "/pages/login/login"
                })
            }
        })
    },

    /**
     * 下拉刷新
     */
    onPullDownRefresh(){
        // 获取卡权益
        this.indexInfo();
        this.locaTion();
    },

     /**
     * 防止swiper控件卡死
     */
    swiperChange(e){
        if (this.data.current == 0 && this.data.swiperCurrent>1 ) {//卡死时，重置current为正确索引
            this.setData({ current: this.data.swiperCurrent });
        }
        else {//正常轮转时，记录正确页码索引
            this.setData({ swiperCurrent: e.detail.current });
        }
    
    }
})