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/organization_settings/organization_settingsSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";



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

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    
    'default_currency_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'default_language': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'default_timezone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'invoice_prefix': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'quote_prefix': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'receipt_prefix': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'default_vat_rate': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    prices_include_tax: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    enable_inventory: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    enable_projects: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    enable_approvals: false,
    

    

    

    

    

    

    
    
    

    

    
    invoice_footer_note: '',
    

    

    

    

    

    

    

    

    

    

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

  const { organization_settings } = useAppSelector((state) => state.organization_settings)
  
  const { currentUser } = useAppSelector((state) => state.auth);
  

  const { id } = router.query

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

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

  useEffect(() => {
      if (typeof organization_settings === 'object') {
          const newInitialVal = {...initVals};
          Object.keys(initVals).forEach(el => newInitialVal[el] = (organization_settings)[el])
          setInitialValues(newInitialVal);
      }
  }, [organization_settings])

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

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


  

  

  

  

  

  

  

  

  

  
    {hasPermission(currentUser, 'READ_ORGANIZATIONS') && 
    <FormField label='Organization' labelFor='organization'>
        <Field
            name='organization'
            id='organization'
            component={SelectField}
            options={initialValues.organization}
            itemRef={'organizations'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
     } 

  

  

    

  



  
    <FormField
        label="DefaultCurrencyCode"
    >
        <Field
            name="default_currency_code"
            placeholder="DefaultCurrencyCode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="DefaultLanguage"
    >
        <Field
            name="default_language"
            placeholder="DefaultLanguage"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="DefaultTimezone"
    >
        <Field
            name="default_timezone"
            placeholder="DefaultTimezone"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="InvoicePrefix"
    >
        <Field
            name="invoice_prefix"
            placeholder="InvoicePrefix"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="QuotePrefix"
    >
        <Field
            name="quote_prefix"
            placeholder="QuotePrefix"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ReceiptPrefix"
    >
        <Field
            name="receipt_prefix"
            placeholder="ReceiptPrefix"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DefaultVATRate"
    >
        <Field
            type="number"
            name="default_vat_rate"
            placeholder="DefaultVATRate"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='PricesIncludeTax' labelFor='prices_include_tax'>
        <Field
            name='prices_include_tax'
            id='prices_include_tax'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='EnableInventory' labelFor='enable_inventory'>
        <Field
            name='enable_inventory'
            id='enable_inventory'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='EnableProjects' labelFor='enable_projects'>
        <Field
            name='enable_projects'
            id='enable_projects'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='EnableApprovals' labelFor='enable_approvals'>
        <Field
            name='enable_approvals'
            id='enable_approvals'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  
    <FormField label='InvoiceFooterNote' hasTextareaHeight>
        <Field
            name='invoice_footer_note'
            id='invoice_footer_note'
            component={RichTextField}
        ></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('/organization_settings/organization_settings-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditOrganization_settingsPage
