// index.js
let myPro = require("../../utils/wxRequest.js");
let util = require("../../utils/util.js");
Page({
    data: {
		imgUrl: getApp().globalData.imgUrl,
		loading: false, // 全局loading
		page: 1,
		size: 10,
		finished: false,
       	storeList: [], // 店列表
		fare_type: 1, // 0外卖 1自提   默认自提
		showOpenLocation: false, // 开启定位提醒
		showTips: false,
		checkedAddress: null,
		lat: null,
		lng: null
    },
    onLoad: function (options) {
        // Do some initialize when page load.
		let that = this;
		// 拒绝定位时,先提醒去开启
		if(getApp().globalData.location_auth == -1){
			that.setData({
				showOpenLocation: true
			});
		}
    },
    onShow: function () {
        // Do something when page show.
		let that = this;
		// 已选的店
		if(wx.getStorageSync("store")){
			that.setData({
				store: JSON.parse(wx.getStorageSync("store"))
			})
		};
		// 获取配送流程
		if(wx.getStorageSync("fare_type")){
			that.setData({
				fare_type: wx.getStorageSync("fare_type")
			});
		};
		// 获取配送地址
		if(wx.getStorageSync("checkedAddress")){
			that.setData({
				checkedAddress: JSON.parse(wx.getStorageSync("checkedAddress"))
			});
		};
		// 经纬度
		if(that.data.fare_type == 0 && that.data.checkedAddress){
			// 外卖 按收货地址的经纬度
			that.setData({
				lat: that.data.checkedAddress.lat,
				lng: that.data.checkedAddress.lnt
			})
		}else{
			// 自取 按定位的经纬度
			that.setData({
				lat: getApp().globalData.lat,
				lng: getApp().globalData.lng
			})
		}
		// 已定位
		if(getApp().globalData.location_auth == 1){
			// 已定位
			that.setData({
				page: 1,
				storeList: [],
				finished: false
			});
			that.getStoreList();
		}
    },
    onReady: function () {
        // Do something when page ready.
    },
    onHide: function () {
        // Do something when page hide.
    },
    onUnload: function () {
        // Do something when page close.
    },
    onPullDownRefresh: function () {
        
    },
    onReachBottom: function () {
        // Do something when page reach bottom.
		let that = this;
		if(!that.data.finished){
			that.getStoreList();
		};
    },
    onPageScroll: function () {
        // Do something when page scroll
		
    },
    onResize: function () {
        // Do something when page resize
    },
	// 获取店列表
	getStoreList(){
		let that = this;
		let data = {
			lat: that.data.lat,
			lng: that.data.lng,
			page: that.data.page,
			size: that.data.size
		};
		// console.log('参数的经纬度',that.data.lat,that.data.lng);
		myPro.wxRequest("user/store/list","GET",data).then(res=>{
			let list = res.result;
			// 无数据
			if(that.data.fare_type == 0 && that.data.page == 1 && list.length == 0 ){
				that.setData({
					finished: true,
					showTips: true,
					tipsTitle: "抱歉,您的配送地址内暂无可用门店",
					tipsMessage: "可前往自取或更换地址,不便之处敬请见谅"
				});
				// 清掉所选地址
				wx.removeStorageSync('checkedAddress');
			}else if(list.length == 0){
				that.setData({
					finished: true
				});
				wx.showToast({
					title: "暂无更多数据",
					icon: "none"
				});
			}else{
				// 有数据
				for(let i in list){
					if(i == 0 && that.data.page == 1){
						list[i].is_tuijian = 1; // 推荐距离最近的店
					};
					if(that.data.store){
						if(that.data.store.id == list[i].id){
							list[i].is_selected = 1;
						}
					}else{
						// if(i == 0){
						// 	list[i].is_selected = 1;
						// }else{
						// 	list[i].is_selected = 0;
						// }
						list[i].is_selected = 0;
					}
				};
				that.setData({
					storeList: that.data.storeList.concat(list),
					page: that.data.page + 1
				});
			}
		}).catch(err=>{
			console.log('报错信息',err)
			wx.showToast({
				title: err,
				icon: "none",
			});
		});
	},
	// 主动切换门店
	onSelectStore(event){
		let that = this;
		let item = event.currentTarget.dataset.item;
		let list = that.data.storeList;
		let currentObj = null;
		// 判断点选店铺是否支持所选配送方式
		if(that.data.fare_type == 0){
			if(!item.is_delivery){
				that.setData({
					showTips: true,
					tipsTitle: "抱歉,此门店暂不支持外卖",
					tipsMessage: "可前往自取或更换地址,不便之处敬请见谅"
				})
				return;
			}
		}else{
			if(!item.is_ziti){
				that.setData({
					showTips: true,
					tipsTitle: "抱歉,此门店暂不支持自取",
					tipsMessage: "可更换门店,不便之处敬请见谅"
				})
				return;
			}
		};
		for(let i in list){
			if(list[i].id == item.id){
				list[i].is_selected = 1;
				currentObj = list[i];
			}else{
				list[i].is_selected = 0;
			}
		};
		that.setData({
			storeList: list,
			selectStore: currentObj
		});
		// 更新选中门店
		wx.setStorage({
			key: "store",
			data: JSON.stringify(that.data.selectStore)
		});
		// 判断是否切换门店,如有需清空购物车
		if(that.data.store && that.data.store.id != that.data.selectStore.id){
			wx.removeStorage({
				key: 'localCart',
			});
		};
		// 进入商品选购
		wx.navigateTo({
			url: "/pages/goods/index"
		});
	},
	// 取消授权提醒
	onCloseLocation(){
		let that = this;
		that.setData({
			showOpenLocation: false
		});
		// 当是外卖是,进首页
		if(that.data.fare_type == 0){
			wx.reLaunch({
				url: '/pages/index/index'
			})
		}else{
			// 这时只能调取到默认的
			getApp().globalData.lat = 0;
			getApp().globalData.lng = 0;
			that.setData({
				page: 1,
				storeList: [],
				finished: false,
				lat: getApp().globalData.lat,
				lng: getApp().globalData.lng
			});
			that.getStoreList();
		};
	},
	// 同意去打开地理位置
	onConfirmLocation(){
		let that = this;
		wx.openSetting({
			success: function (res) {
				console.log(res)
				let authSetting = res.authSetting;
				if(authSetting["scope.userLocation"]){
					// 开启
					getApp().getLocal(function(){
						that.setData({
							page: 1,
							storeList: [],
							finished: false
						});
						that.setData({
							lat: getApp().globalData.lat,
							lng: getApp().globalData.lng
						})
						that.getStoreList();
					});
				}else{
					// 未开启
					that.setData({
						showOpenLocation: true
					});
				}
			}
		})
	},
	// 关闭提醒
	closeShowTips(){
		let that = this;
		that.setData({
			showTips: false
		})
	}
});
