import { ElButton, ElTag } from 'element-plus'
import type { WarehouseRuleSettingData } from '@/types/api/warehouse/warehouseRuleSetting'

const statusMap: Record<'true' | 'false', { label: string; type: 'success' | 'danger' }> = {
  true: { label: '启用', type: 'success' },
  false: { label: '禁用', type: 'danger' },
}

export interface UseWarehouseRuleSettingTableColumnsOptions {
  onEdit?: (row: WarehouseRuleSettingData) => void
}

export function useWarehouseRuleSettingTableColumns(
  options: UseWarehouseRuleSettingTableColumnsOptions = {},
) {
  const columns = computed(() => [
    {
      label: '库存组名称',
      prop: 'groupName',
      align: 'left',
    },
    {
      label: '共享库存SPU',
      prop: 'warehouseSpu',
      align: 'left',
      render: (row: WarehouseRuleSettingData) => {
        const spuList = [
          ...new Set(
            (row.skuList ?? [])
              .map((item) => item.warehouseSpu)
              .filter(Boolean),
          ),
        ]
        return <span>{spuList.join(',')}</span>
      },
    },
    {
      label: '创建时间',
      prop: 'createTime',
      width: 180,
      align: 'center',
    },
    {
      label: '状态',
      prop: 'status',
      width: 100,
      align: 'center',
      render: (row: WarehouseRuleSettingData) => {
        const status = statusMap[row.status ? 'true' : 'false']
        return status ? (
          <ElTag type={status.type}>{status.label}</ElTag>
        ) : (
          <span>-</span>
        )
      },
    },
    {
      label: '操作',
      width: 100,
      align: 'center',
      render: (row: WarehouseRuleSettingData) => {
        return (
          <div>
            <ElButton
              link
              type="primary"
              onClick={() => options?.onEdit?.(row)}
            >
              编辑
            </ElButton>
          </div>
        )
      },
    },
  ])
  const spuTableColumns = computed(() => [
    {
      label: '扣减顺序',
      width: 90,
      align: 'center',
      slot: 'sortNo',
    },
    {
      label: '图片',
      width: 80,
      align: 'center',
      slot: 'spuImage',
    },
    {
      label: '库存SPU',
      prop: 'warehouseSpu',
      minWidth: 120,
      align: 'left',
    },
    {
      label: '商品名称',
      prop: 'skuName',
      minWidth: 160,
      align: 'left',
    },
    {
      label: '所属仓库',
      prop: 'warehouseName',
      width: 120,
      align: 'center',
    },
    {
      label: '操作',
      width: 80,
      align: 'center',
      slot: 'operation',
    },
  ])
  const spuSearchTableColumns = computed(() => [
    {
      label: '图片',
      width: 80,
      align: 'center',
      slot: 'spuImage',
    },
    {
      label: '库存SPU',
      prop: 'warehouseSpu',
      minWidth: 120,
      align: 'center',
    },
    {
      label: '商品名称',
      prop: 'skuName',
      minWidth: 160,
      align: 'left',
    },
    {
      label: '所属仓库',
      prop: 'warehouseName',
      width: 120,
      align: 'center',
    },
    {
      label: '操作',
      width: 80,
      align: 'center',
      slot: 'operation',
    },
  ])
  return { columns, spuTableColumns, spuSearchTableColumns }
}
