// index.js
let myPro = require("../../../../utils/wxRequest.js");
let util = require("../../../../utils/util.js");
Page({
    data: {
		showConfirmAdd: false, // 确认地址弹层
		page: 1,
		size: 10,
		dataList: [],
		finished: false, // 加载完毕
		loading: false, // 全局loading
		checkedAddress: null, // 选中地址
		showDelDialog: false, // 删除确认框
		is_back: null // 当用户主动切换地址时,支持回退
    },
    onLoad: function (options) {
        // Do some initialize when page load.
		let that = this;
		// 路由回退标识
		if(options.is_back){
			that.setData({
				is_back: options.is_back
			});
		};
    },
    onShow: function () {
        // Do something when page show.
		let that = this;
		that.setData({
			page: 1,
			size: 10,
			dataList: [],
			finished: false, 
		});
		that.getData();
    },
    onReady: function () {
        // Do something when page ready.
    },
    onHide: function () {
        // Do something when page hide.
    },
    onUnload: function () {
        // Do something when page close.
    },
    onPullDownRefresh: function () {
        // Do something when pull down.
		
    },
    onReachBottom: function () {
        // Do something when page reach bottom.
		let that = this;
		if(!that.data.finished){
			that.getData();
		};
    },
    onPageScroll: function () {
        // Do something when page scroll
		
    },
    onResize: function () {
        // Do something when page resize
    },
	// 获取地址列表
	getData(){
		let that = this;
		let params = {
			page: that.data.page,
			size: that.data.size
		};
		myPro.wxRequest("user/address/list","GET",params).then(res=>{
			let list = res.result;
			// 无数据
			if(list.length == 0){
				wx.showToast({
					title: "暂无更多数据",
					icon: "none"
				});
				that.setData({
					finished: true
				});
			}else{
				// 有数据
				that.setData({
					dataList: that.data.dataList.concat(list),
					page: that.data.page + 1
				});
			}
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			});
		})
	},
	// 去详情
	goAddressDetail(event){
		let that = this;
		let url = '';
		if(event.currentTarget.dataset.item){
			let item = event.currentTarget.dataset.item;
			url = '/pages/user/address/detail/index?id='+item.id
		}else{
			url = '/pages/user/address/detail/index'
		}
		wx.navigateTo({
			url: url
		});
	},
	// 获取微信地址
	getAddressFromWechat(){
		let that = this;
		wx.chooseAddress({
			success: function(res){
				console.log('获取成功',res)
				let obj = {
					name: res.userName,
					phone: res.telNumber,
					area: res.provinceName+res.cityName+res.countyName,
					address: res.detailInfo
				};
				// 让用户主动选一遍位置
				wx.navigateTo({
					url: '/pages/user/address/detail/index?type=wechat'
				});
				// 存一下
				wx.setStorage({
					key: "wechatAddress",
					data: JSON.stringify(obj)
				});
			},
			fail: function(res){
				console.log('获取失败',res)
			}
		})
	},
	// 选择地址
	selecteAddress(event){
		let that = this;
		// 先把上一次的值置空(避免出问题)
		that.setData({
			checkedAddress: null
		});
		// 重新赋值
		let checkedAddress = event.currentTarget.dataset.item;
		that.setData({
			showConfirmAdd: true,
			checkedAddress: checkedAddress
		});
	},
	// 更换地址
	cancelCheck(){
		let that = this;
		that.setData({
			showConfirmAdd: false,
		});
	},
	// 确认地址
	confirmCheck(){
		let that = this;
		that.setData({
			showConfirmAdd: false
		});
		// 拿地址,匹配下门店,如果未返回,就是超距离了
		let params = {
			lat: that.data.checkedAddress.lat,
			lng: that.data.checkedAddress.lnt,
			page: 1,
			size: 10
		};
		myPro.wxRequest("user/store/list","GET",params).then(res=>{
			let storeList = res.result;
			if(storeList.length != 0){
				wx.setStorage({
					key: "checkedAddress",
					data: JSON.stringify(that.data.checkedAddress),
					success: function(){
						if(that.data.is_back){
							wx.navigateBack();
						}else{
							// 去购商品
							wx.redirectTo({
								url: "/pages/goods/index"
							});
						}
					}
				});
			}else{
				// 超配送距离
				wx.showToast({
					title: "门店尚未营业", // 单店改为门店尚未营业  多店已超配送距离,请更换地址
					icon: "none"
				});
			};
		}).catch(err=>{
			console.log('报错信息',err)
			wx.showToast({
				title: err,
				icon: "none",
			});
		});
	},
	// 打开删除确认框
	doDeleteAddress(event){
		let that = this;
		that.setData({
			id: event.currentTarget.dataset.id,
			showDelDialog: true
		});
	},
	// 取消删除地址
	onCloseDel(){
		let that = this;
		that.setData({
			showDelDialog: false
		});
	},
	// 确认删除
	onConfirmDel: function(){
		let that = this;
		let params = {
			id: that.data.id
		};
		myPro.wxRequest("user/address/remove","POST",params).then(res=>{
			wx.showToast({
				title: res.msg,
				icon: "none"
			});
			that.setData({
				showDelDialog: false,
				page: 1,
				dataList: [],
				finished: false
			});
			that.getData();
		}).catch(err=>{
			that.setData({
				showDelDialog: false
			});
			console.log("报错信息",err);
			wx.showToast({
				title: err,
				icon: "none"
			});
		})
	},
});
