<script setup>
	/**
	 * name：
	 * user：sa0ChunLuyu
	 * date：2024年9月11日 19:24:50
	 */
	import {
		ref
	} from 'vue'
	import {
		$url,
		$api,
		$response
	} from '@/api'
	import {
		onShow
	} from '@dcloudio/uni-app'
	import {
		useStore
	} from '@/store'
	const $store = useStore()
	import TitleComponent from '../src/title.vue'
	const address_data = ref([])
	const getAddressData = async () => {
		const response = await $api('AddressData')
		$response(response, () => {
			address_data.value = response.data.data.map((i) => {
				return {
					text: i.name,
					value: i.code,
					children: i.children.map((ii) => {
						return {
							text: ii.name,
							value: ii.code,
							children: ii.children.map((iii) => {
								return {
									text: iii.name,
									value: iii.code
								}
							})
						}
					})
				}
			})
			getUserInfo();
		})
	}

	const getUserInfo = async () => {
		uni.showLoading({
			title: "加载中",
		});
		const response = await $api('UserInfo')
		uni.hideLoading();
		$response(response, () => {
			$store.setUser(response.data.info);
			getUserHunjian()
		})
	}
	const form_show = ref(false)
	const getUserHunjian = async () => {
		const response = await $api('UserHunjian', {
			person_id: $store.user.person_id,
		})
		$response(response, () => {
			if (!!response.data.info['姓名']) {
				input_data.value = response.data.info
			}
			form_show.value = true
		})
	}

	const mountedAction = () => {
		getAddressData()
	}

	const config_ref = ref(null)
	const configRef = (e) => {
		if (!config_ref.value) {
			config_ref.value = e
			mountedAction()
		}
	}
	const number_type_array = [{
			value: 1,
			label: '身份证'
		},
		{
			value: 2,
			label: '驾驶证'
		},
		{
			value: 3,
			label: '护照'
		},
		{
			value: 4,
			label: '港澳通行证'
		},
		{
			value: 5,
			label: '外国人永久居留身份证'
		},
		{
			value: 6,
			label: '其他'
		},
	]

	const getEndDate = () => {
		const date = new Date();
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();
		month = month > 9 ? month : '0' + month;
		day = day > 9 ? day : '0' + day;
		return `${year}-${month}-${day}`;
	}

	const input_data_default = {
		'国籍': '中国',
		'证件类型': 0,
		'证件号': "",
		'姓名': "",
		'生日': getEndDate(),
		'民族': "",
		'现地址省市区': [{
				value: '11',
				text: '北京市'
			},
			{
				value: '1101',
				text: '市辖区'
			},
			{
				value: '110101',
				text: '东城区'
			},
		],
		'现地址': "",
		'详细地址省市区': [{
				value: '11',
				text: '北京市'
			},
			{
				value: '1101',
				text: '市辖区'
			},
			{
				value: '110101',
				text: '东城区'
			},
		],
		'详细地址': "",
		'户籍地址省市区': [{
				value: '11',
				text: '北京市'
			},
			{
				value: '1101',
				text: '市辖区'
			},
			{
				value: '110101',
				text: '东城区'
			},
		],
		'户籍地址': "",
		'工作单位': "",
		'文化程度': "",
		'职业': "",
		'出生地': "",
		'配偶姓名': "",
		'配偶证件类型': 0,
		'配偶证件号': "",
		'血缘关系': "无",
		'邮政编码': "",
		'手机号码': "",
	}
	const input_data = ref(JSON.parse(JSON.stringify(input_data_default)))
	const bindPickerChange = (e, key) => {
		input_data.value[key] = e.detail.value
	}
	const bindDateChange = (e, key) => {
		input_data.value[key] = e.detail.value
	}
	const radioChange = (e, key) => {
		input_data.value[key] = e.detail.value
	}
	const editDoneClick = () => {
		let post_data = JSON.parse(JSON.stringify(input_data.value))
		post_data['证件类型'] = number_type_array[input_data.value['证件类型']].value
		post_data['配偶证件类型'] = number_type_array[input_data.value['配偶证件类型']].value
		// for (let i in post_data) {
		// 	if (!post_data[i]) {
		// 		return uni.$lu.toast(`请填写${i}`)
		// 	}
		// }
		HunQianQuestionSubmit(post_data)
	}
	const HunQianQuestionSubmit = async (post_data) => {
		const response = await $api('HunQianQuestionSubmit', {
			person_id: $store.getUser()?.person_id,
			content: post_data
		})
		$response(response, () => {
			uni.navigateTo({
				url: '/pages/main/combo/combo'
			})
		})
	}

	const addressChange = (e, key) => {
		input_data.value[key] = e.detail.value
	}

	onShow(() => {
		if (!!config_ref.value) {
			mountedAction()
		}
	})
</script>
<template>
	<view>
		<view v-if="!!$store.config">
			<view :ref="configRef"></view>
		</view>
		<view v-if="form_show" class="page_wrapper">
			<TitleComponent title="信息登记"></TitleComponent>
			<view class="form_wrapper">
				<view v-for="(i,k) in input_data" :key="k" class="form_item_wrapper">
					<view class="form_title_wrapper">{{ k }}</view>
					<view class="form_input_wrapper">
						<template v-if="['证件类型','配偶证件类型'].includes(k)">
							<view class="select_wrapper">
								<picker class="select_picker_wrapper" @change="(e)=>{bindPickerChange(e, k)}" :value="input_data[k]"
									:range="number_type_array" range-key="label">
									<view>{{ number_type_array[input_data[k]].label }}</view>
								</picker>
								<view class="select_icon_wrapper">
									<uni-icons color="#808080" type="right" size="20"></uni-icons>
								</view>
							</view>
						</template>
						<template v-else-if="k === '生日'">
							<picker mode="date" :value="input_data[k]" :end="getEndDate" @change="(e)=>{bindDateChange(e, k)}">
								<view>{{ input_data[k] }}</view>
							</picker>
						</template>
						<template v-else-if="['现地址省市区','详细地址省市区','户籍地址省市区'].includes(k)">
							<uni-data-picker v-if="address_data.length !== 0" v-model="input_data[k]" placeholder="请选择地址"
								popup-title="请选择城市" :localdata="address_data" :step-searh="true" @change="(e)=>{addressChange(e,k)}">
							</uni-data-picker>
						</template>
						<template v-else-if="k === '血缘关系'">
							<view>
								<radio-group class="radio_wrapper" @change="(e)=>{radioChange(e, k)}">
									<view class="radio_item_wrapper">
										<radio style="transform:scale(0.7)" value="无" :checked="input_data[k] === '无'" />无
									</view>
									<view class="radio_item_wrapper">
										<radio style="transform:scale(0.7)" value="有" :checked="input_data[k] === '有'" />有
									</view>
								</radio-group>
							</view>
						</template>
						<template v-else>
							<input v-model="input_data[k]" :placeholder="`请输入${k}`" />
						</template>
					</view>
				</view>
			</view>
			<view @click="editDoneClick()" class="button_wrapper">提交</view>
			<view style="height: 100rpx;"></view>
		</view>
	</view>
</template>
<style>
	.uni-data-tree-dialog {
		z-index: 999;
	}

	.input-value-border {
		border: none !important;
	}

	.input-value {
		padding: 0 !important;
	}

	.selected-area {
		font-size: 30rpx !important;
	}

	.icon-clear {
		display: none !important;
	}
</style>
<style scoped>
	.address_item_wrapper {
		margin-right: 5rpx;
	}

	.radio_item_wrapper {
		margin-right: 30rpx;
		display: flex;
		align-items: center;
	}

	.radio_wrapper {
		display: flex;
		align-items: center;
	}

	.select_icon_wrapper {
		position: absolute;
		top: 50%;
		right: 20rpx;
		transform: translateY(-50%);
	}

	.select_picker_wrapper {
		position: relative;
		z-index: 9;
		width: 100%;
	}

	.select_wrapper {
		position: relative;
	}

	.form_wrapper {
		margin-top: 20rpx;
	}

	.form_input_wrapper {
		width: calc(100% - 140rpx - 20px);
		margin-left: 20px;
		position: relative;
	}

	.form_title_wrapper {
		width: 140rpx;
	}

	.form_item_wrapper {
		display: flex;
		align-items: center;
		padding: 20rpx 0;
		font-size: 26rpx;
		border-bottom: 1rpx solid #cccccc80;
	}

	.button_wrapper {
		background: #279EA3;
		color: #ffffff;
		font-size: 28rpx;
		text-align: center;
		line-height: 90rpx;
		height: 90rpx;
		width: 600rpx;
		border-radius: 10rpx;
		margin: 50rpx auto 0;
	}

	.page_wrapper {
		padding-top: 20rpx;
		width: calc(100% - 50rpx);
		margin: 0 auto;
	}
</style>