<template>
	<view class="container">
		<view class="download-card">
			<form>
				<view class="uni-form-item uni-row">
					<view class="title">旧密码</view>
					<input :class="'uni-input '+oldPassword" name="oldPassword" :password="true" :value="user.oldPassword"
						placeholder="请输入旧密码" @focus="inputFocus('oldPassword')" @blur="inputBlur('oldPassword')"
						@input="inputOld" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">新密码</view>
					<input :class="'uni-input '+newPassword" name="newPassword" :password="true" :value="user.newPassword"
						placeholder="请输入新密码" @focus="inputFocus('newPassword')" @blur="inputBlur('newPassword')"
						@input="inputNew" />
				</view>
				<view class="uni-form-item uni-row">
					<view class="title">确认密码</view>
					<input :class="'uni-input '+confirmPassword" name="confirmPassword" :password="true"
						:value="user.confirmPassword" placeholder="请确认新密码" @focus="inputFocus('confirmPassword')"
						@blur="inputBlur('confirmPassword')" @input="inputConfirm" />
				</view>
				<button type="primary" @click="submit">提交</button>
			</form>
		</view>
	</view>
</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 = {
		oldPassword : string | null,
		newPassword : string | null,
		confirmPassword : string | null
	}

	export default {
		data() {
			return {
				oldPassword: '',
				newPassword: '',
				confirmPassword: '',
				user: {
					oldPassword: null,
					newPassword: null,
					confirmPassword: null
				} as User,
			}
		},
		onBackPress() {
			// 覆盖系统默认的返回行为，返回到指定页面
			uni.navigateTo({
				url: `/pages/mine/index`,
				// 修改动画方向为从左到右退回
				animationType: 'slide-in-left', // 使用从左到右滑出的动画效果
				animationDuration: 300 // 动画持续时间，单位ms
			})
			// 返回true表示拦截默认返回行为
			return true
		},
		methods: {
			inputFocus(tag : string) {
				this.$data[tag] = "my-focus"
			},
			inputBlur(tag : string) {
				this.$data[tag] = "my-blur"
			},
			inputOld(event : UniInputEvent) {
				this.user.oldPassword = event.detail.value
			},
			inputNew(event : UniInputEvent) {
				this.user.newPassword = event.detail.value
			},
			inputConfirm(event : UniInputEvent) {
				this.user.confirmPassword = event.detail.value
			},
			submit() {
				if (this.user.oldPassword == null) {
					uni.showToast({
						title: "旧密码不能为空",
						icon: "error"
					})
					return
				}
				if (this.user.newPassword == null) {
					uni.showToast({
						title: "新密码不能为空",
						icon: "error"
					})
					return
				} else if (this.user.newPassword != null && (this.user.newPassword.length < 6 || this.user.newPassword.length > 20)) {
					uni.showToast({
						title: "长度在6到20个字符",
						icon: "error"
					})
					return
				}
				if (this.user.oldPassword == this.user.newPassword) {
					uni.showToast({
						title: "新旧密码不能相同",
						icon: "error"
					})
					return
				}
				if (this.user.confirmPassword == null) {
					uni.showToast({
						title: "确认密码不能为空",
						icon: "error"
					})
					return
				} else if (this.user.confirmPassword != this.user.newPassword) {
					uni.showToast({
						title: "两次输入的密码不一致",
						icon: "error"
					})
					return
				}

				let username = '';
				let oldPassword = '';
				getList('app_user', 'username', getCurrentUserNameSync(), 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];
						username = user.getString('username') ?? '';
						oldPassword = user.getString('password') ?? '';
					}
					// 添加逻辑判断：验证原密码是否匹配
					if (oldPassword == this.user.oldPassword) {
						let updatedData = "password='" + this.user.confirmPassword + "' ";
						updateData('app_user', updatedData, 'username', username).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/index'
								});
							} else {
								uni.showToast({
									title: "更新失败，请稍后重试！",
									icon: "error"
								});
							}
						});
					} 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;
	}
</style>