import React from 'react'
import { connect as connectFela } from 'react-fela'
import { Field, reduxForm as connectForm } from 'redux-form'
import { pipe, isNil, not, map, isArrayLike, merge } from 'ramda'
import { SelectField } from 'redux-form-material-ui'
import MenuItem from 'material-ui/MenuItem'
import RaisedButton from 'material-ui/RaisedButton'
import { compose, withState, withHandlers } from 'recompose'
import h from 'react-hyperscript'

import { FormattedMessage } from '../../lib/Intl'
import styles from '../styles/SelectAgentForOrder'
import AvatarField from '../../app/components/AvatarField'

const SelectAgentForOrderForm = compose(
  connectFela(styles),
  connectForm({})
)(props => {
  const { agentType, styles, isEditing, toggleEdit, selectAgent, handleSubmit, agentCollection } = props

  return h('form', {
    className: styles.container
  }, [
    h('div', {
      className: styles.innerContainer
    }, [
      h(Field, {
        name: 'selectAgent',
        floatingLabelText: (
          h(FormattedMessage, {
            id: 'agents.labelText',
            values: {
              agentType
            },
            className: styles.labelText
          })
        ),
        component: SelectField,
        onChange: (e, agentId) => {
          selectAgent(agentId)
        }
      }, [
        map((agent) => {
          return h(MenuItem, {
            value: agent.id,
            primaryText: agent.name
          })
        }, agentCollection)
      ]),
      h(RaisedButton, {
        className: styles.button,
        type: 'button',
        onClick: (ev) => {
          console.log('going to create a new agent / relationship')
        }
      }, 'create new')
    ])
  ])
})

const SelectAgentForOrder = (props) => {
  const nextProps = merge(props, {
    form: `select-${props.agentType}`,
    enableReinitialize: true,
    initialValues: {
      selectAgent: props.selectedAgent ? props.selectedAgent.id : null
    }
  })
  return h(SelectAgentForOrderForm, nextProps)
}

export default SelectAgentForOrder

@@ -1490,219 +1490,8 @@
   %5D)
-,%0A      h(RaisedButton, %7B%0A        className: styles.button,%0A        type: 'button',%0A        onClick: (ev) =%3E %7B%0A          console.log('going to create a new agent / relationship')%0A        %7D%0A      %7D, 'create new')
 %0A   

remove create new agent button from order config selections as it does nothing for now