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 ActivityType = props => {
  const [data, setData] = useState([])
  const [value, setValue] = useState([])
  console.log('props', props);
  useEffect(() => {
    getBuildList();
  }, [props.dynamicTypeId])


  const getBuildList = e => {
    request({ ...apis.dynamicType.list, 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.dynamicTypeId == props.value)
      const va = (tempData.length > 0) ? props.value : '项目已下线，请重新选择项目'
      props.onChange(va)

    }
  }

  const onChange = (dynamicTypeId) => {
    const type = data.filter(x => dynamicTypeId === x.dynamicTypeId)[0]

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

    // }={props.onChange}building.buildingId
  }

  return (
    <Select
      showSearch
      value={props.value}
      style={{ width: '300px' }}
      placeholder="请选择项目"
      onChange={ onChange}
      filterOption={(input, option) =>
        option.props.children && option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
      }>
      {data.map(type => (
        <Option key={type.dynamicTypeId} value={type.dynamicTypeId}>{type.dynamicTypeName}</Option>
      ))}
    </Select>
  )
}
export default ActivityType
