<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}}
						<span v-if="item.is_default==1">默认</span>
					</view>
				</view>
				<view class="idcard">
					<view class="">
						{{item.title}}:
					</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 class="flex-between" v-if="showType>0" style="width: 660rpx;margin: 0 auto;">
				<navigator :url="'/subPackages/user/myAddressAdd'" class="btn" style="width: 300rpx;">添加收货地址</navigator>
				<view class="btn" @click="getWXAddress()"
				style="width: 300rpx;display: flex;align-items: center;justify-content: center;">
					<image class="wx-icon" src="https://static.ticket.sz-trip.com/uploads/20250916/3402d54588c3f0470bf05ac754f76cf0.png"></image>
					<text style="padding-left: 8rpx;">获取微信地址</text>
				</view>
			</view>
			
			<navigator v-else :url="'/subPackages/user/myContactsAdd'" class="btn">添加联系人</navigator>
			
		</view>
		<agreeAdressVue ref="agreeAdressRef" @submitAgreeAdress="submitAgreeAdress"></agreeAdressVue>
	</view>
</template>

<script>
	import agreeAdressVue from '../../components/agreeAdress.vue';
	export default {
		name: "travelerList",
		components: {agreeAdressVue},
		data() {
			return {
				showType: 0,
				travelList: [],
				addressList: []
			};
		},
		onLoad(option) {
			this.showType = option.showType || 0
			console.log(this.showType)
		},
		onShow() {
			this.init()
		},
		methods: {
			init () {
				// 出行人信息
				this.Post({contactType: 'CONTACT',offset: 0,limit: 100}, "/api/uservice/user/getContactOrConsignee").then(res => {
					if (res) this.travelList = res.data
				})
				
				// 收货地址
				this.Post({contactType: 'CONSIGNEE',offset: 0,limit: 100}, '/api/uservice/user/getContactOrConsignee').then(res => {
					if(res) this.addressList = res.data
				})
			},
			delet(item) {
				let that = this
				uni.showModal({
					title: '提示',
					content: '确定要删除该出行人吗?',
					success: function(res) {
						if (res.confirm) {
							if(that.showType == 0) {
								that.Post({
									contactId: item.id
								}, "/api/uservice/user/delContact").then(res => {
									if (res) {
										uni.showToast({
											icon: "none",
											title: res.msg
										})
									}
									that.init()
								})
							}else {
								that.Post({
									id: item.id
								}, "/api/uservice/user/delConsignee").then(res => {
									if (res) {
										uni.showToast({
											icon: "none",
											title: res.msg
										})
									}
									that.init()
								})
							}
						} 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
				});
			},
			
			submitAgreeAdress (val) {
				if (val) {
					this.getWXAddress()
				}
			},
			// 获取微信地址
			getWXAddress () {
				let agress = uni.getStorageSync("agreeAddress")
				if (!agress) {
					this.$refs.agreeAdressRef.openPop()
					return
				}
				
				let _this = this;
				uni.chooseAddress({
					success: (res) => {
						_this.Post({...res,method:"POST"},'/api/uservice/user/addWxDefaultConsignee').then(()=>{
							_this.init()
						})
					},
					fail: (e) => {
						console.log(e)
					}
				})
			},
			
		}
	}
</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;
		background-color: #fff;
		.concat-nav-item{
			ffont-weight: 500;
			font-size: 35rpx;
			color: #000000;
			height: 100%;
			text-align: center;
			position: relative;
			padding-top: 35rpx;
		}
		.concat-nav-item.active{
			color: #000000;
		}
		.active-bar{
			width: 73rpx;
			height: 7rpx;
			background: #515150;
			border-radius: 3rpx;
			margin: auto;
			position: absolute;
			bottom: 0;
			left: 0;
			right: 0;
		}
	}
	
	.item {
		width: 697rpx;
		background: #FFFFFF;
		border-radius: 13rpx;
		margin: 28rpx auto 0;
		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: #515150;
				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: 153rpx;
		background-color: #fff;
		box-shadow: 0rpx 0rpx 13rpx 0rpx rgba(82,82,82,0.25);
		display: flex;
		align-items: center;
		justify-content: center;
		
		.btn {
			width: 697rpx;
			height: 73rpx;
			line-height: 73rpx;
			background: #6A8A2D;
			border-radius: 11rpx;
			font-weight: bold;
			font-size: 32rpx;
			color: #FFFFFF;
			text-align: center;
		}
	}
	.wx-icon{
		width: 30rpx;
		height: 30rpx;
	}
</style>
