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

const defaultTransferName = () =>
  `Transfer ${dayjs().format('YYYY-MM-DD HH:mm:ss')}`

const NewStockTransfer = ({ onOk, reset }) => {
  return (
    <NewObjectForm
      type={'stockTransfer'}
      reset={reset}
      defaultValues={{
        name: defaultTransferName(),
        state: { type: 'draft' },
        lines: []
      }}
    >
      {({ handleSubmit, submitLoading, objectData, formValid }) => {
        const steps = [
          {
            title: 'Required',
            key: 'required',
            content: (
              <ObjectInfo
                type='stockTransfer'
                column={1}
                bordered={false}
                labelWidth={80}
                isEditing={true}
                required={true}
                objectData={objectData}
              />
            )
          },
          {
            title: 'Summary',
            key: 'summary',
            content: (
              <ObjectInfo
                type='stockTransfer'
                column={1}
                bordered={false}
                labelWidth={80}
                visibleProperties={{
                  _id: false,
                  createdAt: false,
                  updatedAt: false,
                  lines: false,
                  _reference: false,
                  postedAt: false
                }}
                isEditing={false}
                objectData={objectData}
              />
            )
          }
        ]
        return (
          <WizardView
            steps={steps}
            loading={submitLoading}
            formValid={formValid}
            title='New Stock Transfer'
            onSubmit={async () => {
              const result = await handleSubmit()
              if (result) {
                onOk()
              }
            }}
          />
        )
      }}
    </NewObjectForm>
  )
}

NewStockTransfer.propTypes = {
  onOk: PropTypes.func.isRequired,
  reset: PropTypes.bool
}

export default NewStockTransfer
