<script setup lang="ts">
import AForm from '@/components/AForm/index.vue'
import DeptTreeSelect from '@/views/components/DeptTreeSelect/index.vue'
import { FormConfigType } from '@/components/AForm/type'
import { ToolbarConfigType } from '@/components/AToolbar/type'
import { ColumnConfigType } from '@/components/ATable/type'
import { StrAnyObj, StrAnyObjArr } from '@/typings'
import { useHandleData } from '@/utils/useHandleData'
import { getTreeApi, getDetailApi, addApi, editApi, deleteApi } from '@/api/system/dept'

const queryRef = ref<InstanceType<typeof AForm>>()
const formRef = ref<InstanceType<typeof AForm>>()
const parentIdRef = ref<InstanceType<typeof DeptTreeSelect>>()

const showQuery = ref<boolean>(true)
const expandAll = ref<boolean>(true)

const queryData = ref<StrAnyObj>({})
const tableData = ref<StrAnyObjArr>([])
const formData = ref<StrAnyObj>({ status: 1, sort: 1 })

const dialogTitle = ref<string>('')
const dialogVisible = ref<boolean>(false)

const queryConfig: FormConfigType[] = [
  {
    type: 'input',
    prop: 'name',
    label: '部门名称'
  },
  {
    type: 'input',
    prop: 'code',
    label: '部门编码'
  },
  {
    type: 'select',
    prop: 'status',
    label: '状态',
    dict: 'enable_disable'
  }
]

const toolbarConfig: ToolbarConfigType[] = [
  {
    common: 'search',
    click() {
      queryData.value.pageNum = 1
      getTree()
    }
  },
  {
    common: 'reset',
    click() {
      queryRef.value?.resetFields()
      getTree()
    }
  },
  {
    common: 'add',
    permissions: 'sysDept:add',
    click() {
      dialogVisible.value = true
      dialogTitle.value = '新增'
      formData.value.parentId = '0'
      nextTick(() => parentIdRef.value?.load())
    }
  },
  {
    text: '展开/折叠',
    icon: 'sort',
    type: 'info',
    click: () => {
      expandAll.value = !expandAll.value
    }
  }
]

const columnConfig: ColumnConfigType[] = [
  {
    prop: 'name',
    label: '部门名称'
  },
  {
    prop: 'code',
    label: '部门编码'
  },
  {
    prop: 'status',
    label: '状态',
    dict: 'enable_disable'
  },
  {
    prop: 'sort',
    label: '排序'
  },
  {
    prop: 'remark',
    label: '备注',
    showOverflowTooltip: true
  },
  {
    width: 250,
    handleConfig: [
      {
        common: 'add',
        permissions: 'sysDept:add',
        click(row) {
          dialogVisible.value = true
          dialogTitle.value = '新增'
          formData.value.parentId = row.id
          nextTick(() => parentIdRef.value?.load())
        }
      },
      {
        common: 'update',
        permissions: 'sysDept:edit',
        click(row) {
          dialogVisible.value = true
          dialogTitle.value = '编辑'
          getDetailApi({ id: row.id }).then((resp: StrAnyObj) => {
            formData.value = resp
          })
          nextTick(() => parentIdRef.value?.load(row.id))
        }
      },
      {
        common: 'delete',
        permissions: 'sysDept:delete',
        click(row) {
          handleRemove(row.id)
        }
      }
    ]
  }
]

const formConfig: FormConfigType[] = [
  {
    type: 'slot',
    prop: 'parentId',
    label: '上级部门',
    rule: [{ required: true, message: '上级部门不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'name',
    label: '部门名称',
    rule: [{ required: true, message: '部门名称不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'code',
    label: '部门编码',
    disabled() {
      return formData.value.id !== undefined
    },
    rule: [{ required: true, message: '部门编码不能为空', trigger: 'blur' }]
  },
  {
    type: 'radio',
    prop: 'status',
    label: '状态',
    dict: 'enable_disable',
    rule: [{ required: true, message: '状态不能为空', trigger: 'blur' }]
  },
  {
    type: 'inputNumber',
    prop: 'sort',
    label: '排序',
    min: 0,
    precision: 0,
    rule: [{ required: true, message: '排序不能为空', trigger: 'blur' }]
  },
  {
    type: 'input',
    prop: 'remark',
    label: '备注',
    itemType: 'textarea',
    rows: 5
  }
]

onMounted(() => {
  getTree()
})

function getTree() {
  getTreeApi(queryData.value).then((resp) => {
    tableData.value = resp
  })
}

function formSubmit() {
  formRef.value?.validate(() => {
    if (formData.value.id) {
      editApi(formData.value).then(() => {
        dialogVisible.value = false
        ElMessage.success('修改成功')
        getTree()
      })
    } else {
      addApi(formData.value).then(() => {
        dialogVisible.value = false
        ElMessage.success('新增成功')
        getTree()
      })
    }
  })
}

function formClosed() {
  formRef.value?.resetFields()
}

function handleRemove(id: string) {
  useHandleData('是否确认删除？', () => {
    deleteApi({ id }).then(() => {
      ElMessage.success('删除成功')
      getTree()
    })
  })
}
</script>

<template>
  <div>
    <el-card v-if="showQuery">
      <a-form ref="queryRef" v-model="queryData" :config="queryConfig" :span="6"> </a-form>
    </el-card>

    <a-table
      :data="tableData"
      :toolbar-config="toolbarConfig"
      :column-config="columnConfig"
      :expand-all="expandAll"
      v-model:showQuery="showQuery"
    >
    </a-table>

    <a-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      @submit="formSubmit"
      @closed="formClosed"
    >
      <a-form ref="formRef" v-model="formData" :config="formConfig" :span="24">
        <template #parentId>
          <dept-tree-select ref="parentIdRef" v-model="formData.parentId" show-top />
        </template>
      </a-form>
    </a-dialog>
  </div>
</template>
