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

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { getItemTypeSimpleList } from '#/api/mes/md/item/type';

/** 物料选择弹窗搜索表单 */
export function useMdmItemSelectGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '物料编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入物料编码',
      },
    },
    {
      fieldName: 'name',
      label: '物料名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入物料名称',
      },
    },
    {
      fieldName: 'categoryId',
      label: '物料分类',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择物料分类',
        allowClear: true,
        api: async () => {
          const res = await getItemTypeSimpleList();
          return res || [];
        },
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        placeholder: '请选择状态',
        allowClear: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
      },
    },
  ];
}

/** 物料选择弹窗列表字段 */
export function useMdmItemSelectGridColumns(
  multiple = true,
): VxeTableGridOptions<MdmItemApi.Item>['columns'] {
  return [
    { type: multiple ? 'checkbox' : 'radio', width: 50 },
    {
      field: 'code',
      title: '物料编码',
      minWidth: 120,
    },
    {
      field: 'name',
      title: '物料名称',
      minWidth: 180,
      align: 'left',
    },
    {
      field: 'specification',
      title: '规格型号',
      minWidth: 150,
      align: 'left',
    },
    {
      field: 'categoryId',
      title: '物料分类',
      minWidth: 120,
      align: 'center',
      slots: { default: 'categoryId' },
    },
    {
      field: 'unitMeasureName',
      title: '单位',
      minWidth: 80,
      align: 'center',
    },
    {
      field: 'isBatchManaged',
      title: '批次管理',
      minWidth: 80,
      align: 'center',
      slots: { default: 'isBatchManaged' },
    },
    {
      field: 'warehouseName',
      title: '默认仓库',
      minWidth: 100,
      align: 'center',
    },
  ];
}
