<template>
  <el-dialog
    title="修改密码"
    :visible.sync="dialogVisible"
    width="33%"
    :before-close="handleClose"
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    :append-to-body="true"
    v-loading.fullscreen.lock="fullscreenLoading"
  >
    <el-form
      v-if="dialogVisible"
      :model="number"
      ref="number"
      :rules="rules"
      label-width="100px"
      label-position="right"
    >
      <el-form-item label prop="oldPassword">
        <template slot="label" class="status"><span>原密码</span>：</template>
        <el-input
          placeholder="请输入原密码"
          v-model.trim="number.oldPassword"
          show-password
          minlength="6"
          maxlength="18"
          show-word-limit
        >
        </el-input>
      </el-form-item>
      <el-form-item label prop="newPassword">
        <template slot="label" class="status"><span>新密码</span>：</template>
        <el-input
          placeholder="请输入新密码"
          v-model.trim="number.newPassword"
          show-password
          minlength="6"
          maxlength="18"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label prop="password">
        <template slot="label" class="status"><span>确认密码</span>：</template>
        <el-input
          placeholder="请输入确认密码"
          v-model.trim="number.password"
          show-password
          minlength="6"
          maxlength="18"
          show-word-limit
        >
        </el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取 消</el-button>
      <el-button
        size="small"
        style="margin-left: 60px"
        type="primary"
        @click="submit('number')"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import * as api from '@/api/api'
export default {
  data() {
    const validatePass = (rule, value, callback) => {
      if (value == '') {
        callback(new Error('原密码不能为空'))
      } else if (value.length < 6 || value.length > 18) {
        callback(new Error('密码不能小于6位，大于18位'))
      } else {
        callback()
      }
    }
    const validatenewPassword = (rule, value, callback) => {
      if (value == '') {
        callback(new Error('新密码不能为空'))
      } else if (value.length < 6 || value.length > 18) {
        callback(new Error('密码不能小于6位，大于18位'))
      } else {
        callback()
      }
    }
    const validatenewPass = (rule, value, callback) => {
      if (value == '') {
        callback(new Error('确认密码不能为空'))
      } else if (value != this.number.newPassword) {
        callback(new Error('确认密码与新密码不一致'))
      } else {
        callback()
      }
    }
    return {
      fullscreenLoading: false,
      dialogVisible: false,
      number: {
        account: JSON.parse(localStorage.getItem('userInfo')).account,
        newPassword: '',
        oldPassword: '',
        password: ''
      },
      id: '',
      rules: {
        oldPassword: [
          { required: true, trigger: 'blur', validator: validatePass }
        ],
        newPassword: [
          { required: true, trigger: 'blur', validator: validatenewPassword }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatenewPass }
        ]
      }
    }
  },
  methods: {
    handleClose() {
      this.dialogVisible = false
      this.$refs['number'].resetFields()
    },
    openClose() {
      this.dialogVisible = true
    },
    /**
     * @method 确定提交
     * **/
    submit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.fullscreenLoading = true
          let code = {
            account: this.number.account,
            newPassword: this.number.newPassword,
            oldPassword: this.number.oldPassword,
            password: this.number.password
          }
          api
            .POST('/update', code)
            .then(res => {
              if (res.code == 0) {
                this.handleClose()
                this.$message.success(res.message)
                this.fullscreenLoading = false
              }
            })
            .catch(() => {
              this.fullscreenLoading = false
            })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
// .status {
//   padding: 0 12px 0 0;
// }
// .status::before {
//   content: '*';
//   color: #f56c6c;
//   margin-right: 4px;
// }
</style>