<template>
	<view class="cotainer">
		<view class="list">
			<u-swipe-action :show="item.show" :index="index" v-for="(item, index) in list" :key="item.id" 
			@click="clickDel"
			@close = "closeDel"
			@open="openDel"
			@content-click="clickItem"
			:options="options">
				<view class="list-item u-border-bottom">
					<view class="list-item-title">
						<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>
			</u-swipe-action>
			<view class="list-add" @click="toAdd">
				<u-icon name="plus-circle" color="#007aff"></u-icon>
				新增地址
			</view>
		</view>
	</view>
</template>

<script>
	import {toast} from '@/common/util.js'
	export default {
		data() {
			return {
				list: [],
				firstDel:false,
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#dd524d',
						width:'200rpx'
					}
				}],
				showinfo:false,
				form:{
					name:'',
					mobile:'',
					area:'',
					address:'',
					
				}
				
			}
		},
		onShow(){
			this.getAddress()
			
		},
		onLoad() {
		},
		methods: {
			clickDel(index, optionIndex) {
				if(!this.firstDel){
					this.options[optionIndex].text = "删除该地址"
					this.firstDel = true
					return
				}else{
					let id = this.list[index].id
					
					this.$u.api.delUserAddress({
						id:id
					}).then(res=>{
						toast('删除成功')
						this.getAddress()
					})
				}
				
			},
			openDel(index){
				this.list.map((item,index1)=>{
					item.show = false
					if(index===index1){
						item.show = true
					}
					console.log("item",item.show)
				})
			},
			
			closeDel(index){
				this.list[index].show = false
				this.options[0].text = "删除"
				this.firstDel = false
			},
			clickItem(index){
				if(this.list[index].show){
					this.list[index].show = false
					this.options[0].text = "删除"
					this.firstDel = false
				}else{
					this.list.map(item=>{
						item.show = false
					})
					this.options[0].text = "删除"
					this.firstDel = false
					uni.setStorageSync("vuex_address",this.list[index])
					uni.navigateTo({
						url:"/packages/my/addaddress"
					})
				}
			},
			async getAddress(){
				const res = await this.$u.api.getUserAddress()
				res.address.map(item=>{
					item.show = false
				})
				this.list = res.address
			},
			toAdd(){
				uni.navigateTo({
					url:'/packages/my/addaddress'
				})
			},
		}
	}
</script>

<style lang="scss">
	.cotainer{
		::v-deep .u-btn-text{
			width: 200rpx;
			text-align: center;
		}
		.list{
			padding:30rpx;
			padding-bottom:80rpx;
			&-item{
				padding:20rpx 0;
				&-name{
					color:#333;
					text{
						margin-right:20rpx
					}
				}
				&-address{
					color:#ccc;
				}
			}
			&-add{
				color:#2979ff;
				margin: 20rpx 0;
				font-size: 32rpx;
				u-icon{
					margin-right:20rpx
				}
			}
		}
	}
</style>