import { mdiChartTimelineVariant, mdiUpload } from '@mdi/js'
import Head from 'next/head'
import React, { ReactElement, useEffect, useState } from 'react'
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import dayjs from "dayjs";

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 FormFilePicker from '../../components/FormFilePicker'
import FormImagePicker from '../../components/FormImagePicker'
import { SelectField } from "../../components/SelectField";
import { SelectFieldMany } from "../../components/SelectFieldMany";
import { SwitchField } from '../../components/SwitchField'
import {RichTextField} from "../../components/RichTextField";

import { update, fetch } from '../../stores/ap_payments/ap_paymentsSlice'
import { useAppDispatch, useAppSelector } from '../../stores/hooks'
import { useRouter } from 'next/router'
import {saveFile} from "../../helpers/fileSaver";
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from "../../components/ImageField";



const EditAp_payments = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    
    paymentDate: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    paymentMode: '',
    

    

    

    

    
    
    
    'checkNumber': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'bankName': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'totalAmount': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    createdByUser: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    approvedByUser: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    lines: [],
    
    
    
  }
  const [initialValues, setInitialValues] = useState(initVals)

  const { ap_payments } = useAppSelector((state) => state.ap_payments)
  

  const { ap_paymentsId } = router.query

  useEffect(() => {
    dispatch(fetch({ id: ap_paymentsId }))
  }, [ap_paymentsId])

  useEffect(() => {
    if (typeof ap_payments === 'object') {
      setInitialValues(ap_payments)
    }
  }, [ap_payments])

  useEffect(() => {
      if (typeof ap_payments === 'object') {

          const newInitialVal = {...initVals};

          Object.keys(initVals).forEach(el => newInitialVal[el] = (ap_payments)[el])

          setInitialValues(newInitialVal);
      }
  }, [ap_payments])

  const handleSubmit = async (data) => {
    await dispatch(update({ id: ap_paymentsId, data }))
    await router.push('/ap_payments/ap_payments-list')
  }

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit ap_payments')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit ap_payments'} main>
        {''}
        </SectionTitleLineWithButton>
        <CardBox>
          <Formik
            enableReinitialize
            initialValues={initialValues}
            onSubmit={(values) => handleSubmit(values)}
          >
            <Form>


  

  

  

  

  

  
      <FormField
          label="PaymentDate"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.paymentDate ?
                  new Date(
                      dayjs(initialValues.paymentDate).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'paymentDate': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PaymentMode" labelFor="paymentMode">
        <Field name="paymentMode" id="paymentMode" component="select">
          
            <option value="CHECK">CHECK</option>
          
            <option value="BANK_TRANSFER">BANK_TRANSFER</option>
          
            <option value="CASH">CASH</option>
          
            <option value="PDC">PDC</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="CheckNumber"
    >
        <Field
            name="checkNumber"
            placeholder="CheckNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="BankName"
    >
        <Field
            name="bankName"
            placeholder="BankName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TotalAmount"
    >
        <Field
            type="number"
            name="totalAmount"
            placeholder="TotalAmount"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='CreatedBy' labelFor='createdByUser'>
        <Field
            name='createdByUser'
            id='createdByUser'
            component={SelectField}
            options={initialValues.createdByUser}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='ApprovedBy' labelFor='approvedByUser'>
        <Field
            name='approvedByUser'
            id='approvedByUser'
            component={SelectField}
            options={initialValues.approvedByUser}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='PaymentLines' labelFor='lines'>
          <Field
              name='lines'
              id='lines'
              component={SelectFieldMany}
              options={initialValues.lines}
              itemRef={'ap_payment_lines'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'amountPaid'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></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('/ap_payments/ap_payments-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

EditAp_payments.getLayout = function getLayout(page: ReactElement) {
  return (
      <LayoutAuthenticated
        
          permission={'UPDATE_AP_PAYMENTS'}
        
      >
          {page}
      </LayoutAuthenticated>
  )
}

export default EditAp_payments
