<template>
	<view class="container">
		<navbar :config="config" backColor="#666666"></navbar>
		<view class="list">
			<radio-group>
			<view class="item u-bg-fff" v-for="(item,index) in addressList" :key="item.id">
				<view class="u-border-one-one userInfo" @click="backSurePay(item.id)">
					<view>收货人：<text class="u-999">{{item.name}}</text> <text class="u-ml80">{{item.phone}}</text> </view>
					<view class="u-mt5 u-text2">收货地址：<text class="u-999">{{item.province_name}}{{item.city_name}}{{item.area_name}}{{item.street_name}}{{item.community_name}}{{item.address}}</text></view>
				</view>
				<view class="operation u-flex-center-sb u-font24 u-999">
					<view class="u-flex-center" @click="isDefault(item.id)">
						<view class="kuang">
						<!-- <radio color="#FF0000" style="transform:scale(0.7)" :checked="item.is_default" /> -->
						<image v-if="item.is_default" :src="imgUrl+'/checked.png'" mode="widthFix" style="width: 30rpx;height: 30rpx;margin-right: 10rpx;"></image>
						<image v-else :src="imgUrl+'/nocheck.png'" mode="widthFix" style="width: 50rpx;height: 50rpx;"></image>
						<!-- <checkbox color="#FF0000" style="transform:scale(0.7)" :checked="item.is_default" /> -->
						<!-- <radio v-else style="transform:scale(0.7)" :checked="false" /> -->
						{{item.is_default?'默认地址':'设为默认'}}
						</view>
					</view>
					<view class="u-flex-center">
						<view class="u-mr45" @click="edit(item.id)">编辑</view>
						<view @click="del(item.id)">删除</view>
					</view>
				</view>
			</view>
			</radio-group>
		</view>
		<!-- 没有数据 -->
		<view class="nogoods u-mt30 u-flex-column-center" v-if="addressList.length==0">
			<nodata  :config="{top:5,content:'您还没有添加地址~'}"></nodata>
		</view>
		<view class="add-btn" @click="goAddEditAddress">
			<view class="u-plr30">
				<button class="u-btn-two u-FFF">添加新地址</button>
			</view>
			
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				config: {
					back: true, //false是tolbar页面 是则不写
					title: '收货地址',
					color: '#1A1A1A',
					//背景颜色;参数一：透明度（0-1）;参数二：背景颜色（array则为线性渐变，string为单色背景）
					backgroundColor: [1, "#fff"],
					statusBarFontColor: '#1A1A1A'
				},
				addressList:[],
				imgUrl: this.$mConfig.staticUrl
			}
		},
		onLoad() {
			
			
		},
		onShow() {
			this.$nextTick(() => {
				this.getAddressList()
			})
			
		},
		methods:{
			//获取地址列表
			async getAddressList(){
				let that = this
				this.$http.get('/useraddress/list',{}).then(async res => {
					console.log('res',res)
					if(res.code ==200){
						that.addressList =  res.list
					}
				})
			},
			listAddress(e){
				if(e==1){
					this.getAddressList()
				}
			},
			//跳转确认下单页面
			backSurePay(id){
				// uni.setStorageSync('address',item)
				let object={
					addressid:id
				}
				var pages = getCurrentPages();
				var prevPage = pages[pages.length - 2]; 
				if(prevPage.$vm.otherFun){
					prevPage.$vm.otherFun(object)
				}
				uni.navigateBack({
					delta:1
				})
				// uni.navigateTo({
				// 	url:'/pages/product/surePay/surePay?addressid='+id
				// })
			},
 			//跳转到添加地址
			goAddEditAddress(){
				uni.navigateTo({
					url:"addEditAddress"
				})
			},
			isDefault(id){
				let that = this
				this.$http.put('/useraddress/set-def/'+id).then(async res => {
					if(res&&res.code==200){
						uni.showToast({
							title:"修改成功",
							icon:"none",
							duration:3000,
							success() {
								that.getAddressList()
								that.addressList =[]
							}
						})
					}
				})
				// if(e.is_default == true){
				// 	e.is_default = false
				// 	that.update(e)
				// }else{
				// 	e.is_default =true
				// 	that.update(e)
				// }
				
			},
			update(param){
				
			},
			//编辑
			edit(id){
				uni.navigateTo({
					url:"addEditAddress?id="+id
				})
			},
			//删除
			async del(id){
				let that = this
				uni.showModal({
					title:'温馨提示',
					content:"是否删除这条地址",
					success:(res) =>{
						if(res.confirm) {  
							 this.$http.delete('/useraddress/delete/'+id,)
								.then(async res => {
									if(res.code==200){
										uni.showToast({
											title:"删除成功",
											icon:"none",
											duration:3000,
											success() {
												that.getAddressList()
											}
										})
									}
								})
						}
					}
				})

			},
		}
		}
</script>

<style lang="scss">
	page{
		background-color: #F5F5F5;
	}
	.kuang {
		display: flex;
		align-items: center;
	}
	.list{
		margin-bottom: 160rpx;
	}
	.item{
		margin-top: 30rpx;
		padding:46rpx 30rpx;
		.userInfo{
			padding-bottom: 36rpx;
		}
		.operation{
			border-top: 2rpx solid #F5F5F5;
			padding-top: 42rpx;
		}
	}
	.add-btn{
		width: 100%;
		position: fixed;
		bottom: 45rpx;
		button{
			width: 100%;
		}
	}
</style>
