import { mdiChartTimelineVariant } from '@mdi/js'
import Head from 'next/head'
import React, { ReactElement } from 'react'
import CardBox from '../../components/CardBox'
import LayoutAuthenticated from '../../layouts/Authenticated'
import SectionMain from '../../components/SectionMain'
import SectionTitleLineWithButton from '../../components/SectionTitleLineWithButton'
import { getPageTitle } from '../../config'

import { Field, Form, Formik } from 'formik'
import FormField from '../../components/FormField'
import BaseDivider from '../../components/BaseDivider'
import BaseButtons from '../../components/BaseButtons'
import BaseButton from '../../components/BaseButton'
import FormCheckRadio from '../../components/FormCheckRadio'
import FormCheckRadioGroup from '../../components/FormCheckRadioGroup'
import { SwitchField } from '../../components/SwitchField'

import { SelectField } from '../../components/SelectField'
import {RichTextField} from "../../components/RichTextField";

import { create } from '../../stores/loans/loansSlice'
import { useAppDispatch } from '../../stores/hooks'
import { useRouter } from 'next/router'

const initialValues = {

    code: '',

    principal_amount: '',

    term: '',

    pending_amount: '',

    last_pending_amount: '',

    rate: '',

    commission_rate: '',

    registration_date: '',

    started_payment_date: '',

    last_payment_date: '',

    finish_payment_date: '',

    finish_discount: '',

    finish_discount_amount: '',

    admin_rate: '',

    admin_amount: '',

    purpose: '',

    status: '',

    client: '',

    staff: '',

    interest_rate: '',

    branch: '',

    loan_type: '',

}

const LoansNew = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()

  const handleSubmit = async (data) => {
    await dispatch(create(data))
    await router.push('/loans/loans-list')
  }
  return (
    <>
      <Head>
        <title>{getPageTitle('New Item')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title="New Item" main>
        {''}
        </SectionTitleLineWithButton>
        <CardBox>
          <Formik
            initialValues={
                initialValues
            }
            onSubmit={(values) => handleSubmit(values)}
          >
            <Form>

  <FormField
      label="Code"
  >
      <Field
          name="code"
          placeholder="Code"
      />
  </FormField>

    <FormField
        label="PrincipalAmount"
    >
        <Field
            type="number"
            name="principal_amount"
            placeholder="PrincipalAmount"
        />
    </FormField>

    <FormField
        label="Term"
    >
        <Field
            type="number"
            name="term"
            placeholder="Term"
        />
    </FormField>

    <FormField
        label="PendingAmount"
    >
        <Field
            type="number"
            name="pending_amount"
            placeholder="PendingAmount"
        />
    </FormField>

    <FormField
        label="LastPendingAmount"
    >
        <Field
            type="number"
            name="last_pending_amount"
            placeholder="LastPendingAmount"
        />
    </FormField>

    <FormField
        label="Rate"
    >
        <Field
            type="number"
            name="rate"
            placeholder="Rate"
        />
    </FormField>

    <FormField
        label="CommissionRate"
    >
        <Field
            type="number"
            name="commission_rate"
            placeholder="CommissionRate"
        />
    </FormField>

  <FormField
      label="RegistrationDate"
  >
      <Field
          type="datetime-local"
          name="registration_date"
          placeholder="RegistrationDate"
      />
  </FormField>

  <FormField
      label="StartedPaymentDate"
  >
      <Field
          type="datetime-local"
          name="started_payment_date"
          placeholder="StartedPaymentDate"
      />
  </FormField>

  <FormField
      label="LastPaymentDate"
  >
      <Field
          type="datetime-local"
          name="last_payment_date"
          placeholder="LastPaymentDate"
      />
  </FormField>

  <FormField
      label="FinishPaymentDate"
  >
      <Field
          type="datetime-local"
          name="finish_payment_date"
          placeholder="FinishPaymentDate"
      />
  </FormField>

    <FormField
        label="FinishDiscount"
    >
        <Field
            type="number"
            name="finish_discount"
            placeholder="FinishDiscount"
        />
    </FormField>

    <FormField
        label="FinishDiscountAmount"
    >
        <Field
            type="number"
            name="finish_discount_amount"
            placeholder="FinishDiscountAmount"
        />
    </FormField>

    <FormField
        label="AdminRate"
    >
        <Field
            type="number"
            name="admin_rate"
            placeholder="AdminRate"
        />
    </FormField>

    <FormField
        label="AdminAmount"
    >
        <Field
            type="number"
            name="admin_amount"
            placeholder="AdminAmount"
        />
    </FormField>

  <FormField
      label="Purpose"
  >
      <Field
          name="purpose"
          placeholder="Purpose"
      />
  </FormField>

  <FormField label="Status" labelFor="status">
      <Field name="status" id="status" component={SelectField} options={[]} itemRef={'loan_status'}></Field>
  </FormField>

  <FormField label="Client" labelFor="client">
      <Field name="client" id="client" component={SelectField} options={[]} itemRef={'clients'}></Field>
  </FormField>

  <FormField label="Staff" labelFor="staff">
      <Field name="staff" id="staff" component={SelectField} options={[]} itemRef={'staffs'}></Field>
  </FormField>

  <FormField label="InterestRate" labelFor="interest_rate">
      <Field name="interest_rate" id="interest_rate" component={SelectField} options={[]} itemRef={'interest_rates'}></Field>
  </FormField>

  <FormField label="Branch" labelFor="branch">
      <Field name="branch" id="branch" component={SelectField} options={[]} itemRef={'branches'}></Field>
  </FormField>

  <FormField label="LoanType" labelFor="loan_type">
      <Field name="loan_type" id="loan_type" component={SelectField} options={[]} itemRef={'loan_types'}></Field>
  </FormField>

              <BaseDivider />
              <BaseButtons>
                <BaseButton type="submit" color="info" label="Submit" />
                <BaseButton type="reset" color="info" outline label="Reset" />
                <BaseButton type='reset' color='danger' outline label='Cancel' onClick={() => router.push('/loans/loans-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

LoansNew.getLayout = function getLayout(page: ReactElement) {
  return (
      <LayoutAuthenticated>
          {page}
      </LayoutAuthenticated>
  )
}

export default LoansNew
