<!--
 * @Author: 曹捷
 * @Date: 2019-08-19 15:24:34
 * @LastEditors: 曹捷
 * @LastEditTime: 2021-05-18 16:45:41
 * @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-form :inline="true" :model="formData" :rules="rules" ref="formRef" :label-col="{span: 4}"
      :wrapper-col="{span:18}">
      <a-form-item label="角色名称" name="roleName">
        <a-input :maxlength="20" placeholder="请输入角色名称" show-word-limit v-model:value="formData.roleName"></a-input>
      </a-form-item>
      <a-form-item label="角色编码" name="roleCode">
        <a-input :maxlength="20" placeholder="请输入角色编码" show-word-limit v-model:value="formData.roleCode"></a-input>
      </a-form-item>
    </a-form>
    <p class="title bold">设置权限</p>
    <bindRolePermissVue ref="rolePermiss" @save="closeDialog" :checkRole="detailsInfo.roleId || checkRole">
    </bindRolePermissVue>

  </a-modal>
</template>
<script>
import { computed, inject, nextTick, reactive, ref, toRefs, watch } from 'vue'
import bindRolePermissVue from './bindRolePermiss.vue'
import { util } from '../../../entery'
export default {
  components: { bindRolePermissVue },
  props: {
    open: {
      type: Boolean
    },
    addType: {
      type: String,
    },
    detailsInfo: {
      type: Object,
    },
  },
  setup (props, context) {
    let http = inject('http')
    let _data = reactive({
      showModal: props.open,
      formData: {},
      formRef: null,
      rolePermiss: null,
      rules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
        ],
        roleCode: [
          { required: true, message: '请输入角色编码', trigger: 'blur' },
        ]
      },
      checkRole: ''
    })
    let _computed = {
      showTitle: computed(() => props.addType === 'add' ? '新增角色' : '修改角色')
    }
    let _methods = {
      saveInfo () {
        _data.formRef.validate().then(() => {
          let param = util.util.cloneObj(_data.formData)
          http.saveAppRole(param).then((res) => {
            if (props.addType === 'add') {
              _data.checkRole = res
            }
            nextTick(() => {
              _data.rolePermiss.submitRole()
            })
          })
        })
      },
      closeDialog () {
        context.emit('reload')
        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>