<template>
  <el-dialog class="team-edit-password-Dialog" :visible.sync="visible" width="30%" title="修改密码">
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule">
      <el-form-item label="登录密码：" label-width="120px" prop="newPassword">
        <el-input v-model="dataForm.newPassword" placeholder="请输入要修改的密码" />
      </el-form-item>
      <el-form-item prop="confirmPassword" label-width="120px" :label="$t('updatePassword.confirmPassword')">
        <el-input v-model="dataForm.confirmPassword" :placeholder="$t('updatePassword.confirmPassword')" />
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import { clearLoginInfo } from '@/page-subspecialty/utils/request.js'
import { resetRouter } from '@/page-subspecialty/router/index.js'
export default {
  props: {
    roleNameList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      props: {
        multiple: true,
        label: 'name',
        value: 'id',
        children: 'childrenList'
      },
      visible: false,
      dataForm: {
        newPassword: '',
        confirmPassword: '',
        userId: ''
      },
      params: {},
      loginInfo: {}
    }
  },
  computed: {
    dataRule() {
      var validateConfirmPassword = (rule, value, callback) => {
        if (this.dataForm.newPassword !== value) {
          return callback(new Error(this.$t('updatePassword.validate.confirmPassword')))
        }
        callback()
      }
      return {
        newPassword: [
          { required: true, message: '请输入要修改的密码', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.loginInfo = this.$store.state.currentUser
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields() // 重置表单
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function() {
      this.$refs.dataForm.validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http.put('/sys/user/updateOtherPassword', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              if (this.loginInfo.employeeId === this.params.employeeId) {
                clearLoginInfo()
                resetRouter()
                this.$router.replace({ name: 'login' })
              } else {
                this.$emit('refreshDataList')
              }
            }
          })
        }).catch(() => { })
      })
    }, 1000, { leading: true, trailing: false })
  }
}
</script>
<style lang="scss">
.team-edit-password-Dialog {
  .el-dialog__header {
    margin-bottom: 12px;
  }
  .el-cascader {
    display: block;
  }
  .el-dialog__body {
    padding-right: 30px;
  }
}
</style>
