// pages/store/store.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        stores      : [], //门店列表
        id          : 0,  //优惠券id
        searchText  : '', //搜索关键字
        longitude   : 0,  //经度
        latitude    : 0,  //纬度
        //省份选择
        areas       : [],
        areaIndex   : 0,
        //市级选择
        cityList    : [],
        cityIndex   : 0,
        //区域选择
        regiList    : [],
        regiIndex   : 0,

        page        : {}, //下一页
        lodingStats : false //加载状态
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad (options) {
        // 优惠券id
        this.setData({
            id :options.id
        })

        // 获取位置
        wx.getLocation({
            type: 'gcj02',
            success: res=> {
                this.setData({
                    longitude : res.longitude,
                    latitude  : res.latitude
                })
                // 解析坐标
                getApp().qqmapsdk.reverseGeocoder({
                    location: {
                        latitude    : res.latitude,
                        longitude   : res.longitude
                    },
                    success: res=>{
                        if(res.message == "query ok"){
                            let addressInfo = res.result.address_component
                            // 获取门店列表
                            this.storesInfo(addressInfo);
                        }else{
                            wx.showToast({
                                title: res.message,
                                icon : 'none'
                            })
                        }
                    }
                })
            }
        })
    },

    /**
     * 选择城市信息
     */
    regi(e){
        let chantType  = e.currentTarget.dataset.type,
            chantIndex = e.detail.value,
            name       = ""

        if(chantType == "area"){
            name = "areaIndex"
        }else if(chantType == "city"){
            name = "cityIndex"
            this.getAreas(this.data.cityList[chantIndex].code, "")
        }else if(chantType == "regi"){
            name = "regiIndex"
        }

        this.setData({
            [name]  : chantIndex
        })

        // 获取门店列表
        this.storesInfo()
    },

    /**
     * 门店列表
     */
    storesInfo(addressInfo, page) {
        let coupon_id    = this.data.id, 
            user_lng     = this.data.longitude,
            user_lat     = this.data.latitude,
            province_id = "",
            city_id     = "",
            district_id = "",
            title       = this.data.searchText || "",
            areaIndex   = this.data.areaIndex,
            cityIndex   = this.data.cityIndex,
            regiIndex   = this.data.regiIndex,
            cityList    = this.data.cityList,
            areas       = this.data.areas,
            regiList    = this.data.regiList
        if(!addressInfo){
            province_id = areas[areaIndex].code
            city_id     = cityList[cityIndex].code
            district_id = regiList[regiIndex].code
        }

        wx.$api.user.stores(coupon_id, province_id, city_id, district_id, title, user_lng, user_lat, page).then(res=>{
            let stores = this.data.stores,
                newData = []
            if(page == 1 || page == undefined) stores = []
            newData = stores.concat(res.data.stores.data)

            newData.map(res=>{
                let distance = res.distance
                if(res.distance > 1000){
                    distance = (distance / 1000) + "km"
                }else{
                    distance = distance + "m"
                }
                res.km = distance
            })

            if(addressInfo){
                cityList  = res.data.citys
                areas     = res.data.provinces

                areaIndex = areas.findIndex(val => val.name == addressInfo.province)
                cityIndex = cityList.findIndex(val => val.name == addressInfo.city)
                // this.getAreas(cityList[cityIndex].code, addressInfo.district)
                this.getAreas(cityList[cityIndex].code, '')

            }
            this.setData({
                areas       : areas,
                stores      : newData,
                cityList    : cityList,
                areaIndex   : areaIndex,
                cityIndex   : cityIndex,
                page        : res.data.stores.page,
                lodingStats : false
            })
            wx.stopPullDownRefresh()
        }).catch(err => {});
    },

    /**
     * 获取区级信息
     */
    getAreas(sn, areas){
        wx.$api.user.areas(sn).then(res=>{
            let regiList  = res.data,
                regiIndex = 0
            
            if(areas) regiIndex = regiList.findIndex(val => val.name == areas)
            
            this.setData({
                regiList    : regiList,
                regiIndex   : regiIndex
            })
        }).catch(err => {});
    },

    /**
     * 查看门店详情页
     */
    detailsTap(e) {
        let store_id     = e.currentTarget.dataset.id,
            user_lng     = this.data.longitude,
            user_lat     = this.data.latitude

        wx.navigateTo({
          url: '/pages/storeDetails/storeDetails?store_id=' + store_id + '&user_lng=' +  user_lng  + '&user_lat=' + user_lat,
        })
    },

    /**
     * 搜索门店
     */
    searchForm(e) {
        this.setData({
            searchText : e.detail.value.search
        })

        // 获取门店列表
        this.storesInfo()
    },

    /**
     * 上拉加载
     */
    onReachBottom(){
        this.setData({
            lodingStats: true
        })
        let pageNumber = this.data.page.current
        if(this.data.page.has_more){
            pageNumber++
            this.setData({
                page: pageNumber
            })
            this.storesInfo('', pageNumber)
        }
    }
})