import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MesWmTransactionApi } from '#/api/mes/wm/transaction';

import { markRaw } from 'vue';

import { MdmItemSelect } from '#/views/basicData/mdm/components';
import { WmWarehouseSelect } from '#/views/wls/warehouse/components';

/** 业务类型选项 */
const bizTypeOptions = [
  { label: '种子质检', value: 5 },
  { label: '盘点调整', value: 107 },
  { label: '自定义入库', value: 126 },
  { label: '自定义出库', value: 127 },
  { label: '采购入库', value: 110 },
  { label: '采购入库取消', value: 111 },
  { label: '销售出库', value: 119 },
  { label: '调拨出库', value: 112 },
  { label: '调拨入库', value: 113 },
  { label: '其他入库', value: 115 },
  { label: '其他出库', value: 114 },
  { label: '生产领料', value: 116 },
  { label: '生产退料', value: 117 },
  { label: '生产入库', value: 118 },
  { label: '生产消耗', value: 123 },
  { label: '产品产出', value: 124 },
  { label: '供应商退货', value: 125 },
  { label: '销售退货', value: 120 },
  { label: '外协发料', value: 121 },
  { label: '外协入库', value: 122 },
];

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'itemId',
      label: '物料',
      component: markRaw(MdmItemSelect),
      componentProps: {
        placeholder: '请选择物料',
      },
    },
    {
      fieldName: 'batchCode',
      label: '批次号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入批次号',
      },
    },
    {
      fieldName: 'warehouseId',
      label: '仓库',
      component: markRaw(WmWarehouseSelect),
      componentProps: {
        placeholder: '请选择仓库',
      },
    },
    {
      fieldName: 'type',
      label: '事务类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          { label: '入库', value: 1 },
          { label: '出库', value: 2 },
          { label: '调拨移出', value: 3 },
          { label: '调拨移入', value: 4 },
          { label: '盘盈', value: 5 },
          { label: '盘亏', value: 6 },
        ],
        placeholder: '请选择事务类型',
      },
    },
    {
      fieldName: 'bizType',
      label: '业务类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: bizTypeOptions,
        placeholder: '请选择业务类型',
      },
    },
    {
      fieldName: 'bizCode',
      label: '业务单号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入业务单号',
      },
    },
    {
      fieldName: 'transactionTime',
      label: '事务时间',
      component: 'RangePicker',
      componentProps: {
        allowClear: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        showTime: true,
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<MesWmTransactionApi.Transaction>['columns'] {
  return [
    {
      field: 'typeName',
      title: '事务类型',
      width: 80,
      slots: { default: 'typeName' },
    },
    {
      field: 'bizTypeName',
      title: '业务类型',
      width: 100,
    },
    {
      field: 'itemCode',
      title: '物料编码',
      width: 120,
    },
    {
      field: 'itemName',
      title: '物料名称',
      minWidth: 150,
    },
    {
      field: 'unitMeasureName',
      title: '规格',
      minWidth: 120,
    },
    {
      field: 'specification',
      title: '类别',
      minWidth: 120,
    },
    {
      field: 'bizCode',
      title: '业务单号',
      minWidth: 150,
    },
    {
      field: 'batchCode',
      title: '批次号',
      width: 120,
      slots: { default: 'batchCode' },
    },
    {
      field: 'palletCode',
      title: '托盘码',
      minWidth: 150,
    },
    {
      field: 'bagCode',
      title: '袋码',
      minWidth: 180,
    },
    {
      field: 'quantity',
      title: '数量',
      width: 100,
      slots: { default: 'quantity' },
    },
    {
      field: 'warehouseName',
      title: '仓库',
      width: 100,
    },
    {
      field: 'transactionTime',
      title: '事务时间',
      width: 160,
      formatter: 'formatDateTime',
    },
  ];
}
