<!--
 * @Author: 曹捷
 * @Date: 2019-08-19 15:24:34
 * @LastEditors: 曹捷
 * @LastEditTime: 2021-06-09 10:59:01
 * @Description: file content
 -->
<template>
  <a-modal class="obit-modal-default" v-if="showModal" :maskClosable="false" title="提示" v-model:visible="showModal"
    @cancel="closeDialog" @ok="saveInfo" cancelText="取消" okText="确认" width="40%">
    <a-form :inline="true" :model="formData" :rules="rules" ref="formRef" :label-col="{span: 4}"
      :wrapper-col="{span:18}">
      <a-form-item label="" name="label">
        {{label}}
      </a-form-item>
      <a-form-item label="" name="password">
        <a-input :maxlength="20" show-word-limit v-model:value="formData.password"></a-input>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { computed, inject, reactive, toRefs } from 'vue'
import { util } from '../../../entery'
export default {
  props: {
    open: {
      type: Boolean
    },
    dataInfo: {
      type: Object,
    },
  },
  setup (props, context) {
    let http = inject('http')
    let checkPassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入用户密码'));
      } else {
        if (value.length < 6) {
          callback(new Error('密码长度不能低于6位'));
        } else {
          callback();
        }
      }
    }
    let _data = reactive({
      showModal: props.open,
      formData: {},
      formRef: null,
      rules: {
        password: [
          { required: true, validator: checkPassword, trigger: 'change' }
        ],
      },
    })
    let _computed = {
      label: computed(() => `请输入"${props.dataInfo.userName}"的新密码`)
    }
    let _methods = {
      saveInfo () {
        _data.formRef.validate().then(() => {
          let param = util.util.cloneObj(_data.formData)
          param.userId = props.dataInfo.userId
          http.sysPost('system/user/resetPwd', param).then(res => {
            context.emit('reload')
            _methods.closeDialog()
          })
        })
      },
      closeDialog () {
        util.util.cleanObj(_data.formData)
        context.emit('update:open', false)
      },
    }
    return {
      ..._computed,
      ...toRefs(_data),
      ..._methods
    }
  },
}
</script>
<style lang="less">
</style>