import { useEffect, useState } from 'react';
import { Select } from 'antd'
import { getDetail } from '@/services/org'
import { getList } from '@/services/org'

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

  const showType = 0; // 不显示报错

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

  const searchData = (val) => { 
    getList({ pageSize: 999,orgName:val }, showType).then(res => {
      setList(res.records || [])
    })    
  }

  const handleSearch = (text) => {
    if (text) {
      searchData(text)
    }else{
      getList({ pageSize: 999 }, showType).then(res => {
        setList(res.records || [])
      })
    }
  }

  useEffect(() => {
    if (value) {
      getDetail(value).then((res) => {
        setList([res])
      })
    }else{
      getList({ pageSize: 999 }, showType).then(res => {
        setList(res.records || [])
      })
    }
  }, [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.orgName}</Option>
          )
        })
      }
    </Select>
  )
}
