import React, { useEffect } from 'react';
import { Select } from 'antd';
import { useModel, useRequest } from '@umijs/max';
import { querySupplierList } from '@/services/contract';

const { Option } = Select;

function ManufacturerField(props) {
  const {
    initialState: { user },
  } = useModel('@@initialState');
  const { value, disabled = false, onChange } = props;
  //供应商列表
  const {
    data: option = [],
    loading,
    run,
  } = useRequest((data) => querySupplierList(data), {
    manual: true,
    formatResult: (res) => {
      return res?.data?.list
        ? res?.data.list.map((item) => {
            return { label: item.name, value: item.id };
          })
        : [];
    },
  });
  useEffect(() => {
    run({
      project_id: 1,
      is_super: user?.IsSuper,
      created_by: user?.CName,
      page_size: 99999,
    });
  }, []);
  return (
    <Select
      showSearch
      loading={loading}
      style={{ width: '100%' }}
      disabled={disabled}
      defaultValue={value ? Number(value) : undefined}
      onChange={(value) => {
        onChange(String(value));
      }}
      filterOption={(input, option) =>
        (option?.label ?? '').toLowerCase().includes(input.toLowerCase())
      }
      options={option}
    ></Select>
  );
}

export default ManufacturerField;
