<template>
  <div class="role-edit">
    <el-dialog
      v-bind="$attrs"
      :title="state.dialogTitle"
      v-model="state.modalVisible"
      :before-close="cancelDialog"
      :append-to-body="true"
      :destroy-on-close="true"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :lock-scroll="false"
      class="role-dialog"
      draggable
    >
      <el-form ref="roleRef" :model="state.form" :rules="state.rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="角色名称" prop="title">
              <el-input v-model="state.form.title" placeholder="请输入角色名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="角色描述" prop="details">
              <el-input v-model="state.form.details" type="textarea" :rows="4" placeholder="请输入角色描述" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDialog">确 定</el-button>
          <el-button @click="cancelDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import useSystemManageStore from '@store/modules/systemManage'
import {filterObject} from '@/utils/index'
import {nextTick} from 'vue'

const systemManageStore = useSystemManageStore()
const {proxy} = getCurrentInstance()
const roleRef = ref(null)

const state = reactive({
  modalVisible: false,
  dialogTitle: '',
  editView: {},
  form: {
    title: '',
    details: ''
  },
  rules: {
    title: [{required: true, message: '角色名称不能为空', trigger: 'blur'}]
  }
})

const emits = defineEmits(['refresh'])

const getDataView = async (id) => {
  nextTick(async () => {
    const result = await systemManageStore.sysusergroupDetail({
      ['id.Value']: id
    })
    state.editView = result
    state.form = filterObject(result, Object.keys(state.form))
  })
}

const submitDialog = () => {
  roleRef.value.validate(async (valid) => {
    if (valid) {
      if (Object.keys(state.editView).length) {
        state.form.id = state.editView.id
        state.form['id.value'] = state.editView.id
        await systemManageStore.sysusergroupUp(state.form)
        proxy.$modal.msgSuccess('修改成功！')
      } else {
        await systemManageStore.sysusergroupAdd(state.form)
        proxy.$modal.msgSuccess('新增成功！')
      }
      state.editView = {}
      roleRef.value.resetFields()
      emits('refresh')
      state.modalVisible = false
    }
  })
}

const cancelDialog = () => {
  state.editView = {}
  roleRef.value.resetFields()
  state.modalVisible = false
}

const openDialog = async (data = {}) => {
  if (Object.keys(data).length) await getDataView(data.id)
  state.modalVisible = true
  state.dialogTitle = Object.keys(data).length ? `编辑【${data.title}】` : '新增'
}

defineExpose({
  openDialog
})
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
