// pages/groupmeallist/storeList/storeList.js


import StoreApi from '../../../api/storeList'
const cache = require('../../../utils/cache.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        phoneshow:false,
        isShowConfirmPannel:false,
        idx:'',
        page:1,
        pageSize:8,
        longitude:"",
        latitude:"",
        keyword:"",
        storeList:[],
        selectStoreInfo:null,
        id:0,
        isClicked:false, //是否已经点击过了，默认没有
        pageSource:"",
        storePhone:'',
        store:null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },



        /**
     * 获取门店列表
     */
    async getNearbyStoreList(step='init') {
        let _self = this;
        try {
            let storeRes = await StoreApi.getNearbyStoreList(
                _self.data.page,
                _self.data.pageSize,
                _self.data.keyword,
                _self.data.longitude,
                _self.data.latitude);
            if(storeRes.code==200 && storeRes.msg=='success') {
                let list = storeRes.data;
                if(list.length!=0) {
                    list.map((item,index)=>{
                        if(item.distance!==null){
                            let distance = parseFloat(item.distance);
                            item.distance = (distance).toFixed(2);
                        }
                       
                        if(item.id==_self.data.id && _self.data.isClicked == false){
                            item.active = true;
                            _self.setData({
                                selectStoreInfo:item,
                            })
                        }else {
                            item.active = false;
                        }  
                    })
                }
                if(step=='init') {  //初始化
                    _self.setData({
                        storeList:list
                    })

                    console.log(list.length)
                } else {
                    if(list.length==0) {
                       _self.setData({
                           page:_self.data.page-1
                       })     
                    } else {
                        //分页
                        _self.setData({
                            storeList:_self.data.storeList.concat(list),
                        })  
                    }
                     
                }
            }
        } catch(error) {
            console.log(error);
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let _self = this;
        //获取最近的经纬度
        let locationPosition = cache.getNearLocation()
        if(locationPosition!=null) {
            _self.setData({
                longitude:locationPosition.longitude,
                latitude:locationPosition.latitude
            })
        }
        _self.getNearbyStoreList();
    },

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

    },

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

    },

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

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let _self = this;
        _self.setData({
            page:_self.data.page+1
        })
        //获取门店列表
        _self.getNearbyStoreList('pull')
    },

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

    },
    // 拉出电话面板
    showPhoneTab(e){
        let idx = e.currentTarget.dataset.idx;
        var that = this;
        let storePhone=this.data.storeList[idx].phone
        this.setData({
            storePhone:storePhone,
            phoneshow : !this.data.phoneshow
          
        })

    },
    // 确认门店拉出
    confirmstore(){},
    confirmtPannel: function (e) {
        this.setData({
                phoneshow : false
        })
        if (this.data.isShowConfirmPannel) {
            this.closeConfirmPannel()
            return
        }

        this.showConfirmPannel(e);
    },
    //关闭购物车面板
  closeConfirmPannel: function() {
    this.setData({
        isShowConfirmPannel: false,
            phoneshow : false

    })
},
  //显示对话框
  showConfirmPannel (e) {
    // 显示遮罩层
    let idx = e.currentTarget.dataset.idx;
    var that = this;
    let store=this.data.storeList[idx]

    var animation = wx.createAnimation({
        duration: 100,
        timingFunction: "linear",
        delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
        animationData: animation.export(),
        isShowConfirmPannel: true,
        store:store,
        idx:idx
    })
    setTimeout(function () {
        animation.translateY(0).step()
        this.setData({
            animationData: animation.export()
        })
    }.bind(this), 100)
},

// 确定门店跳回商品列表
confirmStoreGoToStoreGoods(e){
    let way = e.currentTarget.dataset.way;
    //缓存门店取餐方式
        cache.setStoreWay(way)
        console.log(this.data.storeList[this.data.idx])
        cache.setCurrStore(this.data.storeList[this.data.idx]);
    wx.switchTab({  
        url:'/pages/groupmeallist/storeGoods/storeGoods'       
            });

},


  /**
     * 选择门店
     */
    sureStore() {
        let _self = this;
        //存储选中的地址
       
        console.log(_self.data.pageSource);
        if(_self.data.pageSource == "storeGoods"){
            console.log(999999);
            wx.redirectTo({
                url: '/pages/storeGoods/storeGoods'
            })
           
        }else{
            wx.reLaunch({
                url: '/pages/index/index?step=storeSelect'
            })
         }
    },
})