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/vendors/vendorsSlice'
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 EditVendors = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'vendorCode': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'vendorName': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    vendorType: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    taxClassification: '',
    

    

    

    

    
    
    
    'tinNumber': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    birRegAddress: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'contactPerson': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'contactNumber': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'email': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    address: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    paymentTermsDays: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    defaultAccount: null,
    

    
    
    
    'wtaxAtcCode': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'wtaxRate': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'bankName': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'bankAccountNumber': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'bankAccountName': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    isActive: false,
    

    

    

    

    

    

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

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

  const { vendorsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  
    <FormField
        label="VendorCode"
    >
        <Field
            name="vendorCode"
            placeholder="VendorCode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="VendorName"
    >
        <Field
            name="vendorName"
            placeholder="VendorName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="VendorType" labelFor="vendorType">
        <Field name="vendorType" id="vendorType" component="select">
          
            <option value="SUPPLIER">SUPPLIER</option>
          
            <option value="SERVICE_PROVIDER">SERVICE_PROVIDER</option>
          
            <option value="CONTRACTOR">CONTRACTOR</option>
          
            <option value="GOVERNMENT">GOVERNMENT</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="TaxClassification" labelFor="taxClassification">
        <Field name="taxClassification" id="taxClassification" component="select">
          
            <option value="VAT_REGISTERED">VAT_REGISTERED</option>
          
            <option value="NON_VAT">NON_VAT</option>
          
            <option value="GOVERNMENT">GOVERNMENT</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="TINNumber"
    >
        <Field
            name="tinNumber"
            placeholder="TINNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="BIRRegisteredAddress" hasTextareaHeight>
        <Field name="birRegAddress" as="textarea" placeholder="BIRRegisteredAddress" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ContactPerson"
    >
        <Field
            name="contactPerson"
            placeholder="ContactPerson"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ContactNumber"
    >
        <Field
            name="contactNumber"
            placeholder="ContactNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Email"
    >
        <Field
            name="email"
            placeholder="Email"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Address" hasTextareaHeight>
        <Field name="address" as="textarea" placeholder="Address" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PaymentTermsDays"
    >
        <Field
            type="number"
            name="paymentTermsDays"
            placeholder="PaymentTermsDays"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='DefaultAccount' labelFor='defaultAccount'>
        <Field
            name='defaultAccount'
            id='defaultAccount'
            component={SelectField}
            options={initialValues.defaultAccount}
            itemRef={'accounts'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="WithholdingATCCode"
    >
        <Field
            name="wtaxAtcCode"
            placeholder="WithholdingATCCode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="WithholdingTaxRate"
    >
        <Field
            type="number"
            name="wtaxRate"
            placeholder="WithholdingTaxRate"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="BankAccountNumber"
    >
        <Field
            name="bankAccountNumber"
            placeholder="BankAccountNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="BankAccountName"
    >
        <Field
            name="bankAccountName"
            placeholder="BankAccountName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsActive' labelFor='isActive'>
        <Field
            name='isActive'
            id='isActive'
            component={SwitchField}
        ></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('/vendors/vendors-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditVendors
