<template>
  <el-dialog
    title="修改密码"
    :visible.sync="dialogVisible"
    width="500px"
    @close="handleClose"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px"
    >

      <el-form-item label="旧密码" prop="oldPassword">
        <el-input
          v-model="form.oldPassword"
          type="password"
          show-password
          placeholder="请输入当前密码"
        />
      </el-form-item>

      <el-form-item label="新密码" prop="password">
        <el-input
          v-model="form.password"
          type="password"
          show-password
          placeholder="6-16位字母数字组合"
        />
      </el-form-item>

      <el-form-item label="确认密码" prop="password_confirmation">
        <el-input
          v-model="form.password_confirmation"
          type="password"
          show-password
          placeholder="请再次输入新密码"
          @blur="validateConfirmPass"
          @keydown.enter.native="submitForm"
        />
      </el-form-item>
    </el-form>

    <span slot="footer">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="submitForm">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { setPassword } from '@/api/data'
import { Notification } from 'element-ui'

export default {
  data() {
    const validatePass = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入新密码'))
      } else if (value.length<=8) {
        callback(new Error('密码需大于8位'))
      } else {
        if (this.form.password_confirmation) {
          this.$refs.form.validateField('password_confirmation')
        }
        callback()
      }
    }

    const validateConfirmPass = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请确认密码'))
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }

    return {
      dialogVisible: false,
      form: {
        oldPassword: '',
        password: '',
        password_confirmation: ''
      },
      rules: {
        oldPassword: [
          { required: true, message: '请输入旧密码', trigger: 'blur' }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        password_confirmation: [
          { validator: validateConfirmPass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleClose() {
      this.$refs.form.resetFields()
    },
    validateConfirmPass() {
      this.$refs.form.validateField('password_confirmation')
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // 调用API提交密码修改
          console.log('提交数据:', this.form)
          setPassword(this.form)
            .then(() => {
              Notification({
                title: '成功',
                message: '修改成功～',
                type: 'success'
              })
              this.dialogVisible = false
            })
        }
      })
    },
    openClose() {
      this.dialogVisible = !this.dialogVisible
    }
  }
}
</script>
