import { useEffect, useState } from 'react'
import Picker from '@/components/Picker'
import { fetch } from '@/utils/request'
import { API_GET_AGENT_BUILDINGS, API_ITEMS_LIST } from '@/constants/api'
import { ROLE_CODE } from '@/constants/user'

export default (props) => {

  const { type = '', change = () => {} } = props
  const [dicts, setDicts] = useState([])

  useEffect(() => {
    if (type === ROLE_CODE.CUSTOMER) {
      fetch({ url: API_ITEMS_LIST, payload: { pageNumber: 1, pageSize: 1000 } }).then((res) => {
        setDicts(res.records || [])
      })
    } else {
      fetch({ url: API_GET_AGENT_BUILDINGS }).then((res) => {
        setDicts(res || [])
      })
    }
  }, [])

  const BuildingChange = (value) => {
    change(value)
  }

  return (
    <Picker kv={['buildingName', 'buildingId']} dicts={dicts} {...props} onChange={BuildingChange} />
  )
}
