<route lang="yaml">
meta:
  enabled: false
</route>

<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { createGroup, getGroupById, updateGroup } from '@/api/system/groupApi'
const emit = defineEmits(['success'])

const formRef = ref<FormInstance>()
const formRules = ref<FormRules>({
  groupName: [
    { required: true, message: '请输入标题', trigger: 'blur' },
  ],
  roleDesc: [
    { required: false, message: '请输入描述', trigger: 'blur' },
  ],
})

const state = reactive({
  title: '',
  dialogVisible: false,
  loading: false,
  groupParent: undefined,
  formData: {
    groupId: 0,
    groupName: '',
    parentId: 0,
    parentName: '',
    groupDesc: '',
    groupCatId: 0,
  },
  actionType: 1,
})

onMounted(() => {})

const getData = async (groupId: number) => {
  state.loading = true
  const { data } = await getGroupById(groupId)
  state.loading = false
  state.formData = data
}

function onSubmit() {
  formRef.value && formRef.value.validate(async (valid) => {
    if (valid) {
      if (state.actionType === 1) {
        const { msg } = await createGroup(state.formData)
        ElMessage.success(msg)
        state.dialogVisible = false
        emit('success')
      }
      else {
        const { msg } = await updateGroup(state.formData)
        ElMessage.success(msg)
        state.dialogVisible = false
        emit('success')
      }
    }
  })
}

function onCancel() {
  state.dialogVisible = false
}

function resetFormData() {
  state.formData = {
    groupId: 0,
    groupName: '',
    groupDesc: '',
    parentId: 0,
    parentName: '',
    groupCatId: 0,
  }
}

defineExpose({
  showAddModel(parentId: number, parentName: string) {
    resetFormData()
    state.title = '新增部门'
    state.actionType = 1
    state.dialogVisible = true
    state.formData.parentId = parentId
    state.formData.parentName = parentName
  },
  showEditModel(groupId: number) {
    resetFormData()
    state.title = '编辑部门'
    state.actionType = 2
    state.dialogVisible = true
    getData(groupId)
  },

})
</script>

<template>
  <el-drawer v-model="state.dialogVisible" :title="state.title" size="30%">
    <el-form ref="formRef" :model="state.formData" :rules="formRules" label-width="120px" label-suffix="：">
      <el-form-item v-if="state.groupParent" label="上级部门">
        <el-input v-model="state.formData.parentName" readonly />
      </el-form-item>

      <el-form-item label="部门名称" prop="groupName">
        <el-input v-model="state.formData.groupName" />
      </el-form-item>

      <el-form-item label="类别" prop="groupCatId">
        <group-cat-selector v-model="state.formData.groupCatId" />
      </el-form-item>

      <el-form-item label="说明">
        <el-input v-model="state.formData.groupDesc" :rows="5" type="textarea" show-word-limit maxlength="50" />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button size="large" @click="onCancel">
        取消
      </el-button>
      <el-button type="primary" size="large" @click="onSubmit">
        保存
      </el-button>
    </template>
  </el-drawer>
</template>
