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/settings/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";



const EditSettings = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'company_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'legal_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    address: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'phone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'email': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'city': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'country': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tax_id': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    logo: [],
    

    

    
    
    
    'primary_color': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'secondary_color': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    theme: '',
    

    

    

    

    
    
    
    'invoice_prefix': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'quote_prefix': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'sale_prefix': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    invoice_next_number: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    quote_next_number: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    sale_next_number: '',
    

    

    

    

    

    

    

    

    

    
    
    

    
    invoice_footer_note: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { settingsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  
    <FormField
        label="Nomentreprise"
    >
        <Field
            name="company_name"
            placeholder="Nomentreprise"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Raisonsociale"
    >
        <Field
            name="legal_name"
            placeholder="Raisonsociale"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

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

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Téléphone"
    >
        <Field
            name="phone"
            placeholder="Téléphone"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Ville"
    >
        <Field
            name="city"
            placeholder="Ville"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Pays"
    >
        <Field
            name="country"
            placeholder="Pays"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="NINEA/Identifiantfiscal"
    >
        <Field
            name="tax_id"
            placeholder="NINEA/Identifiantfiscal"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='Logo'
                color='info'
                icon={mdiUpload}
                path={'settings/logo'}
                name='logo'
                id='logo'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></Field>
        </FormField>
    

  



  
    <FormField
        label="Couleurprincipale"
    >
        <Field
            name="primary_color"
            placeholder="Couleurprincipale"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Couleursecondaire"
    >
        <Field
            name="secondary_color"
            placeholder="Couleursecondaire"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Thème" labelFor="theme">
        <Field name="theme" id="theme" component="select">
          
            <option value="light">light</option>
          
            <option value="dark">dark</option>
          
            <option value="system">system</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Préfixefacture"
    >
        <Field
            name="invoice_prefix"
            placeholder="Préfixefacture"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Préfixedevis"
    >
        <Field
            name="quote_prefix"
            placeholder="Préfixedevis"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Préfixevente"
    >
        <Field
            name="sale_prefix"
            placeholder="Préfixevente"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Prochainnumérofacture"
    >
        <Field
            type="number"
            name="invoice_next_number"
            placeholder="Prochainnumérofacture"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Prochainnumérodevis"
    >
        <Field
            type="number"
            name="quote_next_number"
            placeholder="Prochainnumérodevis"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Prochainnumérovente"
    >
        <Field
            type="number"
            name="sale_next_number"
            placeholder="Prochainnumérovente"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Pieddepagefacture" hasTextareaHeight>
        <Field name="invoice_footer_note" as="textarea" placeholder="Pieddepagefacture" />
    </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('/settings/settings-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditSettings
