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_settingsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'site_name_ar': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'site_tagline_ar': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    logo: [],
    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    favicon: [],
    

    

    
    
    
    'primary_font': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    direction: '',
    

    

    

    

    
    
    
    'primary_color_hex': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'accent_color_hex': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'seo_default_title_ar': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    seo_default_description_ar: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    brand_guidelines: [],
    

    

    

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

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

  const { id } = router.query

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

  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: id, 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="SiteNameArabic"
    >
        <Field
            name="site_name_ar"
            placeholder="SiteNameArabic"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="SiteTaglineArabic"
    >
        <Field
            name="site_tagline_ar"
            placeholder="SiteTaglineArabic"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

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

  



  

  

  

  

  

  

  

  

  

  

  

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

  



  
    <FormField
        label="PrimaryFont"
    >
        <Field
            name="primary_font"
            placeholder="PrimaryFont"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Direction" labelFor="direction">
        <Field name="direction" id="direction" component="select">
          
            <option value="rtl">rtl</option>
          
            <option value="ltr">ltr</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="PrimaryColorHex"
    >
        <Field
            name="primary_color_hex"
            placeholder="PrimaryColorHex"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="AccentColorHex"
    >
        <Field
            name="accent_color_hex"
            placeholder="AccentColorHex"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="SEODefaultTitleArabic"
    >
        <Field
            name="seo_default_title_ar"
            placeholder="SEODefaultTitleArabic"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="SEODefaultDescriptionArabic" hasTextareaHeight>
        <Field name="seo_default_description_ar" as="textarea" placeholder="SEODefaultDescriptionArabic" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='BrandGuidelines'
            color='info'
            icon={mdiUpload}
            path={'site_settings/brand_guidelines'}
            name='brand_guidelines'
            id='brand_guidelines'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></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_settingsPage.getLayout = function getLayout(page: ReactElement) {
  return (
      <LayoutAuthenticated
        
          permission={'UPDATE_SITE_SETTINGS'}
        
      >
          {page}
      </LayoutAuthenticated>
  )
}

export default EditSite_settingsPage
