<template>
  <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="40%" v-dialogDrag append-to-body>
    <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="formData.name" disabled />
      </el-form-item>
      <el-form-item label="工号" prop="workNo">
        <el-input v-model="formData.workNo" disabled />
      </el-form-item>
      <el-form-item label="电话" prop="tel">
        <el-input v-model="formData.tel" disabled />
      </el-form-item>
      <el-form-item label="新密码" prop="password">
        <el-input type="password" v-model="formData.password" show-password placeholder="请输入新密码"/>
      </el-form-item>
      <el-form-item label="确认新密码" prop="newPassword">
        <el-input type="password" v-model="formData.newPassword" show-password placeholder="确认新密码"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import * as StaffApi from '@/api/company/staff'

export default {
  name: 'StaffResetPassword',
  data() {
    const validatePassword= async(rule, value, callback) => {
      return this.formData.password === value ? callback() : callback(new Error());
    }
    return {
      // 弹出层标题
      dialogTitle: '重置员工密码',
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      formData: {
        name: undefined,
        workNo: undefined,
        tel: undefined,
        password: undefined,
        newPassword: undefined,
        userId: undefined
      },
      formRules:{
        password: [
          { required: true, message: '登录密码不能为空', trigger: 'blur' },
          { min: 6, max: 20, message: "登录密码长度在6~20个字符", trigger: 'blur'},
          {
            pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+{}\[\]:;<>,.?~\\-])\S{6,}$/,
            message: '登录密码必须由大小写字母、数字、特殊字符组成',
            trigger: 'blur'
          }
        ],
        newPassword: [
          {required: true, message: "登录密码不能为空", trigger: "blur"},
          {validator: validatePassword, message: '再次输入的密码不一致', trigger: 'blur'}
        ]
      },
    }
  },
  methods: {
    /** 表单重置 */
    reset() {
      this.formData = {
        name: undefined,
        workNo: undefined,
        tel: undefined,
        password: undefined,
        newPassword: undefined,
        userId: undefined,
      }
      this.resetForm('formRef')
    },
    /** 打开弹窗 */
    async open(row) {
      this.dialogVisible = true
      this.reset();
      this.formData = row
    },
    /** 提交表单 */
    async submitForm() {
      // 校验主表
      await this.$refs['formRef'].validate()
      this.formLoading = true
      try{
        await StaffApi.resetPassword(this.formData)
        this.$modal.msgSuccess('密码已重置')
        this.dialogVisible = false
        this.$emit('success')
      } finally {
        this.formLoading = false
      }
    },
  }
}
</script>

<style scoped>

</style>
