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

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'store_tagline': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'support_email': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'support_phone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'whatsapp_number': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'currency_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'timezone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    logo_images: [],
    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    banner_images: [],
    

    

    
    
    
    'primary_color_hex': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'secondary_color_hex': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    maintenance_mode: false,
    

    

    

    

    

    

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

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

  const { store_settingsId } = router.query

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

  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: store_settingsId, 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="SupportEmail"
    >
        <Field
            name="support_email"
            placeholder="SupportEmail"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="CurrencyCode"
    >
        <Field
            name="currency_code"
            placeholder="CurrencyCode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Timezone"
    >
        <Field
            name="timezone"
            placeholder="Timezone"
        />
    </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="PrimaryColorHex"
    >
        <Field
            name="primary_color_hex"
            placeholder="PrimaryColorHex"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="SecondaryColorHex"
    >
        <Field
            name="secondary_color_hex"
            placeholder="SecondaryColorHex"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

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

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

export default EditStore_settings
