<template>
  <el-dialog
    title="修改密码"
    width="50%"
    @close="$emit('close')"
    :visible="show"
  >
    <el-form :model="form" ref="passedit" :rules="rule" :label-width="formLabelWidth">
      <div class="formcon">
          <el-form-item label="旧密码：" prop="oldPassword">
            <el-input onkeyup="value=value.replace(/[^\w~!@#$%^\&*]|_/ig,'')" :maxlength="INPUTLENGTH" show-word-limit  v-model="form.oldPassword"></el-input>
          </el-form-item>
          <el-form-item label="新密码：" prop="newPassword">
            <el-input onkeyup="value=value.replace(/[^\w~!@#$%^\&*]|_/ig,'')" :maxlength="INPUTLENGTH" show-word-limit  v-model="form.newPassword"></el-input>
          </el-form-item>
          <el-form-item label="确认密码：" prop="confirmPassword">
            <el-input onkeyup="value=value.replace(/[^\w~!@#$%^\&*]|_/ig,'')" :maxlength="INPUTLENGTH" show-word-limit  v-model="form.confirmPassword"></el-input>
          </el-form-item>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('close')">取 消</el-button>
      <el-button type="primary" @click="audit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {updatePwd} from "@/configue/api";
import { encodeStr } from '@/util'
import { Base64 } from 'js-base64'
let rule = {

  oldPassword: [{ required: true, message: "请输入旧密码", trigger: "blur" }],
  newPassword: [{ required: true, message: "请输入新密码", trigger: "blur" }],
  confirmPassword: [{ required: true, message: "请输入新密码", trigger: "blur" }]

};

export default {
  props: {
    id: {
      type: String || Number,
    },
    show: {
      type: Boolean,
    },
    form: Object
  },
  data() {
    return {
      formLabelWidth: "120px",
      rule
    };
  },
  methods: {
    audit() {
      this.$refs.passedit.validate(async (valid)=>{
        if (valid) {
          if (this.form.confirmPassword != this.form.newPassword) {
            return this.$alert('两次密码不一致', '提示')
          }

          let params = {
            newPassword: encodeStr(Base64.encode(this.form.newPassword)),
            oldPassword: encodeStr(Base64.encode(this.form.oldPassword)),
          }
          updatePwd(params,()=>{
                this.$emit('logout')
          })
        }
      })
      
    },
  },
};
</script>

<style lang="less" scoped>
.formcon {
  width: 100%;
  padding: 0 20px;
}
</style>
