<template>
  <el-dialog
    class="sl"
    title="修改密码"
    :visible.sync="visible"
    width="300px"
    :append-to-body="true">
    <el-form :model="dataForm" :show-message="false" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="Account">
        <span>{{ userName }}</span>
      </el-form-item>
      <el-form-item label="Old Password" prop="password">
        <el-input type="password" v-model="dataForm.password"></el-input>
      </el-form-item>
      <el-form-item label="New Password" prop="newPassword">
        <el-input type="password" v-model="dataForm.newPassword"></el-input>
      </el-form-item>
      <el-form-item label="Confirm Password" prop="confirmPassword">
        <el-input type="password" v-model="dataForm.confirmPassword"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()">Save</el-button>
      <el-button @click="visible = false">Close</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { clearLoginInfo } from '@/utils'
  export default {
    data () {
      var validateConfirmPassword = (rule, value, callback) => {
        if (this.dataForm.newPassword !== value) {
          callback(new Error('The confirmed password does not match the new password.'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        dataForm: {
          password: '',
          newPassword: '',
          confirmPassword: ''
        },
        dataRule: {
          password: [
            { required: true, message: 'The original password cannot be empty.', trigger: 'blur' }
          ],
          newPassword: [
            { required: true, message: 'The new password cannot be empty.', trigger: 'blur' }
          ],
          confirmPassword: [
            { required: true, message: 'The confirm password field cannot be empty.', trigger: 'blur' },
            { validator: validateConfirmPassword, trigger: 'blur' }
          ]
        }
      }
    },
    computed: {
      userName: {
        get () { return this.$store.state.user.name }
      },
      mainTabs: {
        get () { return this.$store.state.common.mainTabs },
        set (val) { this.$store.commit('common/updateMainTabs', val) }
      }
    },
    methods: {
      // 初始化
      init () {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          let password = this.dataForm.password
          let newPassword = this.dataForm.newPassword
          let confirmPassword =this.dataForm.confirmPassword
          if (!password){
            this.$message.warning('The original password cannot be empty.')
            return
          }
          if (!newPassword){
            this.$message.warning('The new password cannot be empty.')
            return
          }
          if (!confirmPassword){
            this.$message.warning('The confirm password field cannot be empty.')
            return
          }
          if (valid) {
            this.$http({
              url: this.$http.adornUrl('/sys/user/password'),
              method: 'post',
              data: this.$http.adornData({
                'password': this.dataForm.password,
                'newPassword': this.dataForm.newPassword
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message.success( 'Operation successful.')
                this.visible = false
                this.$nextTick(() => {
                  this.mainTabs = []
                  clearLoginInfo()
                  this.$router.replace({ name: 'login' })
                })
              } else {
                this.$message.warning(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

