import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { ErpWarehouseMappingApi } from '#/api/erp/warehouseMapping';

import { getWarehouseSimpleList as getErpWarehouseSimpleList } from '#/api/erp/stock/warehouse';
import { getWarehouseSimpleList as getMesWarehouseSimpleList } from '#/api/mes/wm/warehouse';

/** 状态选项 */
export const STATUS_OPTIONS = [
  { label: '启用', value: 1 },
  { label: '禁用', value: 0 },
];

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'erpWarehouseId',
      label: 'ERP仓库',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getErpWarehouseSimpleList,
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择ERP仓库',
      },
    },
    {
      fieldName: 'mesWarehouseId',
      label: 'MES仓库',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getMesWarehouseSimpleList,
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择MES仓库',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: STATUS_OPTIONS,
        placeholder: '请选择状态',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<ErpWarehouseMappingApi.WarehouseMapping>['columns'] {
  return [
    {
      field: 'erpWarehouseName',
      title: 'ERP仓库',
      minWidth: 160,
    },
    {
      field: 'mesWarehouseName',
      title: 'MES仓库',
      minWidth: 160,
    },
    {
      field: 'status',
      title: '状态',
      width: 100,
      align: 'center',
      cellRender: {
        name: 'CellTag',
        props: {
          options: STATUS_OPTIONS,
          colorKeyMap: {
            0: 'error',
            1: 'success',
          },
        },
      },
    },
    {
      field: 'createTime',
      title: '创建时间',
      width: 180,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 160,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'erpWarehouseId',
      label: 'ERP仓库',
      component: 'ApiSelect',
      componentProps: {
        api: getErpWarehouseSimpleList,
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择ERP仓库',
      },
      rules: 'required',
    },
    {
      fieldName: 'mesWarehouseId',
      label: 'MES仓库',
      component: 'ApiSelect',
      componentProps: {
        api: getMesWarehouseSimpleList,
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择MES仓库',
      },
      rules: 'required',
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      defaultValue: 1,
      componentProps: {
        options: STATUS_OPTIONS,
      },
    },
  ];
}
