/* eslint-disable prefer-destructuring */
/* eslint-disable no-param-reassign */
import { useUserStore } from '@/store/user'
import { ListFormOption } from '@/types'
import FUserSelect from '@/components/f-user-select/index.vue'

const userStore = useUserStore()

export default {
  api: {
    base: '/sys/group'
  },
  permissionPrefix: 'sys_group_',
  permissions: userStore.permissions,
  tabsProps: {
    autoSwitchToBase: false,
    columns: [
      {
        title: '成员管理',
        slotName: 'userManager',
        permission: 'sys_group_user_manager'
      }
    ]
  },
  columns: [
    {
      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)
    },
    {
      name: '排序值',
      prop: 'sort',
      type: 'number',
      min: 0,
      max: 9999999,
      precision: 0
    }
  ]
} as ListFormOption
