import type { TableColumn } from '@/components/core/dynamic-table';
import { ContractStatusEnum } from '@/enums/contractEnum';
import { DictEnum } from '@/enums/dictEnum';
import { useDictStore } from '@/store/modules/dict';
import { formatToDate } from '@/utils/dateUtil';
import { Tag } from 'ant-design-vue';

export type TableListItem = API.MaterialsInventoryEntity;
export type TableColumnItem = TableColumn<TableListItem>;
const dictStore = useDictStore();
export const baseColumns: TableColumnItem[] = [
  {
    title: '所属项目',
    width: 180,
    dataIndex: 'project',
    customRender: ({ record }) => {
      return record?.project?.name || '';
    },
  },
  {
    title: '所属公司',
    width: 180,
    dataIndex: 'product',
    customRender: ({ record }) => {
      return record?.product?.company?.name || '';
    },
  },
  {
    title: '产品名称',
    width: 180,
    dataIndex: 'product',
    customRender: ({ record }) => {
      return record?.product?.name || '';
    },
  },
  {
    title: '单位',
    width: 60,
    hideInSearch: true,
    dataIndex: 'unit',
    formItemProps: {
      component: 'Select',
      componentProps: {
        options: dictStore
          .getDictItemsByCode(DictEnum.Unit)
          .map(({ label, id }) => ({ value: id, label })),
      },
    },
    customRender: ({ record }) => {
      return record?.product?.unit?.label || '';
    },
  },
  {
    title: '库存数量',
    hideInSearch: true,
    width: 80,
    dataIndex: 'quantity',
  },
  {
    title: '库存单价',
    hideInSearch: true,
    width: 80,
    dataIndex: 'unitPrice',
    customRender: ({ record }) => {
      return parseFloat(record.unitPrice) || 0;
    },
  },
];

export function formatStatus(status: ContractStatusEnum): {
  color: string;
  label: string;
  value: number;
} {
  switch (status) {
    case ContractStatusEnum.Pending:
      return {
        color: '#ccc',
        label: '待审核',
        value: ContractStatusEnum.Pending,
      };
    case ContractStatusEnum.Approved:
      return {
        color: 'green',
        label: '已通过',
        value: ContractStatusEnum.Approved,
      };
    case ContractStatusEnum.Rejected:
      return {
        color: 'red',
        label: '已拒绝',
        value: ContractStatusEnum.Rejected,
      };
    default:
      return {
        color: '#ccc',
        label: '待审核',
        value: ContractStatusEnum.Pending,
      };
  }
}
