/* eslint-disable prefer-destructuring */
/* eslint-disable no-param-reassign */
import { IconUserGroup } from '@arco-design/web-vue/es/icon'
import { useUserStore } from '@/store/user'
import axios from '@/utils/axios'
import { ListFormOption } from '@/types'
import FUserSelect from '@/components/f-user-select/index.vue'

const userStore = useUserStore()
export default {
  api: {
    base: '/sys/dept',
    edit: '/edit/new',
    fetch: '/fetch/with/drag'
  },
  isTree: true,
  addBtn: {
    onBefore: (form: any): any => {
      return {
        parentId: form ? form.id : '0'
      }
    }
  },
  editBtn: {
    display: (form: any) => form.id !== '0'
  },
  delBtn: {
    display: (form: any) => form.id !== '0'
  },
  permissionPrefix: 'sys_dept_',
  permissions: userStore.permissions,
  tabsProps: {
    autoSwitchToBase: false,
    columns: [
      {
        title: '成员管理',
        slotName: 'deptUserManager',
        permission: 'sys_dept_user_manager'
      }
    ]
  },
  treeProps: {
    draggable: true,
    onDrop: (e: any) =>
      axios.post('/sys/dept/drag', {
        dragId: e.dragNode.id,
        dropId: e.dropNode.id,
        dropPosition: e.dropPosition
      }),
    iconRender: (node: any) => {
      if (node.id === '0') {
        return h('div', { class: 'iconfont icon-enterprise dept-tree-icon' })
      }
      return h(IconUserGroup)
    }
  },
  columns: [
    {
      name: '父级部门',
      prop: 'parentId',
      type: 'tree',
      viewDisplay: false,
      dicData: () => axios.post('/sys/dept/fetch/all'),
      props: {
        key: 'id',
        title: 'name',
        icon: 'iconRender'
      },
      rules: [{ required: true, message: '请选择父级部门' }]
    },
    {
      name: '部门名称',
      prop: 'name',
      rules: [
        { required: true, message: '名称不能为空' },
        { minLength: 2, message: '名称不少于2个字符' },
        { maxLength: 50, message: '不能超过50个字符' }
      ]
    },
    {
      name: '主负责人',
      prop: 'primaryChargeUserId',
      type: (form: any) =>
        h(FUserSelect, {
          selectIds: form.primaryChargeUserId ? [form.primaryChargeUserId] : [],
          limit: 1,
          onChange: (val: string[]) => {
            form.primaryChargeUserId = val[0]
            const idx = form.secondaryChargeUserIds.findIndex((item: string) => item === val[0])
            if (idx >= 0) {
              form.secondaryChargeUserIds.splice(idx, 1)
            }
          }
        }),
      formatValue: (record: any) => record.primaryChargeUserName
    },
    {
      name: '副负责人',
      prop: 'secondaryChargeUserIds',
      value: [],
      type: (form: any) =>
        h(FUserSelect, {
          selectIds: form.secondaryChargeUserIds,
          limit: 10,
          onChange: (val: string[]) => {
            form.secondaryChargeUserIds = val
            if (val.findIndex((item) => item === form.primaryChargeUserId) >= 0) {
              form.primaryChargeUserId = undefined
            }
          }
        }),
      formatValue: (record: any) => h('span', record.secondaryChargeUserNames)
    }
  ]
} as ListFormOption
