<template>
  <el-dialog v-model="visible" width="400" title="修改密码" class="password-dialog"
    @closed="onCancel">
    <el-form :model="formData" status-icon :rules="rules" ref="ruleFormRef" class="demo-ruleForm">
      <!-- <el-form-item label="" prop="username"> -->
      <!-- 添加 autocomplete="off" 尝试阻止填充账号 -->
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input type="password" v-model.trim="formData.oldPassword" placeholder="请输入旧密码" autocomplete="new-password">
          <template #prefix><el-icon>
              <Lock />
            </el-icon></template>
        </el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input type="password" v-model.trim="formData.newPassword" placeholder="请输入新密码" autocomplete="new-password">
          <template #prefix><el-icon>
              <Lock />
            </el-icon></template>
        </el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="reNewPassword">
        <el-input type="password" v-model.trim="formData.reNewPassword" placeholder="请再次输入新密码" autocomplete="new-password">
          <template #prefix><el-icon>
              <Lock />
            </el-icon></template>
        </el-input>
      </el-form-item>

      <!-- <el-form-item label="" prop="captcha" class="captcha">
      <el-input class="input-captcha" v-model.trim="formData.captcha" placeholder="请输入验证码"></el-input>
      <img src="@/assets/images/组11拷贝2x.png" alt="验证码" />
    </el-form-item> -->
      <el-form-item>
        <footer class="form-footer">
          <el-button @click="onCancel"><span>取消</span></el-button>
          <el-button type="primary" @click="onSubmit" class="submit"><span>提交</span></el-button>
        </footer>
        <!-- <div class="other clearfix">
        <span class="forgot-password">忘记密码？</span>
        <span class="sign-up">立即注册</span>
        <span class="no-account">还没账号？</span>
      </div> -->
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup>
import { ref, watch,getCurrentInstance } from 'vue'
import { getSessionVar, clearSessionVar } from '@/utils/session'

let visible = ref(true)
const { proxy } = getCurrentInstance()
const ruleFormRef = ref()
let formData = ref({
  oldPassword: "",
  newPassword: "",
  reNewPassword: ""
})
const props = defineProps({ show: Boolean })
const emit = defineEmits(['update:modelValue'])
const validatePass = (rule, value, callback) => {
  if (value !== formData.value.newPassword) {
    callback(new Error('输入的密码不同'))
  } else {
    callback()
  }
}
const rules = {
  username: [{ required: true, message: "账号是必填", trigger: ["change", "blur"] }],
  oldPassword: [{ required: true, message: "必填", trigger: ["change", "blur"] }],
  newPassword: [{ required: true, message: "必填", trigger: ["change", "blur"] }],
  reNewPassword: [{ required: true, message: "必填", trigger: ["change", "blur"] },
  { validator: validatePass, trigger: ["blur", "change"] }
  ],
};
watch(() => props.show, (newVal) => {
  visible.value = newVal
})
function onCancel() {
  ruleFormRef.value.resetFields()
  emit('update:modelValue', false)
}
function onSubmit() {
  ruleFormRef.value.validate(async(valid) => {
    if (valid) {
      try {
        // 这里可以添加修改密码的逻辑
        console.log("提交的数据：", formData.value);
        const data = {
          action:'modifyPWD',
          id:getSessionVar('user_id'),
          params:[
            {
              name:'session_id',
              value:getSessionVar('session_id')
            },
            {
              name:'password',
              value: formData.value.oldPassword
            },
            {
              name:'new_password',
              value: formData.value.newPassword
            }
          ]
        }
        await proxy.$http.post('/open-platform/user/session',data )
        // 修改密码成功后，关闭对话框
        ruleFormRef.value.resetFields()
        emit('update:modelValue', false);
      } catch (error) {
        console.error("修改密码失败：", error);
      }
    }
  })
}
</script>

<style scoped lang="less">
.form-footer {
  text-align: right;
  margin-left: auto;
}
</style>