<template>
	<view class="cotainer">
		<view class="list">
			<view class="list-item" v-for="item in list">
				<view class="list-item-title u-border-bottom">
					<view class="list-item-name">
						<text>{{item.contact}}</text>
						<text>{{item.mobile}}</text>
					</view>
					<view class="list-item-address">
						{{item.area}}{{item.address}}
					</view>
				</view>
				<view class="list-item-btm">
					<view>
						<u-checkbox @change="e=>{return checkboxChange(item)}" active-color="#ba8b45"
							v-model="item.defalut" :name="item.defalutName">
							{{item.defalutName}}
						</u-checkbox>
					</view>
					<view class="list-item-btm-btn">
						<text @click="delAddress(item.id)">删除</text>
						<text @click="toAdd('copy',item)">复制</text>
						<text @click="toAdd('editor',item)">修改</text>
					</view>
				</view>
			</view>
			<view class="list-add" @click="toAdd">
				<view>新增地址</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		toast
	} from '@/common/util.js'
	export default {
		data() {
			return {
				list: [],
				form: {
					name: '',
					mobile: '',
					area: '',
					address: '',

				}

			}
		},
		onShow() {
			this.getAddress()

		},
		onLoad() {},
		methods: {
			async getAddress() {
				const res = await this.$u.api.getUserAddress()
				res.address.map(item => {
					item.defalutName = item.defalut ? '已设为默认地址' : '设为默认地址'
				})
				console.log("res.address", res.address)
				this.list = res.address
			},
			delAddress(id) {
				let that = this
				uni.showModal({
					title: '确认',
					content: '是否删除该地址',
					success: function(res) {
						if (res.confirm) {
							that.$u.api.delUserAddress({
								id: id
							}).then(res => {
								toast('删除成功')
								that.getAddress()
							})
						} else if (res.cancel) {
							item.defalut = false
							console.log('用户点击取消');
						}
					}
				})
				
			},
			copyAddress(){
				
			},
			toAdd(type,item) {
				var itemtype = type?type:'add'
				if(type){
					uni.setStorageSync('vuex_address',item)
				}
				uni.navigateTo({
					url: '/packages/my/addaddress?type='+itemtype
				})
			},
			checkboxChange(item) {
				let that = this
				console.log("e", item.defalut)
				if (!item.defalut) {
					uni.showModal({
						title: '确认',
						content: '是否将该地址设为默认地址',
						success: function(res) {
							if (res.confirm) {
								that.changeDefalut(item)
							} else if (res.cancel) {
								item.defalut = false
								console.log('用户点击取消');
							}
						}
					})
				} else {
					uni.showModal({
						title: '确认',
						content: '是否取消该地址为默认地址',
						success: function(res) {
							if (res.confirm) {
								that.changeDefalut(item)
							} else if (res.cancel) {
								console.log('用户点击取消');
								item.defalut = true
							}
						}
					})
				}
			},
			changeDefalut(item) {
				let that = this
				item.defalut = item.defalut ? 1 : 0
				this.$u.api.saveUserAddress(item).then(res => {
					toast('设置成功', 1000, function() {
						that.getAddress()
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	.cotainer {
		background-color: #f0efed;
		height: 100vh;
		overflow: scroll;
		
		::v-deep .u-btn-text {
			width: 200rpx;
			text-align: center;
		}

		.list {
			padding: 30rpx 50rpx;
			padding-bottom: 200rpx;
			&-item {
				padding: 30rpx 25rpx;
				background-color: #fff;
				border-radius: 10rpx;
				margin-bottom: 30rpx;

				&-name {
					color: #000;
					font-size: 30rpx;
					margin-bottom: 20rpx;

					text {
						margin-right: 20rpx
					}
				}

				&-address {
					color: #000;
					font-size: 24rpx;
					padding-bottom: 20rpx;
					line-height: 1.5;
				}

				&-btm {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-top: 20rpx;
					font-size: 22rpx;
					color: #adadac;

					.u-checkbox__label {
						font-size: 22rpx;
					}

					&-btn {
						text {
							padding: 0rpx 20rpx;
							border-radius: 20rpx;
							text-align: center;
							border: 1px solid #adadac;
							margin-left: 20rpx;
							color: #000;
						}
					}

				}
			}

			&-add {
				background: #fff;
				position: fixed;
				bottom: 0;
				left: 0;
				width: 100%;
				padding: 40rpx;
				z-index: 9;
			
				>view {
					background-color: #ba8b45;
					color: #fff;
					border-radius: 40rpx;
					width: 60%;
					margin: 0 auto;
					height: 75rpx;
					text-align: center;
					line-height: 75rpx;
				}
			}
		}
	}
</style>