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

const statusMap: Record<number, { label: string; type: 'success' | 'info' }> = {
  1: { label: '启用', type: 'success' },
  0: { label: '禁用', type: 'info' },
}

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

export function useWarehouseRuleSettingTableColumns(
  options: UseWarehouseRuleSettingTableColumnsOptions = {},
) {
  const columns = computed(() => [
    {
      label: '库存组名称',
      prop: '',
      align: 'left',
    },
    {
      label: '共享库存SPU',
      prop: '',
      align: 'left',
    },
    {
      label: '创建时间',
      prop: 'createTime',
      width: 180,
      align: 'center',
    },
    {
      label: '状态',
      prop: 'status',
      width: 100,
      align: 'center',
      render: (row: WarehouseRuleSettingData) => {
        const status = statusMap[row.status ?? -1]
        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: 'productName',
      minWidth: 160,
      align: 'left',
    },
    {
      label: '操作',
      width: 80,
      align: 'center',
      slot: 'operation',
    },
  ])
  return { columns, spuTableColumns }
}
