<template>
	<view class="content b-t">
		<!-- #ifdef H5 -->
		<empty src="../../static/error/emptyAddress.png" v-if="addressList.length === 0"></empty>
		<!-- #endif -->
		<!-- #ifndef H5 -->
		<empty src="../static/error/emptyAddress.png" v-if="addressList.length === 0"></empty>
		<!-- #endif -->
		<view class="list" v-for="(item, index) in addressList" :key="index" @click="checkAddress(item)">
			<view class="wrapper">
				<view class="address-box">
					<text class="name">{{ item.real_name }}</text>
					<text class="mobile">{{ item.phone }}</text>
				</view>
				<view class="u-box">
					<text class="address">{{ item.province + item.city + item.district }} {{ item.detail }}</text>
				</view>
			</view>
			<view class="buttom">
				<view class="default-buttom" @click.stop="defaultUp(item,index)">
					<image class="checkedImg" v-if="item.is_default == 1" src="../../static/icon/addressIconXz.png"
						mode="widthFix"></image>
					<view v-else class="checkbox">

					</view>
					<text class="text">设为默认地址</text>
				</view>
				<view class="operation">
					<view @click.stop="addAddress('edit', item)" class="flex">
						<image class="icon" src="../../static/icon/addressIcon1.png" mode="widthFix"></image>
						<text class="text">编辑</text>
					</view>
					<view class="blank"></view>
					<view @click.stop="delAddress(item)" class="flex">
						<image class="icon" src="../../static/icon/addressIcon2.png" mode="widthFix"></image>
						<text class="text">删除</text>
					</view>
				</view>
			</view>
		</view>
		<!-- #ifdef H5 || MP -->
		<view class="add-btn two base-buttom" @click="addWxAddress">获取微信地址</view>
		<!-- #endif -->
		<view class="add-btn one base-buttom" @click="addAddress('add')">新增地址</view>
	</view>
</template>

<script>
	import {
		getAddressList,
		setAddressDefault,
		addressDel,
		addressEdit
	} from '@/api/user.js';
	// #ifdef H5
	import weixinObj from "@/plugin/jweixin-module/index.js";
	import {
		weixindata
	} from "../../utils/wxAuthorized.js"
	import {
		prePage
	} from '@/utils/rocessor.js';
	// #endif
	export default {
		data() {
			return {
				source: 0,
				addressList: []
			};
		},
		onLoad(option) {
			this.source = option.source || 0
			this.loadAddress();
		},
		methods: {
			// 加载地址
			loadAddress() {
				getAddressList({
					page: 1,
					limit: 100
				}).then(({
					data
				}) => {
					this.addressList = data;
				});
			},
			// 设为默认地址
			defaultUp(data, ind) {
				this.addressList = this.addressList.map((e) => {
					e.is_default = 0
					return e
				})
				this.addressList[ind].is_default = 1
				setAddressDefault({
					id: data.id
				}).then(({
					data
				}) => {
					this.loadAddress();
				}).catch((e) => {
					console.log(e);
				});
			},
			//删除地址
			delAddress(item) {
				addressDel({
					id: item.id
				}).then(({
					data
				}) => {
					uni.showToast({
						title:"删除成功",
						icon:"none"
					})
				})
				let s = this.addressList.indexOf(item);
				this.addressList.splice(s, 1);
			},
			//选择地址
			checkAddress(item) {
				if (this.source == 1) {
					//prePage()获取上一页实例，在App.vue定义
					prePage().addressData = item;
					uni.navigateBack();
				}
			},
			// 添加地址
			addAddress(type, item) {
				uni.navigateTo({
					url: `/pages/set/addressManage?type=${type}&data=${JSON.stringify(item)}`
				});
			},
			//添加或修改成功之后回调
			refreshList() {
				// 重新加载地址
				this.loadAddress()
			},
			// #ifdef H5
			// 公众号获取地址
			addWxAddress() {
				let that = this;
				weixindata().then(() => {
					weixinObj.openAddress({
						success: function(data) {
							that.addAddressOn(data)
						},fail(e){
							console.log(e,'授权错误');
						}
					});
				})
			},
			// #endif
			// #ifdef MP
			addWxAddress() {
				let that = this;
				wx.chooseAddress({
					success: function(data) {
						console.log('获取地址',data);
						that.addAddressOn(data)
					},fail(e){
						console.log(e);
					}
				});
			},
			// #endif
			// #ifdef H5 || MP
			addAddressOn(data) {
				let that = this;
				addressEdit({
					real_name: data.userName,
					phone: data.telNumber,
					address: {
						province: data.provinceName,
						city: data.cityName,
						district: data.countyName
					},
					detail: data.detailInfo,
					type: 1
				}).then(function(e) {
					uni.showToast({
						title: '提交成功',
						duration: 2000
					});
					setTimeout(function() {
						that.loadAddress();
					}, 800);
				});
			
			}
			// #endif
		}
	};
</script>

<style lang="scss">
	page {
		padding-bottom: 120rpx;
		padding-top: 20rpx;
		background-color: $page-color-base;
	}

	.content {
		position: relative;
	}

	.list {
		align-items: center;
		padding: 20rpx 30rpx;
		background: #fff;
		margin: 20rpx;
		margin-top: 0;

		.buttom {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-top: 10rpx;

			.checkbox,
			.checkedImg {
				height: 36rpx;
				width: 36rpx;
				border-radius: 100rpx;
			}

			.checkbox {
				border: 1px solid $font-color-light;
			}

			.checkbox.checked {
				color: $base-color;
			}

			.default-buttom {
				display: flex;
				align-items: center;
			}

			.operation {
				display: flex;
				align-items: center;

				.blank {
					width: 30rpx;
				}

				.icon {
					width: 32rpx;
				}
			}

			.text {
				padding-left: 10rpx;
				font-size: 24rpx;
				color: #666666;
			}
		}
	}

	.wrapper {
		display: flex;
		flex-direction: column;
		flex: 1;
		border-bottom: 1px solid #f0f0f0;
		padding-bottom: 20rpx;
	}

	.address-box {
		display: flex;
		align-items: center;
		justify-content: space-between;

		.address {
			font-size: $font-base + 2rpx;
			color: $font-color-dark;
		}

		.mobile {
			font-size: $font-base;
			color: rgba(51, 51, 51, 1);
		}
	}

	.u-box {
		font-size: $font-base;
		color: $font-color-light;
		margin-top: 16rpx;

		.name {
			margin-right: 30rpx;
		}
	}

	.icon-bianji {
		display: flex;
		align-items: center;
		height: 80rpx;
		font-size: 40rpx;
		color: $font-color-light;
		padding-left: 30rpx;
	}

	.add-btn {
		position: fixed;
		z-index: 95;
		left: 30rpx;
		right: 30rpx;

		&.one {
			bottom: 16rpx;
		}

		&.two {
			bottom: 136rpx;
			background-color: $font-color-disabled;
		}
	}
</style>
