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/site_settings/site_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 EditSite_settings = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'site_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'tagline': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    logo_images: [],
    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    favicon_images: [],
    

    

    
    
    
    'primary_phone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'secondary_phone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'whatsapp_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'contact_email': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    address: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    google_maps_embed: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    working_hours: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'facebook_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'instagram_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'linkedin_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'youtube_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    show_cookie_banner: false,
    

    

    

    

    

    

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

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

  const { site_settingsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  
    <FormField
        label="SiteName"
    >
        <Field
            name="site_name"
            placeholder="SiteName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Tagline"
    >
        <Field
            name="tagline"
            placeholder="Tagline"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

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

  



  

  

  

  

  

  

  

  

  

  

  

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

  



  
    <FormField
        label="PrimaryPhone"
    >
        <Field
            name="primary_phone"
            placeholder="PrimaryPhone"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="SecondaryPhone"
    >
        <Field
            name="secondary_phone"
            placeholder="SecondaryPhone"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="WhatsAppNumber"
    >
        <Field
            name="whatsapp_number"
            placeholder="WhatsAppNumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ContactEmail"
    >
        <Field
            name="contact_email"
            placeholder="ContactEmail"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

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

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="GoogleMapsEmbedCode" hasTextareaHeight>
        <Field name="google_maps_embed" as="textarea" placeholder="GoogleMapsEmbedCode" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="WorkingHours" hasTextareaHeight>
        <Field name="working_hours" as="textarea" placeholder="WorkingHours" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="FacebookURL"
    >
        <Field
            name="facebook_url"
            placeholder="FacebookURL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="InstagramURL"
    >
        <Field
            name="instagram_url"
            placeholder="InstagramURL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="LinkedInURL"
    >
        <Field
            name="linkedin_url"
            placeholder="LinkedInURL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="YouTubeURL"
    >
        <Field
            name="youtube_url"
            placeholder="YouTubeURL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='ShowCookieBanner' labelFor='show_cookie_banner'>
        <Field
            name='show_cookie_banner'
            id='show_cookie_banner'
            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('/site_settings/site_settings-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditSite_settings
