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 NewInvoice = ({ onOk, reset, defaultValues }) => {
  return (
    <NewObjectForm
      type={'invoice'}
      reset={reset}
      defaultValues={{
        state: { type: 'draft' },
        issuedAt: new Date(),
        dueAt: dayjs().add(3, 'day').toDate(),
        ...defaultValues
      }}
    >
      {({ handleSubmit, submitLoading, objectData, formValid }) => {
        const steps = [
          {
            title: 'Required',
            key: 'required',
            content: (
              <ObjectInfo
                type='invoice'
                column={1}
                bordered={false}
                isEditing={true}
                required={true}
                objectData={objectData}
                visibleProperties={{
                  orderType: true,
                  order: true,
                  to: true,
                  from: true,
                  toType: true,
                  fromType: true,
                  issuedAt: true,
                  dueAt: true
                }}
              />
            )
          },
          {
            title: 'Summary',
            key: 'summary',
            content: (
              <ObjectInfo
                type='invoice'
                column={1}
                bordered={false}
                visibleProperties={{
                  _id: false,
                  createdAt: false,
                  invoiceOrderItems: false,
                  invoiceShipments: false,
                  updatedAt: false,
                  _reference: false,
                  totalAmount: false,
                  totalAmountWithTax: false,
                  totalTaxAmount: false,
                  shippingAmount: false,
                  shippingAmountWithTax: false,
                  grandTotalAmount: false,
                  sentAt: false,
                  paidAt: false,
                  cancelledAt: false,
                  overdueAt: false,
                  acknowledgedAt: false,
                  postedAt: false
                }}
                isEditing={false}
                objectData={objectData}
              />
            )
          }
        ]
        return (
          <WizardView
            steps={steps}
            loading={submitLoading}
            formValid={formValid}
            title='New Invoice'
            onSubmit={async () => {
              const result = await handleSubmit()
              if (result) {
                onOk()
              }
            }}
          />
        )
      }}
    </NewObjectForm>
  )
}

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

export default NewInvoice
