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/seo_pages/seo_pagesSlice'
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 EditSeo_pagesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    
    page_key: '',
    

    

    

    

    
    
    
    'title_ar': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    description_ar: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'canonical_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    structured_data_jsonld: [],
    

    

    

    
    
    

    

    

    

    

    

    
    indexable: false,
    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  
    <FormField label="PageKey" labelFor="page_key">
        <Field name="page_key" id="page_key" component="select">
          
            <option value="home">home</option>
          
            <option value="products">products</option>
          
            <option value="product_detail">product_detail</option>
          
            <option value="projects">projects</option>
          
            <option value="project_detail">project_detail</option>
          
            <option value="services">services</option>
          
            <option value="service_detail">service_detail</option>
          
            <option value="about">about</option>
          
            <option value="contact">contact</option>
          
            <option value="blog">blog</option>
          
            <option value="post_detail">post_detail</option>
          
            <option value="privacy">privacy</option>
          
            <option value="terms">terms</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="TitleArabic"
    >
        <Field
            name="title_ar"
            placeholder="TitleArabic"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="DescriptionArabic" hasTextareaHeight>
        <Field name="description_ar" as="textarea" placeholder="DescriptionArabic" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="CanonicalURL"
    >
        <Field
            name="canonical_url"
            placeholder="CanonicalURL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='StructuredDataJSON-LD'
            color='info'
            icon={mdiUpload}
            path={'seo_pages/structured_data_jsonld'}
            name='structured_data_jsonld'
            id='structured_data_jsonld'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  

  

  

  

  

  

  

  

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

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

export default EditSeo_pagesPage
