<!--
 * @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="showTitle" v-model:visible="showModal"
    @cancel="closeDialog" @ok="saveInfo" cancelText="取消" okText="确认" width="40%">
    <a-tooltip class="hover-operator  ">
      <p style="color:red">新增用户初始默认密码为123456，请用户自行登录后及时修改密码。</p>
    </a-tooltip>
    <a-form :inline="true" :model="formData" :rules="rules" ref="formRef" :label-col="{span: 4}"
      :wrapper-col="{span:18}">
      <a-form-item label="账号" name="account">
        <a-input :maxlength="10" placeholder="请输入账号" :disabled="addType ==='edit'" show-word-limit
          v-model:value="formData.account"></a-input>
      </a-form-item>
      <a-form-item label="用户名称" name="name">
        <a-input :maxlength="10" placeholder="请输入用户名称" show-word-limit v-model:value="formData.name"></a-input>
      </a-form-item>
      <a-form-item label="用户类型" name="userType">
        <obit-dict dictCode="userType" placeholder="请选择用户类型" v-model:value="formData.userType"></obit-dict>
      </a-form-item>
      <a-form-item label="组织机构" name="orgId">
        <obitOrg v-model:value="formData.orgId" placeholder="请选择组织机构"></obitOrg>
      </a-form-item>
      <a-form-item label="绑定角色">
        <obit-select-url type="select" multiple v-model:value="formData.roleIds" :itemInfo="{
          valuename: 'roleId',
          labelname: 'roleName',
          ajaxurl: '/manager/role/getList',
        }" placeholder="请绑定角色"></obit-select-url>
      </a-form-item>
      <a-form-item label="联系方式">
        <a-input :maxlength="11" placeholder="请输入联系方式" show-word-limit v-model:value="formData.telphone"></a-input>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { computed, inject, reactive, ref, toRefs, watch } from 'vue'
import { util } from '../../../entery'
import obitOrg from '../../../components/obit-org/obit-org.vue'
export default {
  // inheritAttrs: false,
  components: { obitOrg },
  props: {
    open: {
      type: Boolean
    },
    addType: {
      type: String,
    },
    detailsInfo: {
      type: Object,
    },
  },
  setup (props, context) {
    let http = inject('http')
    let _data = reactive({
      showModal: props.showModal,
      formData: {},
      formRef: null,
      rules: {
        account: [
          { required: true, message: '请输入用户登录账号', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入用户名称', trigger: 'blur' }
        ],
        userType: [
          { required: true, message: '请选择用户类型', trigger: 'change' }
        ],
        orgId: [
          { required: true, message: '请选择组织机构', trigger: 'change' ,type:'number'}
        ]
      },
    })
    let _computed = {
      showTitle: computed(() => props.addType === 'add' ? '新增用户' : '修改用户')
    }
    let _methods = {
      saveInfo () {
        _data.formRef.validate().then(() => {
          let param = util.util.cloneObj(_data.formData)
          http.saveSystemUserInfo(param).then((res) => {
            context.emit('reload')
            _methods.closeDialog()
          })
        })
      },
      closeDialog () {
        util.util.cleanObj(_data.formData)
        context.emit('update:open', false)
      }
    }
    watch(() => props.open, (value) => {
      if (value && props.addType === 'edit') {
        _data.formData = util.util.cloneObj(props.detailsInfo)
      } else {
        util.util.cleanObj(_data.formData)
      }
    })
    return {
      ..._computed,
      ...toRefs(_data),
      ..._methods
    }
  },
}
</script>
<style lang="less">
</style>