<template>
	<view>
		<!--标题栏-->
		<bar-title bgColor="bg-white" isBack>
			<block slot="content">我的地址</block>
		</bar-title>
		
		<!--列表-->
		<view class="bg-white zaiui-address-card-view" v-for="item in addressList" :key="item.id">
			<view class="flex text-black text-lg" :data-item="item" @click="selectAddressTap">
				<view class="flex-sub text-left">{{item.name}}</view>
				<view class="flex-sub text-right">{{item.phone}}</view>
			</view>
			<view class="margin-tb-sm text-gray" :data-item="item" @click="selectAddressTap">
				{{item.area_string}}  {{item.address}}
			</view>
			<view class="zaiui-line-view"/>
			<view class="zaiui-right-view">
				<text>
					<checkbox class='round red sm zaiui-checked' style="transform: scale(0.7);" :class="item.type == 1?'checked':''"
					:checked="item.type == 1?true:false" :data-item="item" @click="setDefaultAddress" /><text>设为默认</text>
				</text>
				<text class="cuIcon-write" @click="editAddressTap(item)">编辑</text>
				<text class="cuIcon-delete" @click="deleteAddress(item)">删除</text>
			</view>
		</view>
		
		<!--按钮-->
		<view class="bg-white wecanui-footer-fixed foot-pb">
			<view class="flex flex-direction">
				<button class="cu-btn bg-red" @click="AddAddressTap">添加新地址</button>
			</view>
		</view>
		
	</view>
</template>

<script>
	import barTitle from '@/components/zaiui-common/basics/bar-title';
	import _tool from '@/static/zaiui/util/tools.js';	//工具函数
	export default {
		components: {
			barTitle
		},
		data() {
			return {
				addressList: [],
				fromOrder: false
			}
		},
		onLoad(options) {
			this.fromOrder = options.from==='cart'
		},
		onShow() {
			this.getList()
		},
		onReady() {
			_tool.setBarColor(true);
			uni.pageScrollTo({
			    scrollTop: 0,
			    duration: 0
			});
		},
		methods: {
			editAddressTap(item) {
				uni.navigateTo({
					url: '/pages/my/add-address?id='+item.id
				});
			},
			AddAddressTap() {
				uni.navigateTo({
					url: '/pages/my/add-address'
				});
			},
			selectAddressTap(e) {
				if(this.fromOrder) {
					let pages = getCurrentPages();
					if(pages[pages.length - 2] && pages[pages.length - 2].$vm.setAddress) {
						pages[pages.length - 2].$vm.setAddress(e.currentTarget.dataset.item);
						uni.navigateBack({
							delta: 1
						})
					}
				}
			},
			getList() {
				this.reqByuser({
					url: '?l=address.list', 
					// method: 'POST',
					data: {
					},
					success: res => {
						if(res.data.status == 1) {
							this.addressList = res.data.data;
						}else {
							uni.showToast({
								icon: 'error',
								title: '加载失败'
							})
						}
					}
				})
			},
			setDefaultAddress(e) {
				let {id, province_id, city_id, county_id, address, name, phone} = e.currentTarget.dataset.item;
				let data = {
						id, province: province_id, city: city_id, county: county_id, address, name, phone,
						type: 1
					};
					
				this.reqByuser({
					url: '?l=address.up',
					data,
					success: res => {
						if(res.data.status == 1) {
							uni.showToast({
								icon: 'success',
								title: '更新成功'
							})
							this.getList()
						}
					}
				})
			},
			deleteAddress(item) {
				uni.showModal({
					title: '提醒',
					content: '确定要删除吗？',
					success: (res) => {
						if(res.confirm) {
							this.reqByuser({
								url: '?l=address.delete',
								data: {
									id: item.id
								},
								success: res => {
									if(res.data.status == 1) {
										uni.showToast({
											icon: 'success',
											title: '删除成功'
										})
										this.getList()
									}
								}
							})
						}
					}
				})
				
			}
		}
	}
</script>

<style lang="scss">
	/* #ifdef APP-PLUS */
		@import "../../static/colorui/main.css";
		@import "../../static/colorui/icon.css";
		@import "../../static/zaiui/style/app.scss";
	/* #endif */
	.zaiui-address-card-view {
	    position: relative;
	    margin: 36.36upx 27.27upx;
		border-radius: 10.9upx;
		padding: 18.18upx;
		.zaiui-line-view {
		    position: relative;
		    background: #f9f9f9;
			margin-bottom: 20upx;
		    height: 2upx;
		}
		.zaiui-right-view {
			position: relative;
			text-align: right;
			text {
				&:before {
				    margin-right: 5px;
				}
			}
			text + text {
			    margin-left: 20px;	
			}
		}
	}
	.wecanui-footer-fixed .flex-direction {
		padding: 18.18upx;
	}
</style>
