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/page_elements/page_elementsSlice'
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 EditPage_elements = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    page: null,
    

    
    
    

    

    

    

    

    

    

    

    
    element_type: '',
    

    

    

    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    sort_order: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_visible: false,
    

    

    

    

    

    

    
    
    
    'x_percent': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'y_percent': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'width_percent': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'height_percent': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'rotation_deg': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    style_json: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    content_json: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { page_elementsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Elementtype" labelFor="element_type">
        <Field name="element_type" id="element_type" component="select">
          
            <option value="nav_button">nav_button</option>
          
            <option value="spot">spot</option>
          
            <option value="description">description</option>
          
            <option value="tooltip">tooltip</option>
          
            <option value="gallery">gallery</option>
          
            <option value="carousel">carousel</option>
          
            <option value="logo">logo</option>
          
            <option value="video_player">video_player</option>
          
            <option value="popup">popup</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Sortorder"
    >
        <Field
            type="number"
            name="sort_order"
            placeholder="Sortorder"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Isvisible' labelFor='is_visible'>
        <Field
            name='is_visible'
            id='is_visible'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  
    <FormField
        label="X(%)"
    >
        <Field
            type="number"
            name="x_percent"
            placeholder="X(%)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Y(%)"
    >
        <Field
            type="number"
            name="y_percent"
            placeholder="Y(%)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Width(%)"
    >
        <Field
            type="number"
            name="width_percent"
            placeholder="Width(%)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Height(%)"
    >
        <Field
            type="number"
            name="height_percent"
            placeholder="Height(%)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Rotation(deg)"
    >
        <Field
            type="number"
            name="rotation_deg"
            placeholder="Rotation(deg)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="StyleJSON" hasTextareaHeight>
        <Field name="style_json" as="textarea" placeholder="StyleJSON" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="ContentJSON" hasTextareaHeight>
        <Field name="content_json" as="textarea" placeholder="ContentJSON" />
    </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('/page_elements/page_elements-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditPage_elements
