// index.js
let myPro = require("../../../../utils/wxRequest.js");
let util = require("../../../../utils/util.js");
Page({
    data: {
		id: null, // 地址id 用于编辑信息
		name: '',
		sex: 1,
		phone: '',
		area_address: '',
		address: '',
		checked: false, // 是否默认
		loading: false // 全局loading
    },
    onLoad: function (options) {
        // Do some initialize when page load.
		let that = this;
		// 编辑地址id
		if(options.id){
			that.setData({
				id: options.id
			});
			that.getAddressInfo();
		};
		// 新增地址 来自微信
		if(options.type && options.type == 'wechat'){
			wx.getStorage({
				key: 'wechatAddress',
				success: function(res){
					let obj = JSON.parse(res.data);
					that.setData({
						name: obj.name,
						phone: obj.phone,
						address: obj.address
					});
					wx.removeStorageSync('wechatAddress');
				}
			})
		}
    },
    onShow: function () {
        // Do something when page show.
		let that = this;
    },
    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.
    },
    onPageScroll: function () {
        // Do something when page scroll
		
    },
    onResize: function () {
        // Do something when page resize
    },
	// 获取地址详情
	getAddressInfo(){
		let that = this;
		let params = {
			id: that.data.id
		};
		myPro.wxRequest("user/address/detail","GET",params).then(res=>{
			let info = res.result;
			that.setData({
				name: info.name,
				sex: info.sex,
				phone: info.phone,
				area_address: info.area_address,
				address_lat: info.lat,
				address_lng: info.lnt,
				address: info.address,
				checked: info.is_default ? true :false,
			});
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			})
		})
	},
	// 收货人性别
	onChangeName(event){
		let that = this;
		that.setData({
			name: event.detail
		});
	},
	// 选择性别
	onChangeSex(event){
		let that = this;
		that.setData({
			sex: event.detail
		});
	},
	// 获取输入的手机号
	onChangePhone(event){
		let that = this;
		that.setData({
			phone: event.detail
		});
	},
	// 获取输入的地址
	onChangeAddress(event){
		let that = this;
		that.setData({
			address: event.detail
		});
	},
	// 设置默认
	onChangeDefault(event){
		let that = this;
		that.setData({
			checked: event.detail
		});
	},
	// 选点
	toChooseLocation(){
		var that = this
		// 选点
		wx.chooseLocation({
			success: function(res){
				// console.log('成功',res)
				that.setData({
					area_address: res.address,
					address_lat: res.latitude,
					address_lng: res.longitude
				});
			},
			fail: function(res){
				console.log('失败',res)
			}
		})
	},
	// 提交
	saveAddress(){
		let that = this;
		if (that.data.name == null || that.data.name == '') {
			wx.showToast({
				title: '请输入联系人',
				icon: 'none'
			});
			return;
		};
		if (that.data.phone == null || that.data.phone == '' || !(/^1[3456789]\d{9}$/.test(that.data.phone))) {
			wx.showToast({
				title: '请检查手机号',
				icon: 'none'
			});
			return;
		};
		if ( !that.data.area_address) {
			wx.showToast({
				title: '请选择所在地区',
				icon: 'none'
			});
			return;
		};
		if (that.data.address == null || that.data.address == '') {
			wx.showToast({
				title: '请输入详细地址',
				icon: 'none'
			});
			return;
		};
		let params = {
			name: that.data.name,
			phone: that.data.phone,
			area_address: that.data.area_address,
			address: that.data.address,
			lnt: that.data.address_lng,
			lat: that.data.address_lat,
			is_default: that.data.checked ? 1 : 0  // int	是否默认（1是 0否）
		};
		// 性别
		if(that.data.sex){
			params.sex = that.data.sex;
		};
		// 编辑
		if(that.data.id){
			params.id = that.data.id;
			myPro.wxRequest("user/address/edit","POST",params).then(res=>{
				wx.showToast({
					title: res.msg,
					icon: 'none',
					success: function(){
						wx.navigateBack();
					}
				});
			}).catch(err=>{
				console.log('报错信息',err);
				wx.showToast({
					title: err,
					icon: 'none'
				});
			});
		}else{
			// 新增
			myPro.wxRequest("user/address/create","POST",params).then(res=>{
				wx.showToast({
					title: res.msg,
					icon: 'none',
					success: function(){
						wx.navigateBack();
					}
				});
			}).catch(err=>{
				console.log('报错信息',err);
				wx.showToast({
					title: err,
					icon: 'none'
				});
			});
		}
	},
});
