<template>
  <div class="set-password-container">
    <!-- 将el-dialog改为直接显示 -->
    <div class="password-dialog">
      <div class="password-header">
        <span>设置密码</span>
      </div>
      <div class="password-body">
        <el-form ref="passwordForm" :model="form" label-position="right" :rules="rules" label-width="100px">
          <el-form-item label="旧密码" prop="oldPassword">
            <el-input v-model="form.oldPassword" type="password" show-password></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newPassword">
            <el-input v-model="form.newPassword" type="password" show-password></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input v-model="form.confirmPassword" type="password" show-password></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="password-footer">
        <el-button @click="submitNewPassword">确 定</el-button>
        <el-button @click="cancelSetPassword">取 消</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { updateUserPwd } from "@/api/company/companyUser";
import { removeToken } from "@/utils/auth";

export default {
  name: "SetPassword",
  data() {
    const validateConfirmPass = (rule, value, callback) => {
      if (value !== this.form.newPassword) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };

    return {
      form: {
        oldPassword: "",
        newPassword: "",
        confirmPassword: ""
      },
      rules: {
        oldPassword: [
          {required: true, message: "旧密码不能为空", trigger: "blur"}
        ],
        newPassword: [
          {required: true, message: "新密码不能为空", trigger: "blur"},
          {
            pattern: /^(?=.*[A-Za-z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,20}$/,
            message: "密码长度为 8-20 位，必须包含字母、数字和特殊字符",
            trigger: ["blur", "change"],
          }
        ],
        confirmPassword: [
          {required: true, message: "确认密码不能为空", trigger: "blur"},
          {validator: validateConfirmPass, trigger: "blur"}
        ]
      },
    };
  },
  methods: {
    submitNewPassword() {
      this.$refs.passwordForm.validate(valid => {
        if (valid) {
          // 调用API设置新密码
          updateUserPwd(this.form.oldPassword, this.form.newPassword).then(res => {
            if (res.code === 200) {
              this.$message.success('密码设置成功');
              // 更新首次登录状态
              localStorage.setItem('isFirstLogin', 'false');

              // 清除token等信息
              this.cancelSetPassword();
            } else {
              this.$message.error(res.message || '密码设置失败')
            }
          })
        }
      });
    },
    cancelSetPassword() {
      // 清除token等信息
      removeToken()

      // 跳转到登录页面
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
.set-password-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.password-dialog {
  width: 400px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.password-header {
  padding: 15px;
  border-bottom: 1px solid #ebeef5;
  font-size: 16px;
  font-weight: bold;
  color: #1f2d3d;
}

.password-body {
  padding: 20px;
}

.password-footer {
  padding: 10px 20px;
  text-align: right;
  border-top: 1px solid #ebeef5;
}
</style>
