<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="formatName(form.name||form.nickname)" shape="square" fontSize="20"
							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">
						<input name="card" class="input" :value="form.card" placeholder="请输入身份证号码" />
					</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;
	// openid
	const openid = computed(() => {
		return uni.getStorageSync('openid');
	})
	// 基本信息
	const config = ref({ logo: '' });
	// 用户信息
	const form = ref({});
	// 字典表
	const sexList = ref([]);
	onLoad(async () => {
		await searchConfig();
		await search();
	})
	// config信息
	const searchConfig = async () => {
		config.value = uni.getStorageSync('config');
	};
	// 查询
	const search = async () => {
		const res = await $api(`login/wxapp/${openid.value}`, 'POST', {});
		if (res.errcode == '0') {
			form.value = res.data
			uni.setStorageSync('user', res.data);
		}
	};
	// 保存
	const formSubmit = async (e) => {
		if (form.value._id) {
			let data = e.detail.value;
			data = delEmptyQueryNodes(data);
			const res = await $api(`user/${form.value._id}`, 'POST', data);
			if (res.errcode == '0') search();
		} else {
			uni.navigateTo({
				url: `/pagesHome/login/index`
			})
		}
	};
	// 去除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;
	};
	const formatName = (str) => {
		if (str) return str.substr(0, 1) + new Array(str.length).join('');
	};
</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>