<template>
	<view class="my-info-page">
		<!-- 导航区域 -->
		<customNavbarVue title="个人信息" :show-back-btn="true" @back="handleBack"></customNavbarVue>
		<!-- 姓名 -->
		<view class="form-label-input">
			<view class="phone-form-label"><text class="form-label-require">*</text>姓名</view>
			<input v-model="data.realName"  placeholder="请输入姓名" />
			<icon :style="{ backgroundImage: 'url(' + data.jtIcon + ')' }"></icon>
		</view>
		<!-- 证件号 -->
		<view class="form-label-input">
			<view class="phone-form-label" @click="idCardChange">
				<text class="form-label-require">*</text>
				<text v-if="data.idtype ==1">身份证号</text>
				<text v-if="data.idtype ==2">护照号</text>
				<icon @click="idCardChange" class="change-icon">切换</icon>
			</view>
			<input v-model="data.idcard"  placeholder="请输入证件号" />
			<icon :style="{ backgroundImage: 'url(' + data.jtIcon + ')' }"></icon>
		</view>
		<view class="form-label-input">
			<view class="phone-form-label"><text class="form-label-require"></text>具体地址</view>
			<input v-model="data.juzhuDizhi"  placeholder="请输入具体地址" />
			<icon :style="{ backgroundImage: 'url(' + data.jtIcon + ')' }"></icon>
		</view>

		<button type="default" class="phone-green-btn info-btn" @click="handleUpdate">修改资料</button>
	</view>
</template>

<script setup>
	import customNavbarVue from "@/components/custom-navbar/custom-navbar.vue";
	import * as myApi from "@/api/kehu.js";
	import {getAliyunPolicy} from "@/api/jiazheng.js"
	import {ref,reactive} from "vue"
	import {onLoad} from "@dcloudio/uni-app"
	import cacheManager from '@/utils/cacheManager.js';
	const defaultImage1 = ref({
		url:'', extname: 'png', name: 'moren.png'
	})

	const imageStyles = ref({
		width: '50rpx',
		height: '50rpx',
		background: 'red'
	});

	const data = reactive({
		from: 'shouye', // my | kaoshi | shouye
		realName: '',
		idcard: '',
		userId: null,
		jtIcon: '',
		juzhuDizhi: '',
		idtype: 1,
	})

	onLoad((options) => {
		data.from = options.from || 'shouye';
		data.jtIcon= cacheManager.get('projectImg').nav_bar_jt_bottom;
		initPage();
	})
	
	function idCardChange() {
		data.idtype = data.idtype == 1 ? 2 : 1;
	}

	function handleUpdate() {
		let arr = [];
		if (!data.realName) {
			arr.push('姓名');
		}
		if (!data.idcard) {
			arr.push('证件号');
		}
		if (!data.realName ||!data.idcard) {
			uni.showToast({
				icon: 'none',
				title: `请完善${arr.join('、')}信息！`
			})
			return;
		}
		
		const opt = {
			juzhuDizhi: data.juzhuDizhi,
			idtype: data.idtype, 
			idcard: data.idcard,
			realName: data.realName,
			userId: data.userId,
		}
		myApi.getKehuUpdate(opt).then(res => {
			if (res.data) {
				uni.showToast({
					title: '更新成功'
				})
				handleBack();
			}
		})

	}

	function handleBack() {
		uni.navigateBack()
	}

	function initPage() {
		myApi.getKehuUser().then(res => {
			const {juzhuDizhi,idtype,idcard,realName,userId} = res.data;
			data.idtype = res.data.idtype;
			data.idcard = res.data.idcard;
			data.realName = res.data.realName; 
			data.userId = res.data.userId;
			data.juzhuDizhi = res.data.juzhuDizhi
		})
	}
</script>

<style>

</style>