<template>
  <div style="display: inline-block">
    <el-dialog :visible.sync="dialog" :close-on-click-modal="false" :before-close="cancel" :title="title" append-to-body width="500px" @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="30%">
        <el-form-item :label="$t('common.Old_password')" prop="oldPass">
          <el-input v-model="form.oldPass" type="password" auto-complete="on" style="width: 70%;" />
        </el-form-item>
        <el-form-item :label="$t('common.New_password')" prop="newPass">
          <el-input v-model="form.newPass" type="password" auto-complete="on" style="width: 70%;" />
        </el-form-item>
        <el-form-item :label="$t('common.Verify_password')" prop="confirmPass">
          <el-input v-model="form.confirmPass" type="password" auto-complete="on" style="width: 70%;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancel">{{ $t('common.Cancel') }}</el-button>
        <el-button :loading="loading" type="primary" @click="doSubmit">{{ $t('common.Confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import store from '@/store'
import { updatePass } from '@/views/system/user/user'
import i18n from '@/i18n'
export default {
  data() {
    const confirmPass = (rule, value, callback) => {
      if (value) {
        if (this.form.newPass !== value) {
          callback(new Error(this.$t('common.Tip8')))
        } else {
          callback()
        }
      } else {
        callback(new Error(i18n.t('common.Tip9')))
      }
    }
    return {
      loading: false, dialog: false, title: i18n.t('common.Change_password'), form: { oldPass: '', newPass: '', confirmPass: '' },
      rules: {
        oldPass: [
          { required: true, message: i18n.t('common.Tip10'), trigger: 'blur' }
        ],
        newPass: [
          { required: true, message: i18n.t('common.Tip11'), trigger: 'blur' },
          { min: 6, max: 20, message: i18n.t('common.Tip12'), trigger: 'blur' }
        ],
        confirmPass: [
          { required: true, validator: confirmPass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          updatePass(this.form).then(res => {
            this.resetForm()
            this.$notify({
              title: '密码修改成功，请重新登录',
              type: 'success',
              duration: 1500
            })
            setTimeout(() => {
              store.dispatch('LogOut').then(() => {
                location.reload() // 为了重新实例化vue-router对象 避免bug
              })
            }, 1500)
          }).catch(err => {
            this.loading = false
            console.log(err.response.data.message)
          })
        } else {
          return false
        }
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = { oldPass: '', newPass: '', confirmPass: '' }
    }
  }
}
</script>

<style scoped>

</style>
