import { useEffect, useState } from 'react';
import { Select } from 'antd';
import { getCooperativeList, getCooperativeDetail } from '@/services/cooperative';

const Option = Select.Option;
export default (props) => {
  const { value, onChange, ...otherProps } = props;

  const [list, setList] = useState([]);

  const searchData = (val) => {
    getCooperativeList({ name: val, pageSize: 9999 }).then((res) => {
      setList(res.records || []);
    });
  };

  const handleSearch = (text) => {
    if (text) {
      searchData(text);
    }
  };

  useEffect(() => {
    if (value) {
      getCooperativeDetail(value).then((res) => {
        setList([res]);
      });
    }
  }, [value]);

  return (
    <Select
      showSearch
      value={value}
      defaultActiveFirstOption={false}
      showArrow={false}
      filterOption={false}
      onSearch={handleSearch}
      onChange={onChange}
      notFoundContent={null}
      {...otherProps}
    >
      {list.map((item) => {
        return (
          <Option key={item.orgId} value={item.orgId}>
            {item.name}
          </Option>
        );
      })}
    </Select>
  );
};
