import React, { useState, useEffect } from 'react';
import { Select } from 'antd';
import apis from '../../services/apis';
import request from '../../utils/request'

const { Option } = Select;


/**
 *
 *
 * @param {*} props
 * @returns
 */
const SelectPolicyType = props => {
  const [data, setData] = useState([])
//   const [value, setValue] = useState([])
  useEffect(() => {
    getBuildList();
  }, [])

  const getBuildList = e => {
    request({ ...apis.system.getPolicyTypeList, params: { pageNum: 1, pageSize: 999 } }).then(data => {
        setData(data.records)
        checkValue(data.records)
        // 默认选中第一个
    })
  }


  const checkValue = (data) => {
    if (props.value) {
      const tempData = data.filter(f => f.institutionId == props.value)
      const va = (tempData.length > 0) ? props.value : '类型已删除，请重新选择类型'
      props.onChange(va)

    }
  }

  return (
      <Select
      showSearch
      value={props.value}
      style={{ width: '300px' }}
      placeholder="请选择项目"
      onChange={props.onChange}
      filterOption={(input, option) =>
        option.props.children && option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
      }>
          {/* <Option key="" value="">全部项目</Option> */}
          {data.map(building => (
            <Option key={building.policyTypeId} value={building.policyTypeId}>{building.policyTypeName}</Option>
          ))}
      </Select>
  )
}
export default SelectPolicyType
