<template>
	<view class="bg">
		<view class="list-forms">
			<view class="list-item">
				<view class="list-item-title">姓名</view>
				<view class="list-item-input"><input type="text" v-model="username" placeholder="请输入姓名" /></view>
			</view>
			<view class="list-item">
				<view class="list-item-title">证件类型</view>
				<view class="list-item-input">
					<picker mode="selector" :range="idcardTypeList" range-key="title" @change="changeIdType">
						<input type="text" readonly style="font-size: 35rpx;position: relative;"
								v-model="idcardTypeValue" disabled="true" placeholder="请选择"/>
					</picker>
				</view>
			</view>
			<view class="list-item">
				<view class="list-item-title">证件号</view>
				<view class="list-item-input"><input type="text" v-model="idNumber" placeholder="请输入正确的证件号" /></view>
			</view>
			
			<view class="list-item">
				<view class="list-item-title">联系电话</view>
				<view class="list-item-input"><input type="number" v-model="mobile" placeholder="请输入手机号" maxlength="11" /></view>
			</view>
			<view class="list-item">
				<view class="list-item-title">设为默认</view>
				<view class="list-item-switch">
					<switch :checked="idDefault" @change="switchChange" color="#248BAA"/>
				</view>
			</view>
		</view>

		<!-- <view class="btn" @click="submit">保存</view> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				username: '',
				mobile: '',
				idNumber: '',
				idcardTypeValue: '身份证',
				show: false,
				idcardTypeList: [],
				document_type: 'DAM01001',
				id: '',
				type: '',
				idDefault: false,
			}
		},
		methods: {
			
			init (data) {
				
				this.username = ''
				this.mobile= ''
				this.idNumber= ''
				this.idcardTypeValue= '身份证'
				this.show= false
				this.idcardTypeList= []
				this.document_type= 'DAM01001'
				this.id= ''
				this.type=''
				this.idDefault= false
				
				this.type = 'add'
				this.getIdcardTypeList()
				if (data && data.id) {
					this.id = data.id
					this.type = 'edit'
					this.getDetail()
				}
			},
			switchChange(e){
				this.idDefault = e.detail.value
			},
			
			getDetail() {
				this.Post({
					id: this.id
				}, '/api/user/contactDetail').then(res => {
					if (res.code === 1) {
						res = res.data
						if (res && res.id > 0) {
							this.username = res.name
							this.mobile = res.tel
							this.idNumber = res.id_number
							this.idDefault = res.is_default == 1 ? true : false
							this.document_type = res.document_type
							this.idcardTypeValue = res.document_type_text
						}
					}
				})
			},
			//获取证件类型
			getIdcardTypeList() {
				this.Post({}, '/api/user/getIdcardTypeList').then(res => {
					this.idcardTypeList = res.data
				})
			},
			// 选择证件类型
			changeIdType(e) {
				this.idcardTypeValue = this.idcardTypeList[e.detail.value].title
				this.document_type = this.idcardTypeList[e.detail.value].document_type
			},
			// 提交
			async submit() {
				this.username = this.username.trim()
				this.mobile = this.mobile.trim()
				this.idNumber = this.idNumber.trim()
				if (this.username.length < 1) {
					uni.showToast({
						title: '请输入姓名',
						icon: 'none'
					})
					return
				}
				if (!this.IsTel(this.mobile)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return
				}
				if (this.idcardTypeValue == '身份证') {
					if (!this.idCardNumber(this.idNumber)) {
						uni.showToast({
							title: '请输入正确的身份证',
							icon: 'none'
						})
						return
					}
				} else if (this.idcardTypeValue == '护照') {
					if (!this.passportValid(this.idNumber)) {
						uni.showToast({
							title: '请输入正确的护照',
							icon: 'none'
						})
						return
					}
				} else if (this.idcardTypeValue == '台胞证') {
					if (!this.taiwanValid(this.idNumber)) {
						uni.showToast({
							title: '请输入正确的台胞证',
							icon: 'none'
						})
						return
					}
				} else if (this.idcardTypeValue == '港澳通行证') {
					if (!this.gangaoValid(this.idNumber)) {
						uni.showToast({
							title: '请输入正确的港澳通行证',
							icon: 'none'
						})
						return
					}
				} else if (this.idcardTypeValue == '外国人永久居留证') {
					if (!this.foreignerValid(this.idNumber)) {
						uni.showToast({
							title: '请输入正确的外国人永久居留证',
							icon: 'none'
						})
						return
					}
				} else if (this.idcardTypeValue == '军官证') {
					if (!this.officerValid(this.idNumber)) {
						uni.showToast({
							title: '请输入正确的军官证',
							icon: 'none'
						})
						return
					}
				}
				let res = await this.Post({
					id: this.type == 'edit'?this.id:'',
					id_number: this.idNumber,
					name: this.username,
					tel: this.mobile,
					document_type: this.document_type,
					is_default: this.idDefault ? '1' : '0',
				}, this.type=='edit'?'/api/user/editContact':'/api/user/addContact')
				if (res.code == 1) {
					uni.showModal({
						title: '提示',
						content: '操作成功',
						showCancel: false,
					})
				}else{
					uni.showModal({
						title: '提示',
						content: res.msg,
						showCancel: false,
					})
				}
				return {...res,data: {id: this.id}}
			}
		}
	}
</script>

<style scoped lang="scss">
	.bg {
		width: 100%;
		height: 100%;
		position: relative;
	}

	.list-forms {
		padding: 0 33rpx;

		.list-item {
			display: flex;
			border-bottom: 1rpx solid #D8D8D8;
			padding: 30rpx 0;
			height: 60rpx;
			box-sizing: content-box;
			align-items: center;

			.list-item-title {
				font-size: 34rpx;
				margin-right: 20rpx;
				width: 200rpx;
			}

			.list-item-input {
				flex: 1;

				input {
					width: 100%;
					height: 100%;
					padding: 0;
					margin: 0;
					border: 0;
					background-color: transparent;
					line-height: 34rpx;
					font-size: 34rpx;
					text-align: left;
				}

				input::placeholder {
					font-size: 26rpx;
				}
			}
		}
	}

	.btn {
		width: 697rpx;
		height: 80rpx;
		background: linear-gradient(90deg, #9EE4FE, #7FD491);
		border-radius: 40rpx;
		text-align: center;
		line-height: 80rpx;
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		position: absolute;
		bottom: 53rpx;
		left: 27rpx;
	}
</style>
