import Api from '@/api';
import type { FormSchema } from '@/components/core/schema-form/';
import { DictEnum } from '@/enums/dictEnum';
import { useDictStore } from '@/store/modules/dict';
import { debounce } from 'lodash-es';
const { getDictItemsByCode } = useDictStore();
export const formSchemas: FormSchema<API.ProductDto>[] = [
  {
    field: 'name',
    component: 'Input',
    label: '产品名称',
    rules: [{ required: true, type: 'string' }],
    colProps: {
      span: 12,
    },
  },
  {
    field: 'productSpecification',
    component: 'Input',
    label: '产品规格',
    colProps: {
      span: 12,
    },
  },
  {
    label: '单位',
    component: 'Select',
    field: 'unitId',
    colProps: {
      span: 12,
    },
    componentProps: ({ formInstance, schema, formModel }) => ({
      showSearch: true,
      filterOption: (input: string, option: any) => {
        return option.label.indexOf(input) >= 0;
      },
      fieldNames: {
        label: 'label',
        value: 'value',
      },
      options: getDictItemsByCode(DictEnum.Unit).map((item) => ({
        value: item.id,
        label: item.label,
      })),

      getPopupContainer: () => document.body,
      defaultActiveFirstOption: true,
    }),
  },
  {
    field: 'companyId',
    component: 'Select',
    label: '所属公司',
    helpMessage: '如未找到对应公司，请先去公司管理添加公司。',
    componentProps: ({ formInstance, schema }) => ({
      showSearch: true,
      filterOption: false,
      fieldNames: {
        label: 'label',
        value: 'value',
      },
      options: [],
      getPopupContainer: () => document.body,
      defaultActiveFirstOption: true,
      onChange: async (value) => {
        if (!value) {
          formInstance?.setFieldsValue({ companyId: undefined });
          const options = await getCompanyOptions();
          const newSchema = {
            field: schema.field,
            componentProps: {
              options,
            },
          };
          formInstance?.updateSchema([newSchema]);
        }
      },

      request: () => {
        return getCompanyOptions();
      },
      onSearch: debounce(async (keyword) => {
        schema.loading = true;
        const newSchema = {
          field: schema.field,
          componentProps: {
            options: [] as LabelValueOptions,
          },
        };
        formInstance?.updateSchema([newSchema]);
        const options = await getCompanyOptions(keyword).finally(() => (schema.loading = false));
        newSchema.componentProps.options = options;
        formInstance?.updateSchema([newSchema]);
      }, 500),
    }),
  },
  {
    field: 'remark',
    component: 'InputTextArea',
    label: '备注',
    colProps: {
      span: 24,
    },
  },
];

const getCompanyOptions = async (keyword?: string): Promise<LabelValueOptions> => {
  const { items: result } = await Api.company.companyList({ pageSize: 100, name: keyword });
  return (
    result?.map((item) => ({
      label: item.name,
      value: item.id,
    })) || []
  );
};
