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 SalesBatch = props => {
  const [data, setData] = useState([])
  const [value, setValue] = useState([])
  const disabledType = props.type;
  const buildingId = props.buildingId
  useEffect(() => {
    getSalesBatchList();
  }, [props.buildingId])


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


  const checkValue = (data) => {
    if (props.value) {
      const tempData = data.filter(f => f.salesBatchId == props.value)
      const va = (tempData.length > 0) ? props.value : '项目已下线，请重新选择项目'
      props.onChange(va)

    }
  }

  return (
      <Select
      disabled = {disabledType == 'false' ? true : false}
      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(salesBatch => (
            <Option key={salesBatch.salesBatchId} value={salesBatch.salesBatchId}>{salesBatch.salesBatchName}</Option>
          ))}
      </Select>
  )
}
export default SalesBatch
