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/store_settings/store_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 EditStore_settingsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'store_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'store_tagline': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'default_currency': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'support_phone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'support_email': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'whatsapp_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    address_text: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'primary_language': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    logo_images: [],
    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    banner_images: [],
    

    

    
    
    

    

    

    

    

    

    
    maintenance_mode: false,
    

    

    

    

    

    

    
    
    

    

    
    maintenance_message: '',
    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  
    <FormField
        label="StoreName"
    >
        <Field
            name="store_name"
            placeholder="StoreName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="StoreTagline"
    >
        <Field
            name="store_tagline"
            placeholder="StoreTagline"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="DefaultCurrency"
    >
        <Field
            name="default_currency"
            placeholder="DefaultCurrency"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="SupportPhone"
    >
        <Field
            name="support_phone"
            placeholder="SupportPhone"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="SupportEmail"
    >
        <Field
            name="support_email"
            placeholder="SupportEmail"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="AddressText" hasTextareaHeight>
        <Field name="address_text" as="textarea" placeholder="AddressText" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="PrimaryLanguage"
    >
        <Field
            name="primary_language"
            placeholder="PrimaryLanguage"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

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

  



  

  

  

  

  

  

  

  

  

  

  

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

  



  

  

  

  

  

  

  

  

  
    <FormField label='MaintenanceMode' labelFor='maintenance_mode'>
        <Field
            name='maintenance_mode'
            id='maintenance_mode'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  
    <FormField label='MaintenanceMessage' hasTextareaHeight>
        <Field
            name='maintenance_message'
            id='maintenance_message'
            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('/store_settings/store_settings-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditStore_settingsPage
