import { ComputedRef } from 'vue'
import { TableOption } from '@/types'
import { useUserStore } from '@/store/user'
import { SysDict } from '@/entity/upms'

export default (dict: ComputedRef<SysDict>) => {
  const userStore = useUserStore()

  const isSys = computed(() => userStore.user.claims.platformType === 'sys')

  const hasEditable = (record: any) => {
    if (isSys.value) {
      return true
    }
    return record && record.editable
  }

  const option: TableOption = {
    api: {
      base: '/sys/dict/item'
    },
    permissionPrefix: 'sys_dict_',
    permissions: userStore.permissions,
    addBtn: {
      onBefore: () => {
        return {
          dictId: dict.value.id,
          editable: true
        }
      }
    },
    delBtn: {
      display: hasEditable
    },
    columns: [
      {
        name: '字典标签',
        prop: 'label',
        disabled: (record: any) => !hasEditable(record),
        rules: [
          { required: true, message: '名称不能为空' },
          { maxLength: 50, message: '不能超过50个字符' }
        ]
      },
      {
        name: '字典值',
        prop: 'value',
        disabled: (record: any) => !hasEditable(record),
        rules: [
          { required: true, message: '名称不能为空' },
          { maxLength: 50, message: '不能超过50个字符' }
        ]
      },
      {
        name: '描述',
        prop: 'description',
        disabled: (record: any) => !hasEditable(record)
      },
      {
        name: '是否禁用',
        prop: 'disabled',
        value: false,
        type: 'switch'
      },
      {
        name: '是否可编辑',
        prop: 'editable',
        type: 'switch',
        value: true,
        addDisplay: () => isSys.value,
        editDisplay: () => isSys.value
      },
      {
        name: '排序值',
        prop: 'sort',
        type: 'number'
      }
    ]
  }
  return option
}
