<template>
	<view class="container">
		<Navbar :title="title" canBack></Navbar>
		<view class="">
			<addAddress ref="addAds" mode="add" v-if="dataReady" :formData="formData" @setInput="getAds"></addAddress>
		</view>
		<view class="bomBarCss">
			<view class="btnCss">
				<u-button color="#ED2920" shape="circle" @click="savaAds">
					<text class="text-lg">确定</text>
				</u-button>
			</view>
		</view>
		<!-- <u-toast ref="uToast"></u-toast> -->
	</view>
</template>

<script>
	import addAddress from '@/components/addAddress/addAddress.vue'
	export default {
		components: {
			addAddress
		},
		data() {
			return {
				id: "",
				formData: {},
				dataReady: false,
				type: null,
				qpid: "",
				title:"新增地址"
			};
		},
		mounted() {

		},
		onLoad(data) {
			console.log(data);
			if (data.id) {
				this.ifEdit = true;
				this.id = data.id;
				this.title = "编辑地址";
				console.log("编辑", this.id);
				this.getAdsxx()
			} else {
				this.dataReady = true;
			}
			if (data.qpid) {
				this.qpid = data.qpid
			}
		},
		methods: {
			// 获取编辑信息
			async getAdsxx() {
				let query = {
					id: this.id
				}
				const result = await this.$api.memberApi.getwHydzDetail(query);
				let {
					code,
					value
				} = result;
				if (code === 200) {
					this.formData = value;
					this.dataReady = true;
				}
				console.log('查询地址信息', result);
			},
			// 组件传参
			getAds(ads) {
				console.log("组件传递地址",ads);
				const {
					area,
					...formData
				} = ads;
				this.formData = formData;
				console.log('获取地址', JSON.parse(JSON.stringify(this.formData)));
			},
			async savaAds() {
				let that = this;
				// this.$modal.showLoading('获取当前定位中');
				// await this.getJwd();
				console.log('新建地址详情', JSON.parse(JSON.stringify(this.formData)));
				this.$refs.addAds.$refs.uForm.validate().then(async res => {
					// uni.$u.toast('校验通过')
					console.log('打印this.formData', this.formData);
					const result = await that.$api.memberApi.savewHydz(this.formData);
					console.log("添加返回", result);
					let {
						code,
						value,
						message
					} = result;
					if (code === 200) {
						console.log("添加成功", result);
						let adsId = result.value;
						uni.$u.toast(message);
						setTimeout(() => {
							if (that.qpid) {
								// console.log("首次添加地址");
								uni.redirectTo({
									url: `/pages/home/placeOrder?qpid=${that.qpid}&adsId=${adsId}`
								})
							} else {
								that.$goBack();
							}
						}, 500)
					}
				}).catch(errors => {
					console.log("添加地址eroros",errors);
					// uni.$u.toast('校验失败')
				})
			},
			getJwd() {
				let that = this;
				return new Promise((resolve, reject) => {
					uni.getLocation({
						type: 'wgs84',
						success: (res) => {
							that.formData.fJd = res.longitude;
							that.formData.fWd = res.latitude;
							resolve(); // 成功获取经纬度后 resolve
						},
						fail: (error) => {
							reject(error); // 获取经纬度失败时 reject
						},
						complete() {
							that.$modal.closeLoading();
						}
					});
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.bomBarCss {
		width: 100%;
		height: 50px;
		background: #fff;
		position: absolute;
		bottom: 0px;
		left: 0;
		right: 0;

		.btnCss {
			width: 60%;
			margin: 5px auto;
		}
	}
</style>
