<template>
	<!-- #ifdef APP -->
	<scroll-view style="flex:1">
	<!-- #endif -->
		<view class="container">
			<view class="download-card">
				<form>
					<view class="uni-form-item uni-row">
						<view class="title">登录账号</view>
						<input class="uni-input inputDisabled" name="username" :value="username" disabled = "true"/>
					</view>
					<view class="uni-form-item uni-row">
						<view class="title">用户姓名</view>
						<input class="uni-input" name="name" :value="name" @input="inputName"/>
					</view>
					<view class="uni-form-item uni-row">
						<view class="title">新密码</view>
						<input class="uni-input" name="password" :password="true" :value="password" @input="inputNew"
							placeholder="请输入新密码" />
					</view>
					<view class="uni-form-item uni-row">
						<view class="title">确认密码</view>
						<input class="uni-input" name="confirmPassword" :password="true" :value="confirmPassword" @input="inputConfirm"
							placeholder="请确认新密码" />
					</view>
					<button type="primary" @click="submit">提交</button>
				</form>
			</view>

		</view>

	<!-- #ifdef APP -->
	</scroll-view>
	<!-- #endif -->
</template>

<script lang="uts">
	import { updateUserPwd } from "@/api/system/user"
	import { updateData, getList } from '@/api/work';
	import { getCurrentUserSync, getCurrentUserNameSync } from '@/utils/auth.uts'

	type User = {
		username : string | null,
		name : string | null,
		password : string | null,
		confirmPassword : string | null
	}

	export default {
		data() {
			return {
				id: '',
				username: '',
				name: '',
				password: '',
				confirmPassword: '',
				user: {
					username: null,
					name: null,
					password: null,
					confirmPassword: null
				} as User,
			}
		},
		onBackPress() {
			// 覆盖系统默认的返回行为，返回到指定页面
			uni.navigateTo({
				url: `/pages/mine/person/PersonList`,
				// 修改动画方向为从左到右退回
				animationType: 'slide-in-left', // 使用从左到右滑出的动画效果
				animationDuration: 300 // 动画持续时间，单位ms
			})
			// 返回true表示拦截默认返回行为
			return true
		},
		onLoad(options) {
			// 获取URL参数中的id
			const id = options?.['id'] ?? ""
			this.id = id
			this.getUser(id)
		},
		methods: {
			inputName(event : UniInputEvent) {
				this.user.name = event.detail.value
			},
			inputNew(event : UniInputEvent) {
				this.user.password = event.detail.value
			},
			inputConfirm(event : UniInputEvent) {
				this.user.confirmPassword = event.detail.value
			},
			getUser(userId : string) {
				// 使用传入的userId参数获取特定用户的信息
				getList('app_user', 'id', userId, null, null, null).then((response : UTSJSONObject) => {
					if (response != null && response['data'] != null) {
						console.log(response)
						// 解析用户数据并设置到页面变量
						let dataList = response?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
						if (dataList != null && dataList.length > 0) {
							let user = dataList[0];
							this.username = user.getString('username') ?? '';
							this.name = user.getString('name') ?? '';
						}
					}
				})
			},
			submit() {
				if (this.user.password != null && (this.user.password.length < 6 || this.user.password.length > 20)) {
					uni.showToast({
						title: "长度在6到20个字符",
						icon: "error"
					})
					return
				}
				if (this.user.confirmPassword != null && (this.user.confirmPassword.length < 6 || this.user.confirmPassword.length > 20)) {
					uni.showToast({
						title: "长度在6到20个字符",
						icon: "error"
					})
					return
				} else if (this.user.confirmPassword != this.user.password) {
					uni.showToast({
						title: "两次输入的密码不一致",
						icon: "error"
					})
					return
				}
				if (this.user.name == null && this.user.password == null && this.user.confirmPassword == null) {
					uni.showToast({
						title: "没有做修改",
						icon: "error"
					})
					return
				}
				

				getList('app_user', 'id', this.id, null, null, null).then((res : UTSJSONObject) => {
					let userList = res?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
					console.log(userList);
					if (userList != null && userList.length > 0) {
						let user = userList[0];
						if (user != null) {
							let updatedData = "";
							if(this.user.username != null && this.user.username != '') {
								updatedData += `username = '` + this.user.username + `' ,`
							}
							if(this.user.name != null && this.user.name != '') {
								updatedData += `name = '` + this.user.name + `' ,`
							}
							if (this.user.password != null && this.user.password != '') {
									updatedData += `password= '` + this.user.password + `' ,` 
								}
								
								// 移除最后一个多余的逗号（处理可能有空格和无空格的情况）
								if (updatedData.endsWith(', ')) {
									updatedData = updatedData.slice(0, -2);
								} else if (updatedData.endsWith(',')) {
									updatedData = updatedData.slice(0, -1);
								}
								
								updateData('app_user', updatedData, 'id', this.id).then((res : UTSJSONObject) => {
								let data = res?.['data'] as boolean ?? false;
								if (data != null && data == true) {
									uni.showToast({
										title: "更新成功！",
									});
									setTimeout(() => {
							
									}, 1500)
									uni.reLaunch({
										url: '/pages/mine/person/PersonList'
									});
								} else {
									uni.showToast({
										title: "更新失败，请稍后重试！",
										icon: "error"
									});
								}
							});
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss" scope>
	.container {
		padding: 10rpx;
		background-color: #f0f4f8;
		flex: 1;
		display: flex;
		flex-direction: column;
		font-family: "PingFang SC", "Helvetica Neue", Helvetica, Arial, sans-serif;
		/* #ifndef APP-ANDROID */
		min-height: 100vh;
		/* #endif */
		height: 120rpx;
	}

	.download-card {
		background: #ffffff;
		border-radius: 20rpx;
		padding: 15rpx 15rpx;
		box-shadow: 0 8rpx 15rpx rgba(0, 43, 92, 0.1);
		display: flex;
		flex-direction: column;
	}

	.title {
		width: 100rpx;
	}
	
	.inputDisabled {
		background-color: #dfdfdf;
	}
</style>