<template>
	<view class="content">
		<view class="one">
			<form @submit="formSubmit">
				<view class="value other" style="display: none;">
					<view class="title">id</view>
					<view class="label">
						<input name="id" class="input" :value="form.id" placeholder="请输入id" />
					</view>
				</view>
				<view class="value icon">
					<view class="title">头像</view>
					<view class="label">
						<u-avatar :text="form.name" shape="square" fontSize="40" randomBgColor></u-avatar>
					</view>
				</view>
				<view class="remark">
					实名信息(仅用于咨询、免费估值功能)
				</view>
				<view class="value other">
					<view class="title">姓名</view>
					<view class="label">
						<input name="name" class="input" :value="form.name" placeholder="请输入真实姓名" />
					</view>
				</view>
				<view class="value other margin">
					<view class="title">昵称</view>
					<view class="label">
						<input name="nickname" class="input" :value="form.nickname" placeholder="请输入昵称" />
					</view>
				</view>
				<view class="value other">
					<view class="title">性别</view>
					<view class="label">
						<picker name="sex" @change="sexChange" :value="index" :range="sexList" range-key="dictLabel">
							<view class="picker">{{form.sex||'请选择性别'}}</view>
						</picker>
					</view>
				</view>
				<view class="value other">
					<view class="title">手机号</view>
					<view class="label">
						<input name="phone" class="input" :value="form.phone" placeholder="请输入手机号" />
					</view>
				</view>
				<view class="button">
					<button type="warn" size="mini" form-type="submit">保存</button>
				</view>
			</form>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { getCurrentInstance, computed, ref } from 'vue';
	//该依赖已内置不需要单独安装
	import { onLoad } from "@dcloudio/uni-app";
	// 请求接口
	const $api = getCurrentInstance()?.appContext.config.globalProperties.$api;
	const $config = getCurrentInstance()?.appContext.config.globalProperties.$config;
	const $apifile = getCurrentInstance()?.appContext.config.globalProperties.$apifile;
	// openid
	const openid = computed(() => {
		return uni.getStorageSync('openid');
	})
	// 基本信息
	const config = ref({ logoUrl: '' });
	// 用户信息
	const form = ref({ icon: '', city: '', sex: '', nickname: '', phone: '' });
	// 字典表
	const sexList = ref([]);
	onLoad(async () => {
		await searchOther();
		await searchConfig();
		await search();
		uni.$on('setCity', function (city) {
			form.value.city = city
		})
	})
	// 查询其他信息
	const searchOther = async () => {
		let res;
	};
	// config信息
	const searchConfig = async () => {
		config.value = uni.getStorageSync('config');
	};
	// 查询
	const search = async () => {

	};
	// 处理字典表数据
	const toData = (value, list) => {
		if (value) return value = list[value].dictLabel
	};
	// 性别选择
	const sexChange = (e) => {
		const data = sexList.value[e.detail.value]
		if (data) form.value.sex = data.dictLabel
	};
	// 保存
	const formSubmit = async (e) => {
		let data = e.detail.value;
		data.icon = form.value.icon;
		data = delEmptyQueryNodes(data);
		console.log(data);
	};
	// 去除obj里为null的数据
	const delEmptyQueryNodes = (obj = {}) => {
		Object.keys(obj).forEach((key) => {
			let value = obj[key];
			value && typeof value === 'object' && delEmptyQueryNodes(value);
			(value === '' || value === null || value === undefined || value.length === 0 || Object.keys(value).length === 0) && delete obj[key];
		});
		return obj;
	};
</script>
<style lang="scss" scoped>
	.content {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;
		background-color: var(--footColor);

		.one {
			.icon {
				padding: 2vw;
			}

			.margin {
				margin: 3vw 0 0 0;
			}

			.other {
				padding: 3vw 2vw;
				border-bottom: 1px solid var(--footColor);
			}

			.value {
				display: flex;
				justify-content: space-between;
				align-items: center;
				background-color: var(--mainColor);

				.label {
					.input {
						text-align: right;
					}

					.image {
						width: 15vw;
						height: 15vw;
						border-radius: 20vw;
					}
				}
			}

			.remark {
				padding: 4vw 2vw;
				text-align: center;
				color: var(--f99Color);
				font-size: var(--font18Size);
			}

			.button {
				margin: 2vw 0 0 0;
				text-align: center;

				button {
					background-color: var(--fFFColor);
					font-size: var(--font14Size);
					border-radius: 2vw;
				}
			}
		}
	}
</style>