import React, { useState, useEffect } from 'react';
import { Select } from 'antd';

import request from '../../utils/request'
import { APIBaseURL } from '../../utils/constant'

const { Option } = Select;
const prefix = APIBaseURL

/**
 *
 *
 * @param {*} props
 * @returns
 */
const CitySelect3 = (props) => {
  const [ data, setData ] = useState([])
  console.log('props', props.buildingId)
  useEffect(() => {
    getCityList();
    getValue();
  },[props.buildingId])

  const getCityList = (e) => {
    request({
        url: prefix + 'api/admin/tdCityList/tdCity',
        method: 'GET',
        params: {leveltype: 2, pageNum: 1,pageSize: 999},
        action: 'select',
    }).then((data) => {
        setData(data)
    })
  }

  /**
   * 因为 有个需求是，如果这个城市被删除了，那么就直接展示为空，不能展示 cityId
   */
  const getValue = () => {
    if (props.value) {
      const tempData = data.filter(f => f.id == props.value)
      const va = (tempData.length > 0) ? props.value : undefined
      props.onChange(va)
    }
  }

  return (
      <Select
      {...props}
      showSearch
      value={props.value}
      style={{ width: '300px' }}
      placeholder="请选择城市"
      onChange={props.onChange}
      filterOption={(input, option) =>
        option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
      }
      >
          {data.map(city => (
            <Option key={city.id} value={city.id}>{city.name}</Option>
          ))}
      </Select>
  )
}
export default CitySelect3
