<template>
	<view class="container">
		<view class="page">
			<view class="housing">
				<view class="steps">
					<view class="item active">
						<view class="uni-icons"><uni-icons type="home-filled" size="20" color="#6A59F2"></uni-icons>
						</view>
						<view class="name">房屋信息</view>
					</view>
					<view class="dotted dottedActive"></view>
					<view class="item active">
						<view class="uni-icons"><uni-icons type="person-filled" size="20" color="#6A59F2"></uni-icons>
						</view>
						<view class="name">住户信息</view>
					</view>
					<view class="dotted"></view>
					<view class="item">
						<view class="uni-icons"><uni-icons type="auth-filled" size="20" color="#B5B8ED"></uni-icons>
						</view>
						<view class="name">物业审核</view>
					</view>
					<view class="dotted"></view>
					<view class="item">
						<view class="uni-icons"><uni-icons type="checkbox-filled" size="20" color="#B5B8ED"></uni-icons>
						</view>
						<view class="name">认证成功</view>
					</view>
				</view>
				<view class="form">
					<uni-forms>
						<view class="form-item">
							<view class="form-item-fl">
								<view class="asterisk">*</view>与业主关系
							</view>
							<view class="form-item-fr">
								<picker style="width:100%;text-align:right;" mode="selector" @change="e=>bindPickerChange(e, 'relationShip')" range-key="label" :value="relationShipIndex" :range="relationShipList" class="form-item-tit">
									<view class="uni-input">{{relationShipList[relationShipIndex].label}}</view>
								</picker>
								<uni-icons type="forward" color="#E4E4EA"></uni-icons>
							</view>
						</view>
						<view class="form-item">
							<view class="form-item-fl">
								<view class="asterisk">*</view>住户名称
							</view>
							<view class="form-item-fr">
								<input class="form-item-tit" v-model="formData.residentName" focus placeholder="请输入名称" />
							</view>
						</view>
						<view class="form-item">
							<view class="form-item-fl">
								<view class="asterisk">*</view>住户电话
							</view>
							<view class="form-item-fr">
								<input class="form-item-tit" v-model="formData.residentPhone" focus placeholder="请输入电话" />
							</view>
						</view>
						<!-- <view class="form-item">
							<view class="form-item-fl">
								<view class="asterisk">*</view>业主名称
							</view>
							<view class="form-item-fr">
								<input class="form-item-tit" readonly v-model="formData.ownerName" focus placeholder="请输入名称" />
							</view>
						</view> -->
						<!-- <view class="form-item">
							<view class="form-item-fl">
								<view class="asterisk">*</view>业主电话
							</view>
							<view class="form-item-fr">
								<view class="form-item-tit" v-if="relationShipIndex === 0">{{formData.ownerPhone}}</view>
								<input class="form-item-tit" v-else v-model="formData.ownerPhone" focus placeholder="请输入名称" />
							</view>
						</view> -->
						<view class="form-item">
							<view class="form-item-fl">
								<view class="asterisk"></view>证件类型
							</view>
							<view class="form-item-fr">
								<picker style="width:100%;text-align:right;" @change="e=>bindPickerChange(e, 'certificate')" range-key="label" :value="certificateIndex" :range="certificateList" class="form-item-tit">
									<view class="uni-input">{{certificateList[certificateIndex].label}}</view>
								</picker>
								<uni-icons type="forward" color="#E4E4EA"></uni-icons>
							</view>
						</view>
						<view class="form-item">
							<view class="form-item-fl">
								<view class="asterisk"></view>证件号码
							</view>
							<view class="form-item-fr">
								<input class="form-item-tit" v-model="formData.certificateNo" placeholder="请输入证件号码" />
							</view>
						</view>
						<view class="form-item">
							<view class="form-item-fl">
								<view class="asterisk"></view>性别
							</view>
							<view class="form-item-fr">
								<uni-data-checkbox v-model="formData.sex" :localdata="sexs" class="form-item-tit" />
							</view>
						</view>
						<view class="form-item">
							<view class="form-item-fl">
								<view class="asterisk"></view>年龄
							</view>
							<view class="form-item-fr">
								<input class="form-item-tit" v-model.number="formData.age" placeholder="请输入年龄" />
							</view>
						</view>
					</uni-forms>
				</view>

				<button class="form-submit" :loading="isSubmit" :disabled="isSubmit" @click="toAudit()">提交审核</button>
			</view>
		</view>
	</view>
</template>

<script>
import {
	postcommunityOwnerAddApi,
	postcommunityOwnerEditApi
} from '@/config/api.js'
import {isMoble, isValidIDCard, isValidPassport, isValidPermit} from '@/utils/util'
import {relationShipList} from '@/utils/dictionary'
export default {
	data() {
		return {
			formData: {
				// ownerName: '',
				// ownerPhone: '',
				relationShip: '',
				residentName: '',
				residentPhone: '',
				sex: 1,
				certificateType: '',
				certificateNo: '',
				age: ''
			},
			sexs: [
				{ text: '男', value: 1 },
				{ text: '女', value: 2 }
			],
			relationShipList: relationShipList,
			relationShipIndex: 0,
			certificateList: [
				{label: '身份证', value: '1'},
				{label: '港澳通行证', value: '2'},
				{label: '护照', value: '3'}
			],
			certificateIndex: 0,
			isSubmit: false
		}
	},
	methods: {
		bindPickerChange(e, type) {
			if(type === 'certificate') {
				this.certificateIndex = Number(e.detail.value)
			} else {
				this.relationShipIndex = Number(e.detail.value)
				// if(this.relationShipIndex === 0) {
				// 	this.formData.ownerPhone = uni.getStorageSync('user_phone')
				// } else {
				// 	this.formData.ownerPhone = ''
				// 	this.formData.residentName = ''
				// }
			}
		},
		toAudit() {
			if(!this.formData.residentName) {
				uni.showToast({
					title: '请输入住户名称',
					icon: 'none'
				})
				return
			}
			if(!this.formData.residentPhone) {
				uni.showToast({
					title: '请输入住户电话',
					icon: 'none'
				})
				return
			}
			if(!isMoble(this.formData.residentPhone)) {
				uni.showToast({
					title: '请输入正确的住户电话',
					icon: 'none'
				})
				return
			}
			if(this.formData.certificateNo) {
				if(this.certificateIndex === 0 && !isValidIDCard(this.formData.certificateNo)) {
					uni.showToast({
						title: '请输入正确的身份证',
						icon: 'none'
					})
					return
				}
				if(this.certificateIndex === 1 && !isValidPermit(this.formData.certificateNo)) {
					uni.showToast({
						title: '请输入正确的港澳通行证',
						icon: 'none'
					})
					return
				}
				if(this.certificateIndex === 2 && !isValidPassport(this.formData.certificateNo)) {
					uni.showToast({
						title: '请输入正确的护照',
						icon: 'none'
					})
					return
				}
			}
			this.isSubmit = true
			this.formData.relationShip = this.relationShipList[this.relationShipIndex].value
			this.formData.certificateType = this.certificateList[this.certificateIndex].value
			this.formData['auditStatus'] = 'waitAudit'
			let data = getApp().globalData.registerInfo
			if(data.id) {
				postcommunityOwnerEditApi({ ...data, ...this.formData}).then(res=> {
					if(res.code === 200) {
						getApp().globalData.registerInfo = { ...data, ...this.formData}
						setTimeout(()=> {
							uni.showToast({
								icon: "none",
								title: res.message
							})
							setTimeout(()=> {
								this.isSubmit = false
								uni.navigateTo({
									url: '/pages/housingAudit/index',
								});
							}, 1500)
						}, 30)
					}
				}).catch(err=> {
					this.isSubmit = false
				})
			} else {
				postcommunityOwnerAddApi({ ...data, ...this.formData}).then(res=> {
					if(res.code === 200) {
						getApp().globalData.registerInfo = { ...data, ...this.formData}
						uni.setStorageSync('user_type', '1')
						setTimeout(()=> {
							uni.showToast({
								icon: "none",
								title: res.message
							})
							setTimeout(()=> {
								this.isSubmit = false
								uni.navigateTo({
									url: '/pages/housingAudit/index',
								});
							}, 1500)
						}, 30)
					}
				}).catch(err=> {
					this.isSubmit = false
				})
			}
		}
	},
	onLoad() {
		let registerInfo = getApp().globalData.registerInfo
		if(registerInfo.id) {
			this.formData.residentName = registerInfo.residentName
			this.formData.residentPhone = registerInfo.residentPhone
			this.formData.sex = registerInfo.sex
			this.formData.age = registerInfo.age

			this.relationShipIndex = this.relationShipList.findIndex(item=>item.value === registerInfo.relationShip)
			this.certificateIndex = this.certificateList.findIndex(item=>item.value === registerInfo.certificateType)
		} else {
			// this.formData.ownerPhone = this.formData.residentPhone = uni.getStorageSync('user_phone')
		}
	}
}
</script>
<style>
	page {
		background-color: #F8F6F9;
	}
</style>
<style lang="scss" scoped>
	.page {
		padding: 30rpx;
	}

	.housing {
		.steps {
			display: flex;
			justify-content: space-between;
			background-color: #ffffff;
			border-radius: 16rpx;
			padding: 30rpx;
			margin-bottom: 30rpx;

			.item {
				display: flex;
				flex-wrap: wrap;
				justify-content: center;

				.uni-icons {
					text-align: center;
					width: 60rpx;
					height: 60rpx;
					border-radius: 60rpx;
					border: 1px solid #B5B8ED;
					line-height: 60rpx;
				}

				.name {
					margin-top: 10rpx;
					font-size: 28rpx;
					color: #9D9CA6;
				}

			}

			.active {
				.uni-icons {
					border: 1px solid #6A59F2;
				}

				.name {
					color: #373737;
				}
			}

			.dotted {
				margin-top: 28rpx;
				width: 40px;
				height: 1px;
				border-bottom: 1px dashed #B5B8ED;
			}

			.dottedActive {
				border-bottom: 1px solid #6A59F2;
			}
		}

		.form {
			background-color: #ffffff;
			border-radius: 16rpx;
			padding: 30rpx;
			margin-bottom: 30rpx;

			.form-item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 100rpx;
				line-height: 100rpx;
				border-bottom: 1px solid #F8F6F9;
				overflow: hidden;
			}

			.form-item-fl {
				font-size: 30rpx;
				color: #373737;
				display: flex;

				.asterisk {
					color: #F42E2E;
					width: 10rpx;
					margin-right: 6rpx;
				}
			}

			.form-item-fr {
				display: flex;
				flex: 2;
				align-items: center;
				justify-content: flex-end;
				margin-left: 20rpx;
			}

			.form-item-tit {
				font-size: 30rpx;
				color: #666;
				margin-right: 20rpx;
				text-align: right;
			}
		}
	}

	.form-submit {
		height: 80rpx;
		line-height: 80rpx;
		background-color: #6A59F2;
		border-radius: 80rpx;
		width: 100%;
		text-align: center;
		font-size: 28rpx;
		color: #FFFFFF;
	}
</style>