import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { selectAllSysStation } from '#/api/system/infoEntry/stationInfo/stationInfo';
import { getDictOptions } from '#/utils/dict';

export const queryFormSchema: FormSchemaGetter = () => [
  {
    component: 'ApiSelect',
    fieldName: 'stationId',
    label: '油站',
    componentProps: {
      placeholder: '请选择油站',
      api: async () => {
        const resp = await selectAllSysStation();
        const data = resp || [];
        return Array.isArray(data)
          ? data.map((item: any) => ({
              label: item.stationName,
              value: item.id.toString(),
            }))
          : [];
      },
    },
  },
  {
    component: 'Select',
    fieldName: 'gunNo',
    label: '油枪编号',
    componentProps: {
      placeholder: '请选择油枪编号',
      options: getDictOptions('oil_gun'),
    },
  },
  {
    component: 'Select',
    fieldName: 'oilType',
    label: '油品种类',
    componentProps: {
      placeholder: '请选择油品种类',
      options: getDictOptions('oil_product'),
    },
  },
];

export const tableColumns: VxeGridProps['columns'] = [
  {
    type: 'checkbox',
    width: 80,
  },
  {
    field: 'gunNo',
    title: '油枪编号',
    minWidth: 120,
    formatter: (params: { cellValue: string; column: any; row: any }) => {
      const options = getDictOptions('oil_gun');
      const option = options.find((item) => item.value === params.cellValue);
      return option ? option.label : params.cellValue;
    },
  },
  {
    field: 'oilTypeName',
    title: '油品种类',
    minWidth: 120,
  },
  {
    field: 'gunName',
    title: '油枪名称',
    minWidth: 120,
  },
  {
    field: 'stationName',
    title: '油站名称',
    minWidth: 150,
  },
  {
    field: 'remark',
    title: '备注',
    minWidth: 150,
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 150,
  },
];

export const drawerFormSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    fieldName: 'id',
  },
  {
    component: 'ApiSelect',
    fieldName: 'stationId',
    label: '油站',
    componentProps: {
      placeholder: '请选择油站',
      api: async () => {
        const resp = await selectAllSysStation();
        const data = resp || [];
        return Array.isArray(data)
          ? data.map((item: any) => ({
              label: item.stationName,
              value: item.id.toString(),
            }))
          : [];
      },
    },
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'gunNo',
    label: '油枪编号',
    componentProps: {
      placeholder: '请输入油枪编号',
    },
    rules: 'required',
  },
  {
    component: 'ApiSelect',
    fieldName: 'gunName',
    label: '油枪名称',
    componentProps: {
      placeholder: '请选择油枪名称',
      options: getDictOptions('oil_gun').map((item: any) => ({
        label: item.label,
        value: item.value.toString(),
      })),
    },
  },
  {
    component: 'ApiSelect',
    fieldName: 'oilType',
    label: '油品种类',
    componentProps: {
      placeholder: '请选择油品种类',
      options: getDictOptions('oil_product').map((item: any) => ({
        label: item.label,
        value: item.value.toString(),
      })),
    },
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'remark',
    label: '备注',
    componentProps: {
      placeholder: '请输入备注',
      maxlength: 200,
      type: 'textarea',
      showWordLimit: true,
    },
  },
];
