<template>
  <div class="login-title">
    <h1>首次登录</h1>
    <p>请完善资料</p>
  </div>
  <div class="login-form">
    <el-form
      ref="formRef"
      size="large"
      :model="formData"
      :rules="rules"
      label-width="0px"
    >
      <el-form-item label="" prop="name">
        <el-input
          v-model.trim="formData.name"
          placeholder="请输入姓名"
          clearable
        />
      </el-form-item>
      <el-form-item label="" prop="employeeId">
        <el-input
          v-model.trim="formData.employeeId"
          placeholder="请输入工号"
          clearable
        />
      </el-form-item>
      <el-form-item label="" prop="newPassword">
        <el-input
          v-model.trim="formData.newPassword"
          type="password"
          placeholder="输入6位以上新密码"
          show-password
          clearable
        />
      </el-form-item>
      <el-form-item label="" prop="confirmPassword">
        <el-input
          v-model.trim="formData.confirmPassword"
          type="password"
          placeholder="再次输入密码"
          show-password
          clearable
        />
      </el-form-item>

      <el-form-item>
        <el-button
          class="submit-btn"
          type="primary"
          :loading="loading"
          style="width: 100%"
          @click="submit"
          >确认</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width: 100%" link @click="goBack"
          >退出</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive } from 'vue';
  import { useRouter } from 'vue-router';
  import type { FormInstance, FormRules } from 'element-plus';
  import useLoading from '@/hooks/loading';
  // import { updateUserProfile } from '@/api/user'; // 假设有更新用户信息的API
  import { ElMessage } from 'element-plus';

  const router = useRouter();
  const formRef = ref<FormInstance>();
  const { loading, setLoading } = useLoading();

  const formData = reactive({
    name: '',
    employeeId: '',
    newPassword: '',
    confirmPassword: '',
  });

  const validateConfirmPassword = (rule: any, value: any, callback: any) => {
    if (value === '') {
      callback(new Error('请再次输入密码'));
    } else if (value !== formData.newPassword) {
      callback(new Error('两次输入的密码不一致'));
    } else {
      callback();
    }
  };

  const rules: FormRules = {
    name: [
      {
        required: true,
        message: '请输入姓名',
        trigger: 'change',
      },
    ],
    employeeId: [
      {
        required: true,
        message: '请输入工号',
        trigger: 'change',
      },
    ],
    newPassword: [
      {
        required: true,
        message: '请输入新密码',
        trigger: 'change',
      },
      {
        min: 6,
        message: '密码长度不能少于6位',
        trigger: 'change',
      },
    ],
    confirmPassword: [
      {
        required: true,
        validator: validateConfirmPassword,
        trigger: 'change',
      },
    ],
  };

  async function submit() {
    const valid = await formRef.value?.validate().catch(() => false);
    if (!valid) return;

    setLoading(true);
    try {
      // const res = await updateUserProfile(formData); // 调用API更新用户信息
      // if (res) {
      ElMessage.success('资料完善成功，请重新登录');
      // 清空store信息，跳转到登录页
      // userStore.resetInfo();
      // appStore.resetInfo();
      router.push({ name: 'Login' }); // 假设登录页路由名称为 Login
      // }
    } catch (e) {
      // console.error(e);
    } finally {
      setLoading(false);
    }
  }

  function goBack() {
    router.push({ name: 'Login' }); // 假设登录页路由名称为 Login
  }
</script>
