<template>
	<view class="container">
		<view class="header">注：请按照自己的真实情况填写</view>

		<view class="form">
			<view class="form_box">
				<view class="form_key">手机号码：</view>

				<view class="form_value">
					<input class="input" type="text" placeholder="请输入手机号码" placeholder-style="color:#ccc;font-size:28rpx" v-model="studentInfo.phone" />
				</view>
			</view>

			<view class="form_box">
				<view class="form_key">籍贯：</view>

				<view class="form_select">
					<!-- 省 -->
					<picker @change="bindPickerChange" :value="currtIndex" :range="provinceList" range-key="name">
						<view class="select_box" :class="{ active: currtIndex != null }">
							<view class="box_text">
								{{ currtIndex != null ? provinceList[currtIndex].name : '请选择省' }}
							</view>

							<uni-icons type="down" size="16" color="#ccc"></uni-icons>
						</view>
					</picker>
					<!-- 市 -->
					<picker @change="bindPickerChange_city" :value="currtIndex_city" :range="cityList" range-key="name" :disabled="!provinceId">
						<view class="select_box" :class="{ active: currtIndex_city != null }">
							<view class="box_text">
								{{ currtIndex_city != null ? cityList[currtIndex_city].name : '请选择市' }}
							</view>
							<uni-icons type="down" size="16" color="#ccc"></uni-icons>
						</view>
					</picker>
					<!-- 区 -->
					<picker @change="bindPickerChange_district" :value="currtIndex_district" :range="districtList" range-key="name" :disabled="!cityId">
						<view class="select_box" :class="{ active: currtIndex_district != null }">
							<view class="box_text">
								{{ currtIndex_district != null ? districtList[currtIndex_district].name : '请选择区' }}
							</view>
							<uni-icons type="down" size="16" color="#ccc"></uni-icons>
						</view>
					</picker>
				</view>
			</view>

			<view class="form_box">
				<view class="form_key">家庭住址：</view>

				<view class="form_select">
					<!-- 省 -->
					<picker @change="bindPickerChange2" :value="currtIndex2" :range="provinceList2" range-key="name">
						<view class="select_box" :class="{ active: currtIndex2 != null }">
							<view class="box_text">{{ currtIndex2 != null ? provinceList2[currtIndex2].name : '请选择省' }}</view>
							<uni-icons type="down" size="16" color="#ccc"></uni-icons>
						</view>
					</picker>
					<!-- 市 -->
					<picker @change="bindPickerChange_city2" :value="currtIndex_city2" :range="cityList2" range-key="name" :disabled="!provinceId2">
						<view class="select_box" :class="{ active: currtIndex_city2 != null }">
							<view class="box_text">
								{{ currtIndex_city2 != null ? cityList2[currtIndex_city2].name : '请选择市' }}
							</view>

							<uni-icons type="down" size="16" color="#ccc"></uni-icons>
						</view>
					</picker>
					<!-- 区 -->
					<picker @change="bindPickerChange_district2" :value="currtIndex_district2" :range="districtList2" range-key="name" :disabled="!cityId2">
						<view class="select_box" :class="{ active: currtIndex_district2 != null }">
							<view class="box_text">{{ currtIndex_district2 != null ? districtList2[currtIndex_district2].name : '请选择区' }}</view>

							<uni-icons type="down" size="16" color="#ccc"></uni-icons>
						</view>
					</picker>
				</view>
			</view>

			<view class="form_box">
				<view class="form_key"></view>

				<view class="form_value">
					<input class="input" type="text" placeholder="请输入家庭住址" placeholder-style="color:#ccc;font-size:28rpx" v-model="studentInfo.address" />
				</view>
			</view>

			<view class="form_box">
				<view class="form_key">邮编：</view>

				<view class="form_value">
					<input class="input" type="text" placeholder="请输入邮编" placeholder-style="color:#ccc;font-size:28rpx" v-model="studentInfo.zipCode" />
				</view>
			</view>
		</view>

		<!-- 家庭成员区域 -->
		<view class="family">
			<view class="family_top">
				<uni-icons type="staff" size="30"></uni-icons>
				<view class="top_title">家庭成员</view>
				<uni-icons style="margin-left: auto" type="personadd" size="26" color="#0061FF" @click="handleAdd"></uni-icons>
			</view>

			<view class="family_table" v-if="fvs.length">
				<view class="table_box top">
					<view class="title">称谓</view>
					<view class="name">姓名</view>
					<view class="work">工作单位</view>
					<view class="phone">联系方式</view>
				</view>

				<view class="table_box" v-for="(item, index) in fvs" :key="index">
					<view class="title">{{ item.familyShip }}</view>
					<view class="name">{{ item.name }}</view>
					<view class="work">{{ item.workUnit }}</view>
					<view class="phone">{{ item.phone }}</view>
				</view>
			</view>
		</view>

		<!-- 温馨提示区域 -->
		<view class="tips">
			<view class="tips_box">
				<view class="box_title">温馨提示：</view>
				<view class="box_text">{{ info }}</view>
			</view>
		</view>

		<!-- 按钮区域 -->
		<view class="btns">
			<view class="btn up" @click="handleUp">上一步</view>
			<view class="btn down" @click="handleNext">下一步</view>
		</view>

		<!-- 添加家庭成员弹窗区域 -->
		<uni-popup ref="popup" :is-mask-click="false">
			<view class="pop">
				<image class="pop_img" src="/static/3.png" mode="aspectFill"></image>
				<!-- 关闭图标 -->
				<uni-icons class="pop_close" type="closeempty" color="#808080" size="20" @click="handleClose"></uni-icons>

				<view class="pop_box">
					<view class="box_key">称谓：</view>
					<view class="box_value">
						<input class="input" type="text" placeholder="请输入称谓" placeholder-style="color:#CCCCCC;font-size:28rpx;" v-model="popObj.familyShip" />
					</view>
				</view>

				<view class="pop_box">
					<view class="box_key">姓名：</view>
					<view class="box_value">
						<input class="input" type="text" placeholder="请输入姓名" placeholder-style="color:#CCCCCC;font-size:28rpx;" v-model="popObj.name" />
					</view>
				</view>

				<view class="pop_box">
					<view class="box_key">工作单位：</view>
					<view class="box_value">
						<input class="input" type="text" placeholder="请输入工作单位" placeholder-style="color:#CCCCCC;font-size:28rpx;" v-model="popObj.workUnit" />
					</view>
				</view>

				<view class="pop_box">
					<view class="box_key">联系方式：</view>
					<view class="box_value">
						<input class="input" type="text" placeholder="请输入联系方式" placeholder-style="color:#CCCCCC;font-size:28rpx;" v-model="popObj.phone" />
					</view>
				</view>

				<view class="pop_btn" @click="handleConfirm">确定</view>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import { getProvinceGroupReq, getCityGroupReq, getDistrictGroupReq } from '@/api/index.js'

// 学生信息
const studentInfo = ref({})

// 家庭成员数据
const fvs = ref([])

// 弹窗DOM
const popup = ref()

// 省数组
const provinceList = ref([])
const currtIndex = ref(null)
const provinceId = ref()

// 市数组
const cityList = ref([])
const currtIndex_city = ref(null)
const cityId = ref()

// 区数组
const districtList = ref([])
const currtIndex_district = ref(null)
const districtId = ref()

// 省数组
const provinceList2 = ref([])
const currtIndex2 = ref(null)
const provinceId2 = ref()

// 市数组
const cityList2 = ref([])
const currtIndex_city2 = ref(null)
const cityId2 = ref()

// 区数组
const districtList2 = ref([])
const currtIndex_district2 = ref(null)
const districtId2 = ref()

// 弹窗对象数据
const popObj = ref({
	familyShip: '',
	name: '',
	workUnit: '',
	phone: ''
})

const info = ref()

onLoad(() => {
	studentInfo.value = uni.getStorageSync('studentInfo') || {}
	info.value = uni.getStorageSync('settingInfo').familyInfo || ''
	fvs.value = studentInfo.value.fvs || []
	// console.log(studentInfo.value)
	// 获取省分组
	getProvinceGroup()
})

// 获取省分组
const getProvinceGroup = async () => {
	const res = await getProvinceGroupReq()
	// console.log(res)
	if (res.code == 200) {
		provinceList.value = res.data
		provinceList2.value = res.data

		if (studentInfo.value.oprovinceId) {
			provinceList.value.forEach((ele, index) => {
				if (ele.id == studentInfo.value.oprovinceId) {
					currtIndex.value = index
				}
			})

			provinceId.value = studentInfo.value.oprovinceId
			getCityGroup()
		}

		if (studentInfo.value.provinceId) {
			provinceList2.value.forEach((ele, index) => {
				if (ele.id == studentInfo.value.provinceId) {
					currtIndex2.value = index
				}
			})
			provinceId2.value = studentInfo.value.provinceId
			getCityGroup2()
		}
	}
}

// 选择省回调
const bindPickerChange = (e) => {
	cityId.value = null
	cityList.value = []
	currtIndex_city.value = null
	districtId.value = null
	districtList.value = []
	currtIndex_district.value = null

	currtIndex.value = e.detail.value
	provinceId.value = provinceList.value[currtIndex.value].id

	studentInfo.value.oprovinceId = provinceId.value
	studentInfo.value.oprovince = provinceList.value[currtIndex.value].name

	// 获取市分组
	getCityGroup()
}

// 获取市分组
const getCityGroup = async () => {
	const res = await getCityGroupReq({
		provinceId: provinceId.value
	})
	// console.log(res)
	if (res.code == 200) {
		cityList.value = res.data

		if (studentInfo.value.ocityId) {
			cityList.value.forEach((ele, index) => {
				if (ele.id == studentInfo.value.ocityId) [(currtIndex_city.value = index)]
			})

			cityId.value = studentInfo.value.ocityId
			getDistrictGroup()
		}
	}
}

// 选择市回调
const bindPickerChange_city = (e) => {
	districtId.value = null
	districtList.value = []
	currtIndex_district.value = null

	currtIndex_city.value = e.detail.value
	cityId.value = cityList.value[currtIndex_city.value].id

	studentInfo.value.ocityId = cityId.value
	studentInfo.value.ocity = cityList.value[currtIndex_city.value].name

	// 获取区分组
	getDistrictGroup()
}

// 获取区分组
const getDistrictGroup = async () => {
	const res = await getDistrictGroupReq({
		cityId: cityId.value
	})
	// console.log(res)
	if (res.code == 200) {
		districtList.value = res.data

		if (studentInfo.value.odistrictId) {
			districtList.value.forEach((ele, index) => {
				if (ele.id == studentInfo.value.odistrictId) {
					currtIndex_district.value = index
				}
			})
		}
	}
}

// 选择区回调
const bindPickerChange_district = (e) => {
	currtIndex_district.value = e.detail.value
	districtId.value = districtList.value[currtIndex_district.value].id

	studentInfo.value.odistrictId = districtId.value
	studentInfo.value.odistrict = districtList.value[currtIndex_district.value].name
}

// 点击上一步按钮回调
const handleUp = () => {
	uni.navigateBack()
}

// 点击下一步按钮回调
const handleNext = () => {
	if (!studentInfo.value.phone) {
		uni.showToast({
			title: '请输入手机号码',
			icon: 'none'
		})
		return
	}

	let reg = /^1[3-9]\d{9}$/
	if (!reg.test(studentInfo.value.phone)) {
		uni.showToast({
			title: '手机号码格式错误',
			icon: 'none'
		})
		return
	}

	if (currtIndex.value == null) {
		uni.showToast({
			title: '请选择籍贯省',
			icon: 'none'
		})
		return
	}

	if (currtIndex_city.value == null) {
		uni.showToast({
			title: '请选择籍贯市',
			icon: 'none'
		})
		return
	}

	if (currtIndex_district.value == null) {
		uni.showToast({
			title: '请选择籍贯区',
			icon: 'none'
		})
		return
	}

	if (currtIndex2.value == null) {
		uni.showToast({
			title: '请选择家庭住址省',
			icon: 'none'
		})
		return
	}

	if (currtIndex_city2.value == null) {
		uni.showToast({
			title: '请选择家庭住址市',
			icon: 'none'
		})
		return
	}

	if (currtIndex_district2.value == null) {
		uni.showToast({
			title: '请选择家庭住址区',
			icon: 'none'
		})
		return
	}

	if (!studentInfo.value.address) {
		uni.showToast({
			title: '请输入家庭住址',
			icon: 'none'
		})
		return
	}

	if (!studentInfo.value.zipCode) {
		uni.showToast({
			title: '请输入邮编',
			icon: 'none'
		})
		return
	}

	if (!fvs.value.length) {
		uni.showToast({
			title: '请至少添加一名家庭成员',
			icon: 'none'
		})
		return
	}

	studentInfo.value.fvs = fvs.value
	uni.setStorageSync('studentInfo', studentInfo.value)
	uni.navigateTo({
		url: '/pages/arrive/arrive'
	})
}

// 添加家庭成员按钮回调
const handleAdd = () => {
	popup.value.open()
}

// 弹窗关闭图标回调
const handleClose = () => {
	popup.value.close()
}

// 点击弹窗确定按钮回调
const handleConfirm = () => {
	if (!popObj.value.familyShip) {
		uni.showToast({
			title: '请输入称谓',
			icon: 'none'
		})
		return
	}
	if (!popObj.value.name) {
		uni.showToast({
			title: '请输入姓名',
			icon: 'none'
		})
		return
	}
	if (!popObj.value.workUnit) {
		uni.showToast({
			title: '请输入工作单位',
			icon: 'none'
		})
		return
	}
	if (!popObj.value.phone) {
		uni.showToast({
			title: '请输入联系方式',
			icon: 'none'
		})
		return
	}

	let reg = /^1[3-9]\d{9}$/
	if (!reg.test(popObj.value.phone)) {
		uni.showToast({
			title: '联系方式格式错误',
			icon: 'none'
		})
		return
	}

	uni.showModal({
		title: '提示',
		content: '确定添加吗？',
		success: (res) => {
			if (res.confirm) {
				fvs.value.push(popObj.value)
				popObj.value = {
					familyShip: '',
					name: '',
					workUnit: '',
					phone: ''
				}
				popup.value.close()
			}
		}
	})
}

// 选择省回调
const bindPickerChange2 = (e) => {
	cityId2.value = null
	cityList2.value = []
	currtIndex_city2.value = null
	districtId2.value = null
	districtList2.value = []
	currtIndex_district2.value = null

	currtIndex2.value = e.detail.value
	provinceId2.value = provinceList2.value[currtIndex2.value].id

	studentInfo.value.provinceId = provinceId2.value
	studentInfo.value.province = provinceList2.value[currtIndex2.value].name

	// 获取市分组
	getCityGroup2()
}

// 获取市分组
const getCityGroup2 = async () => {
	const res = await getCityGroupReq({
		provinceId: provinceId2.value
	})
	// console.log(res)
	if (res.code == 200) {
		cityList2.value = res.data

		if (studentInfo.value.cityId) {
			cityList2.value.forEach((ele, index) => {
				if (ele.id == studentInfo.value.cityId) {
					currtIndex_city2.value = index
				}
			})

			cityId2.value = studentInfo.value.cityId
			getDistrictGroup2()
		}
	}
}

// 选择市回调
const bindPickerChange_city2 = (e) => {
	districtId2.value = null
	districtList2.value = []
	currtIndex_district2.value = null

	currtIndex_city2.value = e.detail.value
	cityId2.value = cityList2.value[currtIndex_city2.value].id

	studentInfo.value.cityId = cityId2.value
	studentInfo.value.city = cityList2.value[currtIndex_city2.value].name

	// 获取区分组
	getDistrictGroup2()
}

// 获取区分组
const getDistrictGroup2 = async () => {
	const res = await getDistrictGroupReq({
		cityId: cityId2.value
	})
	// console.log(res)
	if (res.code == 200) {
		districtList2.value = res.data

		if (studentInfo.value.districtId) {
			districtList2.value.forEach((ele, index) => {
				if (ele.id == studentInfo.value.districtId) {
					currtIndex_district2.value = index
				}
			})
		}
	}
}

// 选择区回调
const bindPickerChange_district2 = (e) => {
	currtIndex_district2.value = e.detail.value
	districtId2.value = districtList2.value[currtIndex_district2.value].id

	studentInfo.value.districtId = districtId2.value
	studentInfo.value.district = districtList2.value[currtIndex_district2.value].name
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	background-color: #f5f9ff;

	.header {
		padding-left: 20rpx;
		height: 93rpx;
		line-height: 93rpx;
		font-size: 28rpx;
		font-weight: bold;
		background-color: #fff;
	}

	.form {
		padding: 25rpx 0;
		margin-top: 10rpx;
		background-color: #fff;

		.form_box {
			display: flex;
			align-items: center;
			margin-bottom: 20rpx;
			height: 75rpx;

			.form_key {
				width: 170rpx;
				font-size: 28rpx;
				text-align: end;
			}

			.form_value {
				box-sizing: border-box;
				padding: 0 25rpx;
				width: 560rpx;
				height: 100%;
				border-radius: 4rpx;
				border: 2rpx solid #cccccc;

				.input {
					height: 100%;
				}
			}

			.form_select {
				display: flex;
				justify-content: space-between;
				width: 560rpx;

				.select_box {
					display: flex;
					align-items: center;
					justify-content: space-around;
					width: 178rpx;
					height: 74rpx;
					color: #ccc;
					font-size: 28rpx;
					border-radius: 4rpx;
					border: 2rpx solid #cccccc;

					.box_text {
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis;
					}
				}

				.active {
					color: #000;
				}
			}
		}
	}

	.family {
		background-color: #fff;

		.family_top {
			display: flex;
			align-items: center;
			padding: 0 20rpx;
			height: 90rpx;

			.top_title {
				margin-left: 20rpx;
				font-size: 30rpx;
				font-weight: bold;
			}
		}

		.family_table {
			padding: 0 20rpx;
			font-size: 24rpx;

			.table_box {
				display: flex;
				justify-content: space-between;
				align-items: center;
				height: 70rpx;
				border-bottom: 2rpx solid #e6e6e6;

				.title {
					width: 140rpx;
					text-align: center;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
				}

				.name {
					width: 140rpx;
					text-align: center;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
				}

				.work {
					width: 200rpx;
					text-align: center;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
				}

				.phone {
					width: 200rpx;
					text-align: center;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
				}
			}

			.top {
				background-color: #f0f3f7;
			}
		}
	}

	.tips {
		padding: 15rpx;
		font-size: 24rpx;
		background-color: #fff;

		.tips_box {
			padding: 20rpx 0 20rpx 10rpx;
			line-height: 58rpx;
			background-color: #fff2f2;
		}
	}

	.btns {
		display: flex;
		align-items: center;
		justify-content: space-around;
		padding: 10rpx 0 50rpx;
		height: 100rpx;
		background-color: #fff;

		.btn {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 300rpx;
			height: 100rpx;
			font-size: 32rpx;
			border-radius: 8rpx;
		}

		.up {
			border: 2rpx solid #0061ff;
		}

		.down {
			color: #fff;
			background-color: #0061ff;
		}
	}

	.pop {
		position: relative;
		display: flex;
		flex-direction: column;
		align-items: center;
		box-sizing: border-box;
		padding: 150rpx 20rpx 50rpx;
		width: 659rpx;
		height: 728rpx;
		border-radius: 15rpx;
		background: linear-gradient(180deg, #ebf2ff 0%, #ffffff 100%);

		.pop_img {
			position: absolute;
			top: -103rpx;
			left: 227rpx;
			width: 206rpx;
			height: 206rpx;
		}

		.pop_close {
			position: absolute;
			top: 19rpx;
			right: 29rpx;
		}

		.pop_box {
			display: flex;
			align-items: center;
			margin-bottom: 20rpx;

			.box_key {
				width: 150rpx;
				font-size: 28rpx;
				text-align: end;
			}

			.box_value {
				width: 300rpx;
				height: 70rpx;
				border-radius: 4rpx;
				border: 2rpx solid #cccccc;

				.input {
					padding: 0 20rpx;
					height: 100%;
				}
			}
		}

		.pop_btn {
			display: flex;
			align-items: center;
			justify-content: center;
			margin-top: 30rpx;
			width: 460rpx;
			height: 100rpx;
			color: #fff;
			font-size: 32rpx;
			border-radius: 8rpx;
			background-color: #0061ff;
		}
	}
}
</style>
