<template>
  <a-modal
    v-model:visible="visible"
    :title="title"
    :mask-closable="false"
    :esc-to-close="false"
    :width="width >= 500 ? 500 : '100%'"
    draggable
    @before-ok="save"
    @close="reset"
  >
    <GiForm ref="formRef" v-model="form" :columns="columns" />
  </a-modal>
</template>

<script setup lang="ts">
import { Message } from '@arco-design/web-vue'
import { useWindowSize } from '@vueuse/core'
import { mapTree } from 'xe-utils'
import { type DeptResp, type DeptAddReq, type DeptUpdateReq, addDept, getDeptDetail, updateDept } from '@/apis/system/dept'
import { type ColumnItem, GiForm } from '@/components/GiForm'
import { useResetReactive } from '@/hooks'

interface Props {
  depts: DeptResp[]
}
const props = withDefaults(defineProps<Props>(), {
  depts: () => [],
})

const emit = defineEmits<{
  (e: 'save-success'): void
}>()

const { width } = useWindowSize()

const dataId = ref('')
const visible = ref(false)
const isUpdate = computed(() => !!dataId.value)
const title = computed(() => (isUpdate.value ? '修改部门' : '新增部门'))
const formRef = ref<InstanceType<typeof GiForm>>()

// 转换为部门树
const deptSelectTree = computed(() => {
  const data = JSON.parse(JSON.stringify(props.depts)) as DeptResp[]
  return mapTree(data, (i) => ({
    key: i.deptId,
    title: i.deptName,
    children: i.children,
  }))
})

const [form, resetForm] = useResetReactive({
  deptName: '',
  leaderId: '',
  orderNum: 999,
  parentId: '',
  status: 1,
})

const columns: ColumnItem[] = reactive([
  {
    label: '上级部门',
    field: 'parentId',
    type: 'tree-select',
    span: 24,
    props: {
      data: deptSelectTree,
      allowClear: true,
      allowSearch: true,
      fallbackOption: false,
      filterTreeNode(searchKey, nodeData) {
        if (nodeData.title) {
          return nodeData.title.toLowerCase().includes(searchKey.toLowerCase())
        }
        return false
      },
    },
    rules: [{ required: false, message: '请选择上级部门' }],
    hide: (form) => {
      return form.parentId === 0
    },
  },
  {
    label: '部门名称',
    field: 'deptName',
    type: 'input',
    span: 24,
    props: {
      maxLength: 30,
    },
    rules: [{ required: true, message: '请输入部门名称' }],
  },
  {
    label: '负责人ID',
    field: 'leaderId',
    type: 'input',
    span: 24,
    props: {
      maxLength: 50,
    },
  },
  {
    label: '排序',
    field: 'orderNum',
    type: 'input-number',
    span: 24,
    props: {
      min: 1,
      mode: 'button',
    },
  },
  {
    label: '状态',
    field: 'status',
    type: 'switch',
    span: 24,
    props: {
      type: 'round',
      checkedValue: 1,
      uncheckedValue: 2,
      checkedText: '启用',
      uncheckedText: '禁用',
    },
  },
])

// 重置
const reset = () => {
  formRef.value?.formRef?.resetFields()
  resetForm()
}

// 保存
const save = async () => {
  try {
    const isInvalid = await formRef.value?.formRef?.validate()
    if (isInvalid) return false
    if (isUpdate.value) {
      await updateDept(dataId.value, form as DeptUpdateReq)
      Message.success('修改成功')
    } else {
      await addDept(form as DeptAddReq)
      Message.success('新增成功')
    }
    emit('save-success')
    return true
  } catch (error) {
    return false
  }
}

// 新增
const onAdd = (parentId?: string) => {
  reset()
  form.parentId = parentId || ''
  dataId.value = ''
  visible.value = true
}

// 修改
const onUpdate = async (deptId: string) => {
  reset()
  dataId.value = deptId
  const { data } = await getDeptDetail(deptId)
  Object.assign(form, data)
  visible.value = true
}

defineExpose({ onAdd, onUpdate })
</script>

<style scoped lang="scss"></style>
