<template>
	<view class="listAll">
		<!-- <navbar pageName="地址管理"></navbar> -->
		<view class="list" v-if="list.length == 0">
			<view class="nodata">
				<image src="../../static/identify/logo.png" mode=""></image>
				<view>还没有录入地址呦~</view>
			</view>
			<!-- 			<ul v-else>
				<li v-for="(item,index) in list" :key="index" :class="item.type == 1? 'active' : ''" @click="goExhibits(item,item.id,index)">
					<view class="checked"></view>
					<view class="add" >
						<view class="address">
							{{item.address}}{{item.name}}{{item.atlasAdd}}
						</view>
						<view class="namePhone">{{item.userName}}  <span>{{item.phone}}</span></view>
					</view>
					<view class="edior">
						<view class="edit" @click.stop="edit(item.id)">编辑</view>
						<view class="del" @click.stop="del(item.id,item.type)">删除</view>
					</view>
				</li>
			</ul> -->
		</view>
		<view class="address_list" v-else>
			<view class="address" v-for="(item,index) in list" :key="index">
				<!-- <view class="checked"></view> -->
				<view class="add">
					<view class="data">
						{{item.address}} {{item.name}} {{item.atlasAdd}}
					</view>
					<view class="checked_box" @click="goExhibits(item,item.id,index)">
						<!-- <view class="checked" v-if="item.type == 0"></view> -->
						<view class="select" v-if="item.type == 0">
							<image src="../../static/identify/round.png" mode=""></image>
						</view>
						<view class="select" v-else>
							<image src="../../static/identify/select.png" mode=""></image>
						</view>
						默认地址
					</view>
					<!-- <view class="namePhone">{{item.userName}} <span>{{item.phone}}</span></view> -->
				</view>
				<view class="edior">
					<view class="left">
						{{item.userName}} <span>{{item.phone}}</span>
					</view>
					<view class="right">
						<image src="../../static/identify/edit.png" mode="" style="margin-right: 20rpx;" @click.stop="edit(item.id)"></image>
						<image src="../../static/identify/delete.png" mode="" @click.stop="del(item.id,item.type)"></image>
					</view>
				</view>
				<!-- <view class="edior">
					<view class="edit" @click.stop="edit(item.id)">编辑</view>
					<view class="del" @click.stop="del(item.id,item.type)">删除</view>
				</view> -->
			</view>
		</view>

		<view class="addbtn">
			<button type="primary" @click="addAddress">新增地址</button>
		</view>
	</view>
</template>

<script>
	import {
		addList,
		delAdd,
		defaultAdd,
		updateOrder,
		addDetails
	} from '../.././api/index.js'
	export default {
		data() {
			return {
				list: [],
				checkedIndex: 0,
				title: '',
				orderId: '',
				address: {},
				cName: '',
				Cphone: ''
			}
		},
		methods: {
			getList() {
				var that = this
				var data = {
					openId: uni.getStorageSync('wx_copenid'),
				}
				addList(data).then(res => {
					// res.data.data.forEach((element, index) => {
					// 	element.address = JSON.parse(element.address)
					// 	if (element.type == 1) {
					// 		this.checkedIndex = index,
					// 			this.address = JSON.stringify(element.address),
					// 			this.cName = element.userName,
					// 			this.Cphone = element.phone
					// 	}
					// });
					this.list = res.data.data
				}).catch(err => {})
			},
			//设为默认地址
			goExhibits(item, id, index) {
				this.checkedIndex = index
				var id = {
					id: id,
					openid: uni.getStorageSync('wx_copenid'),
				}
				defaultAdd(id).then(res => {
					if (this.title == '支付订单') {
						addDetails(id).then(res => {
							if (res.data.code == 200) {
								var data = {
									cId: this.orderId,
									name: res.data.data.name,
									address: res.data.data.address,
									latitude: res.data.data.latitude,
									longitude: res.data.data.longitude,
									atlasAdd: res.data.data.atlasAdd,
									cName: res.data.data.userName,
									cPhone: res.data.data.phone
								}
								updateOrder(data).then(res => {
									if (res.data.code == 200) {
										uni.navigateBack()
									} else {
										uni.showToast({
											title: res.data.msg,
											icon: 'none'
										})
									}
								})
								setTimeout(() => {
									// uni.navigateBack()
								}, 2000)
							}
						})
					} else {
						if (res.data.code == 200) {
							this.getList()
							uni.showToast({
								title: '设置默认地址成功！',
								icon: 'none'
							})

						}
					}

				})
			},
			//编辑地址
			edit(id,type) {
				uni.navigateTo({
					url: './add_address?id=' + id+ '&type=' + type
				})
			},
			//删除地址
			del(id, type) {
				if (type == 1) {
					uni.showModal({
						title: '提示',
						content: '默认地址不能删除',
						confirmText: '确定', // 确定按钮的文字
						cancelText: '取消', // 取消按钮的文字
					})
					return
				}
				var id = id
				let that = this
				uni.showModal({
					title: '提示',
					content: '确定删除该地址吗？',
					confirmText: '确定', // 确定按钮的文字
					cancelText: '取消', // 取消按钮的文字
					success(res) {
						if (res.confirm) {
							delAdd({
								idList: id
							}).then(res => {
								if (res.data.code == 200) {
									// this.$router.go(0)
									that.getList()
									uni.showToast({
										title: '删除成功',
										icon: 'success',
										duration: 1000
									})
								}
							})
						} else if (res.cancel) {

						}

					}
				})

			},
			//新增地址
			addAddress() {
				var type = 0
				if (this.list.length < 1) {
					type = 1
				} else {
					type = 0
				}
				uni.navigateTo({
					// url: '../../my/addAddress/addAddress?type=' +type,
					url: './add_address?type=' + type,
				})
			},
		},
		onLoad(options) {
			this.title = options.name
			this.orderId = options.orderNo
		},
		onShow() {
			this.getList()
		}
	}
</script>

<style lang="scss" scoped>
	.listAll {
		height: 100vh;
		background-color: #f8f8f8;
		padding: 32rpx;
		box-sizing: border-box;
	}

	.list {

		padding: 32rpx;

		// li.active {
		// 	.checked {
		// 		background-color: #2cb8d4;
		// 		box-shadow: inset 0 0 5rpx 10rpx #fff;
		// 	}
		// }

		.nodata {
			text-align: center;
			padding-top: 200rpx;
			color: #666;
			image {
				width: 400rpx;
				height: 400rpx;
				margin-bottom: 20rpx;
			}
		}
	}

	.address_list {
		width: 100%;

		.address {
			width: 100%;
			// height: 82rpx;
			background: #FFFFFF;
			box-shadow: 0px 2rpx 6rpx 0px rgba(88, 209, 187, 0.05);
			border-radius: 10rpx 10rpx 10rpx 10rpx;
			padding: 32rpx 24rpx;
			margin-bottom: 30rpx;
			box-sizing: border-box;
			.add {
				flex: 1;
				// padding-right: 30px;
				display: flex;
				align-items: start;
				justify-content: space-between;
				margin-bottom: 16rpx;
				.data {
					// width: 75%;
					flex: 1;
					font-size: 32rpx;
					color: #333;
					font-weight: bold;
					margin-bottom: 10rpx;
				}

				.checked_box {
					width: 128rpx;
					display: flex;
					align-items: center;
					font-size: 24rpx;
					.select{
						display: flex;
						align-items: center;
						image{
							width: 24rpx;
							height: 24rpx;
							margin-right:8rpx ;
						}
					}
					
				}
				.namePhone {
					font-size: 28rpx;

					span {
						color: #a4a6a8;
						margin-left: 15rpx;
					}
				}
			}

			.edior {
				display: flex;
				align-items: center;
				justify-content: space-between;
				.left{
				font-size: 26rpx;
					span{
						font-size: 26rpx;
						color: #999999;
						margin-left: 20rpx;
					}
				}
				.right{
					image{
						width: 20px;
						height: 20px;
					}
				}
			}
		}
	}

	.addbtn {
		position: fixed;
		bottom: 0;
		width: 100%;
		background-color: #fff;
		border-top: 1px solid #ccc;
		padding-top: 15px;
		padding-bottom: 30px;

		button {
			margin: 0 50px;
			background-color: #2cb8d4;
			border-radius: 50px;
		}
	}
</style>