<template>
	<view class="container">
		<view class="box">
			<view class="body">
				<!-- 姓名区域 -->
				<view class="body_box">
					<view class="body_box_key">姓名</view>
					<view class="body_box_value">
						<input class="input" type="text" placeholder="请输入姓名" v-model="name" />
					</view>
				</view>

				<!-- 身份证号区域 -->
				<view class="body_box">
					<view class="body_box_key">身份证号</view>
					<view class="body_box_value">
						<input class="input" type="text" placeholder="请输入身份证号" maxlength="18" v-model="identity" />
					</view>
				</view>

				<!-- 联系电话区域 -->
				<view class="body_box">
					<view class="body_box_key">联系电话</view>
					<view class="body_box_value">
						<input class="input" type="text" placeholder="请输入联系电话" maxlength="11" v-model="phone" />
					</view>
				</view>
			</view>

			<view class="btn" @click="handleSub">确定</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 姓名
			name: '',
			// 身份证号
			identity: '',
			// 电话
			phone: '',
			type: '',
			id: ''
		}
	},
	onLoad(options) {
		// console.log(options)
		this.type = options.type
		if (options.type === '2') {
			// 编辑状态
			const info = JSON.parse(options.info)
			this.name = info.user_name
			this.identity = info.card_number
			this.phone = info.user_phone
			this.id = info.id
			uni.setNavigationBarTitle({
				title: '编辑旅客'
			})
		}
	},
	methods: {
		// 确定按钮点击回调
		handleSub() {
			const reName = /^[\u4e00-\u9fa5]{2,4}$/
			const rePhone = /^1[3-9]\d{9}$/
			const reIdentity = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
			if (!this.name) {
				uni.showToast({
					title: '请输入姓名',
					icon: 'none',
					mask: true
				})
				return
			}
			if (!reName.test(this.name)) {
				uni.showToast({
					title: '姓名格式有误',
					icon: 'none'
				})
				return
			}
			if (!this.identity) {
				uni.showToast({
					title: '请输入身份证号',
					icon: 'none',
					mask: true
				})
				return
			}
			if (!reIdentity.test(this.identity)) {
				uni.showToast({
					title: '身份证号码格式有误',
					icon: 'none'
				})
				return
			}
			if (!this.phone) {
				uni.showToast({
					title: '请输入联系电话',
					icon: 'none',
					mask: true
				})
				return
			}
			if (!rePhone.test(this.phone)) {
				uni.showToast({
					title: '电话号码格式有误',
					icon: 'none'
				})
				return
			}
			if (this.type === '1') {
				// 添加
				this.add()
			} else {
				// 编辑
				this.edit()
			}
		},
		// 添加请求
		async add() {
			const res = await this.$myRequest({
				url: '/mhotel/ampAddContact.action',
				data: {
					userName: uni.getStorageSync('userInfo').user_name,
					contactUserName: this.name,
					contactUserPhone: this.phone,
					contactUserIdNum: this.identity,
					userId: uni.getStorageSync('userInfo').id
				}
			})
			// console.log(res)
			if (res.code === 200) {
				uni.showToast({
					title: '添加成功',
					icon: 'success',
					mask: true
				})
				setTimeout(() => {
					uni.navigateBack(1)
				}, 1500)
			}
		},
		// 编辑请求
		async edit() {
			const res = await this.$myRequest({
				url: '/mhotel/ampupdateContact.action',
				data: {
					contactUserName: this.name,
					contactUserPhone: this.phone,
					contactUserIdNum: this.identity,
					contactId: this.id
				}
			})
			// console.log(res)
			if (res.code === 200) {
				uni.showToast({
					title: '编辑成功',
					icon: 'success',
					mask: true
				})
				setTimeout(() => {
					uni.navigateBack(1)
				}, 1500)
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	display: flex;
	flex-direction: column;
	height: 100vh;
	background-color: #f2f2f2;

	.box {
		margin-top: 20rpx;
		height: calc(100vh - 20rpx);
		background-color: #fff;

		.body {
			box-sizing: border-box;
			padding: 0 20rpx;
			height: calc(100vh - 236rpx);
			background-color: #fff;

			.body_box {
				display: flex;
				align-items: center;
				margin-top: 30rpx;
				height: 80rpx;

				.body_box_key {
					width: 152rpx;
					color: #808080;
					font-size: 28rpx;
				}

				.body_box_value {
					display: flex;
					align-items: center;
					box-sizing: border-box;
					padding: 0 28rpx;
					width: 559rpx;
					height: 80rpx;
					font-size: 28rpx;
					border-radius: 15rpx;
					background-color: #f2f2f2;

					.input {
						width: 100%;
					}
				}
			}
		}

		.btn {
			display: flex;
			justify-content: center;
			align-items: center;
			margin: 45rpx auto;
			width: 710rpx;
			height: 96rpx;
			font-size: 32rpx;
			border-radius: 64rpx;
			color: #fff;
			background-color: #096562;
		}
	}
}
</style>
