<template>
	<view class="bg">
		<view class="cancat-nav flex-around">
			<view :class="['concat-nav-item',showType===0?'active':'']" @click="showType=0">
				<view>出行人信息</view>
				<view v-if="showType===0" class="active-bar"></view>
			</view>
			<view :class="['concat-nav-item',showType===1?'active':'']" @click="showType=1">
				<view>收货地址</view>
				<view v-if="showType===1" class="active-bar"></view>
			</view>
		</view>
		
		<!-- 出行人信息 -->
		<view v-if="showType===0">
			<view class="item" v-for="(item,index) in travelList" :key="index">
				<view class="name">
					<view class="">
						{{item.name}}
					</view>
					<view class="">
						{{item.tel}}
					</view>
				</view>
				<view class="idcard">
					<view class="">
						{{item.document_type_text}}:
					</view>
					<view class="">
						{{item.id_number}}
					</view>
				</view>
				<view class="item-btn">
					<view class="choice">
						<image src="https://static.ticket.sz-trip.com/yandu/images/user/dui.png" mode="aspectFill"
							v-if="item.is_default==1">
						</image>
						<view class="yuan" v-else @click.stop="defaultC(item)">
			
						</view>
						<view class="">
							默认出行人
						</view>
					</view>
					<view class="btn-list">
						<view class="btn-item" @click.stop="edit(item.id)">
							修改
						</view>
						<view class="btn-item" @click.stop="delet(item)">
							删除
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 收货地址 -->
		<view v-else>
			<view class="item" v-for="(item,index) in addressList" :key="index">
				<view class="name">
					<view>
						{{item.name}}
					</view>
					<view class="">
						{{item.tel}}
						<span v-if="item.is_default==1">默认</span>
					</view>
				</view>
				<view class="idcard">
					<view class="">
						收货地址:
					</view>
					<view class="text-overflow">
						{{item.address}}
					</view>
				</view>
				<view class="item-btn" style="justify-content: flex-end;">
					<view class="btn-list">
						<view class="btn-item" @click.stop="edit(item.id)">
							修改
						</view>
						<view class="btn-item" @click.stop="delet(item)">
							删除
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<view class="btn-box">
			<navigator :url="showType ? '/subPackages/user/myAddressAdd' : '/subPackages/user/myContactsAdd'" class="btn">
				{{showType ? '添加联系人' : '添加出行人'}}
			</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		name: "travelerList",
		data() {
			return {
				showType: 0,
				travelList: [],
				addressList: []
			};
		},
		onShow() {
			this.init()
		},
		methods: {
			init () {
				// 出行人信息
				this.Post({}, "/api/user/contactList").then(res => {
					if (res) this.travelList = res.data
				})
				
				// 收货地址
				this.Post({}, '/api/user/consigneeList').then(res => {
					if(res) this.addressList = res.data
				})
			},
			delet(item) {
				let that = this
				uni.showModal({
					title: '提示',
					content: '确定要删除该出行人吗?',
					success: function(res) {
						if (res.confirm) {
							that.Post({
								id: item.id
							}, "/api/user/delDetail").then(res => {
								if (res) {
									uni.showToast({
										icon: "none",
										title: res.msg
									})
								}
								that.getList()
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			defaultC(item) {
				this.Post({
					id: item.id,
					is_default: 1
				}, "/api/user/editContact").then(res => {
					if (res.code == 1) {
						this.travelList.forEach(i => i.is_default = 0)
						item.is_default = !item.is_default
					}
				})
			},
			// 修改出行人信息
			edit(id){
				let url = ''
				if(this.showType) {
					url = '/subPackages/user/myAddressAdd?id='+id
				}else {
					url = "/subPackages/user/myContactsAdd?type=edit&id="+id
				}
				uni.navigateTo({
					url: url
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.bg {
		position: relative;
		background: #F7F7F7;
		height: 100vh;
		overflow-x: hidden;
		padding-bottom: 200rpx;
	}
	
	.cancat-nav{
		width: 100%;
		display: flex;
		height: 112rpx;
		flex-shrink: 0;
		.concat-nav-item{
			ffont-weight: 500;
			font-size: 35rpx;
			color: #000000;
			text-align: center;
		}
		.concat-nav-item.active{
			color: #000000;
		}
		.active-bar{
			width: 100%;
			height: 11rpx;
			background: #71B580;
		}
	}
	
	.item {
		width: 697rpx;
		background: #FFFFFF;
		border-radius: 13rpx;
		margin: 0 auto;
		margin-bottom: 28rpx;
		padding: 26rpx;
		
		.name,
		.idcard {
			display: flex;
			font-size: 31rpx;
			font-weight: bold;
			color: #333333;
		
		}
		
		.name {
			view:last-child {
				margin-left: 40rpx;
			}
			
			span {
				padding: 3rpx 9rpx;
				background: #71B580;
				border-radius: 7rpx;
				font-weight: bold;
				font-size: 24rpx;
				color: #FFFFFF;
				margin-left: 15rpx;
			}
		}
		
		.idcard {
			margin-top: 26rpx;
		
			view:last-child {
				margin-left: 14rpx;
				font-weight: 400;
				max-width: 492rpx;
		
			}
		}
		
		.item-btn {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 40rpx;
		
			.choice {
				display: flex;
				align-items: center;
		
				view:last-child {
					margin-left: 20rpx;
		
					font-size: 27rpx;
		
					font-weight: 500;
					color: #333333;
				}
		
				image {
					width: 40rpx;
					height: 40rpx;
				}
				
				.yuan {
					width: 40rpx;
					height: 40rpx;
					background: rgba(255, 200, 37, 0);
					border: 1rpx solid #999999;
					border-radius: 20rpx;
				}
			}
		
			.btn-list {
				display: flex;
				align-items: center;
		
				view {
					width: 134rpx;
					height: 54rpx;
					background: #FFFFFF;
					border: 1px solid #999999;
					border-radius: 27rpx;
		
		
					font-size: 27rpx;
					font-weight: 500;
					color: #333333;
					line-height: 52rpx;
					text-align: center;
				}
		
				view:last-child {
					margin-left: 14rpx;
				}
			}
		}
	}
	
	.btn-box {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 200rpx;
		background-color: #fff;
		
		.btn {
			width: 697rpx;
			line-height: 80rpx;
			background: linear-gradient(90deg, #9EE4FE, #7FD491);
			border-radius: 37rpx;
			font-weight: 500;
			font-size: 36rpx;
			color: #000000;
			text-align: center;
			position: fixed;
			bottom: 53rpx;
			left: 26.67rpx;
		}
	}
</style>
