import PropTypes from 'prop-types'
import ObjectInfo from '../../common/ObjectInfo'
import NewObjectForm from '../../common/NewObjectForm'
import WizardView from '../../common/WizardView'

const NewPayment = ({ onOk, reset, defaultValues }) => {
  return (
    <NewObjectForm
      type={'payment'}
      reset={reset}
      defaultValues={{
        state: { type: 'draft' },
        paymentDate: new Date(),
        amount: 0,
        ...defaultValues
      }}
    >
      {({ handleSubmit, submitLoading, objectData, formValid }) => {
        const steps = [
          {
            title: 'Required',
            key: 'required',
            content: (
              <ObjectInfo
                type='payment'
                column={1}
                bordered={false}
                isEditing={true}
                required={true}
                objectData={objectData}
                visibleProperties={{
                  invoice: true,
                  vendor: true,
                  client: true,
                  paymentDate: true,
                  amount: true,
                  paymentMethod: true
                }}
              />
            )
          },
          {
            title: 'Summary',
            key: 'summary',
            content: (
              <ObjectInfo
                type='payment'
                column={1}
                bordered={false}
                visibleProperties={{
                  _id: false,
                  createdAt: false,
                  updatedAt: false,
                  _reference: false,
                  postedAt: false,
                  authorisedAt: false,
                  declinedAt: false,
                  cancelledAt: false
                }}
                isEditing={false}
                objectData={objectData}
              />
            )
          }
        ]
        return (
          <WizardView
            steps={steps}
            loading={submitLoading}
            formValid={formValid}
            title='New Payment'
            onSubmit={async () => {
              const result = await handleSubmit()
              if (result) {
                onOk()
              }
            }}
          />
        )
      }}
    </NewObjectForm>
  )
}

NewPayment.propTypes = {
  onOk: PropTypes.func.isRequired,
  reset: PropTypes.bool,
  defaultValues: PropTypes.object
}

export default NewPayment
