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

const { Option } = Select;

function usePrevious(props) {
  const ref = useRef();
  useEffect(() => {
    ref.current = props;
  });
  return ref.current;
}

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


  const getList = e => {
    request({ ...apis.house.apartmentList, params: { pageNum: 1, pageSize: 999, buildingId: props.buildingId} }).then(data => {
        setData(data.records)
        checkValue(data.records)
        // 默认选中第一个
    })
  }


  const checkValue = (data) => {
    if (props.value) {
      const tempData = data.filter(f => f.apartmentId == 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
      }>
          {data.map(apartment => (
            <Option key={apartment.apartmentId} value={apartment.apartmentId}>{apartment.apartmentName}</Option>
          ))}
      </Select>
  )
}
export default SalesBatchSelect
