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/cta_buttons/cta_buttonsSlice'
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 EditCta_buttons = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'label': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    cta_type: '',
    

    

    

    

    
    
    
    'url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    style_variant: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    open_in_new_tab: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    page: null,
    

    
    
    

    

    

    
    sort_order: '',
    

    

    

    

    

    

    

    

    

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

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

  const { cta_buttonsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  
    <FormField
        label="Label"
    >
        <Field
            name="label"
            placeholder="Label"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="CTAType" labelFor="cta_type">
        <Field name="cta_type" id="cta_type" component="select">
          
            <option value="request_samples">request_samples</option>
          
            <option value="get_a_quote">get_a_quote</option>
          
            <option value="contact_us">contact_us</option>
          
            <option value="subscribe">subscribe</option>
          
            <option value="partner_with_us">partner_with_us</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="URL"
    >
        <Field
            name="url"
            placeholder="URL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="StyleVariant" labelFor="style_variant">
        <Field name="style_variant" id="style_variant" component="select">
          
            <option value="primary">primary</option>
          
            <option value="secondary">secondary</option>
          
            <option value="outline">outline</option>
          
            <option value="link">link</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='OpenInNewTab' labelFor='open_in_new_tab'>
        <Field
            name='open_in_new_tab'
            id='open_in_new_tab'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Page' labelFor='page'>
        <Field
            name='page'
            id='page'
            component={SelectField}
            options={initialValues.page}
            itemRef={'pages'}
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  
    <FormField
        label="SortOrder"
    >
        <Field
            type="number"
            name="sort_order"
            placeholder="SortOrder"
        />
    </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('/cta_buttons/cta_buttons-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditCta_buttons
