<template>
	<view>
		<!--pages/address/address.wxml-->
		<view v-for="(item, index) in pageList" :key="index" class="li dflex">
			<image :src="item.isDefault == 1? '/static/pages/images/gou_h.png' : '/static/pages/images/gou.png'"
				class="check" v-if="!query.isChoose"></image>
			<image
				:src="item.isDefault == 1 &&query.isChoose? '/static/pages/images/gou_h.png' : '/static/pages/images/gou.png'"
				class="check" @tap="chooseAddress" :data-index="index" v-else></image>
			<view class="info flex" :data-index="index" @tap="chooseAddress">
				<view class="name">{{item.name}}<text>{{item.phone}}</text></view>
				<view class="addr"><text v-if="item.isDefault == 1">默认</text>{{item.address}}{{item.house}}</view>
			</view>
			<navigator hover-class="none"
				:url="'/mine/editAddress/editAddress?id=' + item.id + '&addressId=' + query.addressId">
				<image src="/static/pages/images/bjico.png" class="edit"></image>
			</navigator>
		</view>
		<navigator class="add" hover-class="none" url="/mine/addAddress/addAddress">新增收货地址</navigator>
	</view>
</template>

<script>
	const req = require("../../utils/request.js");
	var QQMapWX = require("../../utils/qqmap.js");

	export default {
		data() {
			return {
				form: {
					page: 1,
					limit: 10
				},
				pageList: [],
				isLoad: true,
				query: {},
				merchant: {},
				address: ""
			};
		},

		components: {},
		props: {},
		onLoad: function(options) {
			if (options) this.query = options;
			this.setData({
				query: options
			}); // 实例化API核心类

			QQMapWX.initMap('3OJBZ-EQEKO-PFNWC-SHHEK-CGWAJ-KRBF7');
			let merchant = req.getStorage("MERCHANT");
			this.setData({
				merchant: merchant
			});
		},
		onShow: function() {
			this.isLoad = true;
			this.form.page = 1;
			this.getList();
		},

		onReachBottom() {
			this.form.page++;
			this.getList();
		},

		methods: {
			getList() {
				if (!this.isLoad) return false;
				this.isLoad = false;
				let that = this;
				req.getRequest('/api/address/list', this.form, data => {
					if (data.list && data.list.length == 10) this.isLoad = true;
					if (that.form.page > 1) data.list = that.pageList.concat(data.list);
					that.setData({
						pageList: data.list
					});
				});
			},

			chooseAddress(event) {
				const index = event.currentTarget.dataset.index;
				const page = this.pageList[index];
				let that = this;

				if (this.query.isChoose) {
					//选择地址
					if (!page.lat || !page.lng) return req.msg('地址未解析，请重新修改');
					// console.log(that.merchant.isDistance);

					if (that.merchant.isDistance === 1 && that.query.orderType === 5) {
						let merchant = that.merchant;
						// console.log(merchant); // QQMapWX.geocoder(merchant.address,success=>{
						//   console.log(success)
						//   merchant.lat=success.location.lat
						//   merchant.lng=success.location.lng
						//   QQMapWX.calculate({ latitude: merchant.lat, longitude: merchant.lng }, [{ latitude: page.lat, longitude: page.lng }], merchant.distance, res => {
						//     console.log(res)
						//   })
						// })

						QQMapWX.calculate({
							latitude: merchant.lat,
							longitude: merchant.lng
						}, [{
							latitude: page.lat,
							longitude: page.lng
						}], merchant.distance, res => {
							// console.log(res);
						});
					} else {
						let pages = getCurrentPages();
						// console.log(page)
						var prevPage = pages[pages.length - 2];
						prevPage.$vm.setData({
							address: page
						});
						uni.navigateBack();
					}
				} else {
					//设置默认地址
					req.postRequest('/api/address/set/default', {
						id: page.id
					}, () => {
						let def = that.pageList.findIndex(it => it.isDefault == 1);
						let d = {};

						if (def > -1) {
							d['pageList[' + def + '].isDefault'] = 0;
						}

						d['pageList[' + index + '].isDefault'] = 1;
						that.setData(d);
					});
				}
			},

			deleteAddress(event) {
				const index = event.currentTarget.dataset.index;
				const page = this.pageList[index];
				let that = this;
				req.msgConfirm('确定删除该地址', () => {
					req.postRequest('/api/address/delete', {
						id: page.id
					}, () => {
						that.pageList.splice(index, 1);
						that.setData({
							pageList: that.pageList
						});
					});
				});
			}

		}
	};
</script>
<style>
	@import "./address.css";
</style>
