<template>
	<view class="user_address_list_page">
		<view class="page_content" style="padding-bottom: calc(env(safe-area-inset-bottom) + 80rpx + 60rpx);">
			<view v-if="list.length">
				<view class="margin-top-15 padding-lr-30 bg-ff" v-for="(item,index) in list" :key="index">
					<view class="padding-tb-25 border-bottom-ed" @click="choose(index)">
						<view>
							<text class="text-30">{{item.linkman}}</text>
							<text class="margin-left-30 text-30">{{item.mobile_phone}}</text>
						</view>
						<view class="margin-top-10 text-28 text-80">
							<view class="">
								<view class="margin-right-10 inline-block" v-if="item.is_default == 2">
									<ty-text-label text="默认" radius="4"></ty-text-label>
								</view>
								{{item.province}}{{item.city}}{{item.area}}{{item.address}}
							</view>
						</view>
					</view>

					<view class="height-100 flex align-center justify-between">
						<view @click="setDefault(index)" class="flex align-center">
							<text
								:class="item.is_default == 0 ? 'tyIcon-quan text-34 text-cc' : 'tyIcon-xuanzhong1 text-40 text-main'"></text>
							<text class="margin-left-15 text-26"
								:class="item.is_default == 0 ? 'text-98' : ''">{{item.is_default == 0 ? '设为默认' : '默认地址'}}</text>
						</view>

						<view class="flex">
							<view @click="go('pages/user/address/edit?id='+item.id)">
								<text class="tyIcon-bianji text-34 text-98"></text>
								<text class="margin-left-10 text-26 text-98">编辑</text>
							</view>
							<view class="margin-left-50" @click="del(index)">
								<text class="tyIcon-shanchu11 text-34 text-98"></text>
								<text class="margin-left-10 text-26 text-98">删除</text>
							</view>
						</view>
					</view>
				</view>
				

				<view class="btn width-100p padding-lr-30" @click="go('pages/user/address/edit')">
					<view class="line-height-80 radius-40 bg-main text-center text-28 text-ff">新建收货地址</view>
				</view>

			</view>

			<view v-else>
				<ty-text-no-data text="哎呀 , 你还没有收货地址哦" btn-text="新增收货地址" top="280" image="/static/user/no_address.png"
					@btnClick="go('pages/user/address/edit')"></ty-text-no-data>
			</view>


		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				type: 1, //1--地址列表进入 2--选择地址进入
				list: []
			}
		},

		onLoad(options) {
			this.type = options.type || 1;
		},

		onShow() {
			this.loadList()
		},

		methods: {
			//加载列表
			loadList() {
				this.rq.getData('base/user/api/Address/getAddressList').then(res => {
					if (res.code == 0) {
						this.list = res.data.list
					}
				})
			},

			// 设置默认
			setDefault(index) {
				if (this.list[index].is_default == 0) {
					this.rq.getData('base/user/api/Address/setDefault', {
						address_id: this.list[index].id
					}).then(res => {
						if (res.code == 0) {
							this.cn.toast(res.msg);
							this.loadList(1);
						}
					})
				} else if (this.list[index].is_default == 1) {
					this.rq.getData('base/user/api/Address/cancelDefault', {
						address_id: this.list[index].id
					}).then(res => {
						if (res.code == 0) {
							this.cn.toast(res.msg);
							this.loadList(1);
						}
					})
				}

			},

			// 删除
			del(index) {
				this.cn.confirm('是否确认删除该条地址').then(res => {
					if (res.confirm) {
						this.rq.getData('base/user/api/Address/delAddress', {
							address_id: this.list[index].id
						}).then(res => {
							if (res.code == 0) {
								this.cn.toast(res.msg)
								this.loadList(1)
							}
						})
					}
				})
			},

			// 选择地址
			choose(index) {
				if (this.type == 2) {
					getApp().globalData.order.myAddressId = this.list[index].id
					uni.navigateBack({
						delta: 1
					});
				}
			}
		}
	}
</script>
<style scoped>
	.btn {
		position: fixed;
		left: 0;
		bottom: calc(30rpx + env(safe-area-inset-bottom));
	}
</style>
