<!--
 * @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="roleKey">
        <a-input :maxlength="20" placeholder="请输入权限字符" show-word-limit v-model:value="formData.roleKey"></a-input>
      </a-form-item>
      <a-form-item label="角色顺序" name="roleSort">
        <a-input-number id="inputNumber" v-model:value.number="formData.roleSort" :min="1" :max="999999999999999"
          style="width: 150px;" />
      </a-form-item>
      <a-form-item label="状态" name="status">
        <obit-dict dictCode="sys_normal_disable" type="radio" placeholder="请选择" v-model:value="formData.status">
        </obit-dict>
      </a-form-item>
      <a-form-item label="备注" name="remark">
        <a-textarea :maxlength="100" placeholder="请输入备注" show-word-limit v-model:value="formData.remark">
        </a-textarea>
      </a-form-item>
    </a-form>
    <p class="title bold">菜单权限</p>
    <bindRolePermissVue ref="rolePermiss" @save="closeDialog" :treeData="treeData" :pData="pData"
      :selectedKeys="selectedKeys">
    </bindRolePermissVue>
  </a-modal>
</template>
<script>
import { computed, inject, reactive, toRefs, watch, toRaw } 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,
    },
    appId: {
      type: String
    },
  },
  setup (props, context) {
    let http = inject('http')
    let _data = reactive({
      showModal: props.open,
      formData: {},
      formRef: null,
      rolePermiss: null,
      treeData: [],
      selectedKeys: [],
      pData: { children: 'children', title: 'label', key: 'id' },
      rules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
        ],
        roleKey: [
          { required: true, message: '请输入权限字符', trigger: 'blur' },
        ],
        roleSort: [
          { required: true, message: '请输入角色顺序', trigger: 'blur', type: 'number' },
        ]
      },
      checkRole: ''
    })
    let _computed = {
      showTitle: computed(() => props.addType === 'add' ? '添加角色' : '修改角色')
    }
    let _methods = {
      saveInfo () {
        _data.formRef.validate().then(() => {
          _data.formData.menuIds = []
          let arr = util.util.cloneObj(toRaw(_data.rolePermiss.checkedKeys))
          if (arr.checked) {
            _data.formData.menuIds = arr.checked
          } else {
            _data.formData.menuIds = arr
          }
          _data.formData.appId = props.appId
          let data = util.util.cloneObj(_data.formData)
          http.saveAppRole(data, props.addType).then((res) => {
            _methods.closeDialog()
          })
        })
      },
      closeDialog () {
        context.emit('reload')
        util.util.cleanObj(_data.formData)
        context.emit('update:open', false)
      },
      getSystemMenuTree () {
        let param = { appId: props.appId }
        http.getSystemMenuTree(param).then(res => {
          let datas = util.util.cloneObj(res)
          _data.treeData = datas
        })
      },
      sysnRolePermission () {
        _data.selectedKeys = []
        if (!props.detailsInfo.roleId) return
        let param = props.detailsInfo.roleId
        http.getAppRolePermission(param).then(res => {
          if (res) {
            res.checkedKeys.forEach(element => {
              _data.selectedKeys.push(element.toString())
            })
          }
        })
      }
    }
    watch(() => props.open, (value) => {
      _methods.getSystemMenuTree()
      if (value && props.addType === 'edit') {
        _data.formData = {}
        let param = props.detailsInfo.roleId
        http.getAppRoleDetail(param).then(res => {
          _data.formData = util.util.cloneObj(res)
        })
        _methods.sysnRolePermission()
      } else {
        _data.selectedKeys = []
        _data.formData = {}
        _data.formData.status = '0'
      }
    })
    return {
      ..._computed,
      ...toRefs(_data),
      ..._methods
    }
  },
}
</script>
<style lang="less">
</style>