<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 :model="number" ref="number" :rules="rules" label-width="100px">
      <el-form-item label prop="password">
        <template slot="label" class="status"
          ><span>密&#8195;&emsp;码</span>：</template
        >
        <el-input
          :type="type"
          placeholder="请输入"
          v-model.trim="number.password"
          autocomplete="off"
          minlength="6"
          maxlength="18"
          show-word-limit
          readonly
          @focus="focus"
          onfocus="this.removeAttribute('readonly')"
        >
          <template slot="suffix">
            <span class="el-icon-view" @click="elIconView"> </span>
          </template>
        </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位'))
      } else {
        callback()
      }
    }
    return {
      type: 'text',
      readonlsy: true,
      fullscreenLoading: false,
      dialogVisible: false,
      number: {
        password: ''
      },
      id: '',
      rules: {
        password: [{ required: true, trigger: 'blur', validator: validatePass }]
      }
    }
  },
  methods: {
    elIconView() {
      this.type == 'password' ? (this.type = 'text') : (this.type = 'password')
    },
    focus() {
      this.number.password==''? this.type='password':''
      if(this.number.password!==''&&this.type=='text'){
        this.type = 'text'
      }
    },
    handleClose() {
      this.type = 'text'
      this.dialogVisible = false
      this.$parent.search()
      this.$refs['number'].resetFields()
    },
    openClose(val) {
      this.dialogVisible = true
      this.id = val.id
    },
    /**
     * @method 确定提交
     * **/
    submit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.fullscreenLoading = true
          let code = {
            id: this.id,
            password: this.number.password
          }
          api
            .PUT('/user/resetPassword', 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">
</style>