import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MdmItemApi } from '#/api/mdm/item';

import { h } from 'vue';
import { Tag } from 'ant-design-vue';

/** 物料选择弹窗搜索表单 */
export function useMdmItemSelectGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '物料编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入物料编码',
      },
    },
    {
      fieldName: 'name',
      label: '物料名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入物料名称',
      },
    },
    {
      fieldName: 'itemType',
      label: '物料类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        placeholder: '请选择物料类型',
        options: [
          { label: '原料', value: 1 },
          { label: '半成品', value: 2 },
          { label: '成品', value: 3 },
          { label: '辅料', value: 4 },
        ],
      },
    },
  ];
}

/** 物料类型颜色映射 */
const itemTypeMap: Record<number, { label: string; color: string }> = {
  1: { label: '原料', color: 'green' },
  2: { label: '半成品', color: 'orange' },
  3: { label: '成品', color: 'blue' },
  4: { label: '辅料', color: 'purple' },
};

/** 物料选择弹窗列表字段 */
export function useMdmItemSelectGridColumns(
  multiple = true,
): VxeTableGridOptions<MdmItemApi.Item>['columns'] {
  return [
    { type: multiple ? 'checkbox' : 'radio', width: 50 },
    {
      field: 'code',
      title: '物料编码',
      width: 180,
    },
    {
      field: 'name',
      title: '物料名称',
      minWidth: 160,
      align: 'left',
    },
    {
      field: 'specification',
      title: '规格型号',
      minWidth: 140,
      align: 'left',
    },
    {
      field: 'unitMeasureName',
      title: '单位',
      width: 90,
      align: 'center',
    },
    {
      field: 'itemType',
      title: '物料类型',
      width: 110,
      align: 'center',
      formatter: ({ cellValue }: { cellValue: number }) => {
        const type = itemTypeMap[cellValue];
        return type ? type.label : '-';
      },
    },
    {
      field: 'isBatchManaged',
      title: '批次管理',
      width: 110,
      align: 'center',
      cellRender: {
        name: 'CellDict',
        props: { type: 'infra_boolean_string' },
      },
    },
    {
      field: 'createTime',
      title: '创建时间',
      width: 180,
      formatter: 'formatDateTime',
    },
  ];
}
