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/avatar_customizations/avatar_customizationsSlice'
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 EditAvatar_customizationsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    
    'profile_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    base_color: '',
    

    

    

    

    
    
    
    'custom_color_hex': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    face_style: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    pattern_style: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    pattern_color: '',
    

    

    

    

    
    
    
    'pattern_custom_hex': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    hat_style: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    render_images: [],
    

    

    
    
    

    

    

    

    

    

    
    is_active: false,
    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='User' labelFor='user'>
        <Field
            name='user'
            id='user'
            component={SelectField}
            options={initialValues.user}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="ProfileName"
    >
        <Field
            name="profile_name"
            placeholder="ProfileName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="BaseColor" labelFor="base_color">
        <Field name="base_color" id="base_color" component="select">
          
            <option value="red">red</option>
          
            <option value="orange">orange</option>
          
            <option value="yellow">yellow</option>
          
            <option value="green">green</option>
          
            <option value="blue">blue</option>
          
            <option value="purple">purple</option>
          
            <option value="pink">pink</option>
          
            <option value="white">white</option>
          
            <option value="gray">gray</option>
          
            <option value="black">black</option>
          
            <option value="custom">custom</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="CustomColorHex"
    >
        <Field
            name="custom_color_hex"
            placeholder="CustomColorHex"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="FaceStyle" labelFor="face_style">
        <Field name="face_style" id="face_style" component="select">
          
            <option value="default">default</option>
          
            <option value="smile">smile</option>
          
            <option value="grin">grin</option>
          
            <option value="surprised">surprised</option>
          
            <option value="sleepy">sleepy</option>
          
            <option value="silly">silly</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PatternStyle" labelFor="pattern_style">
        <Field name="pattern_style" id="pattern_style" component="select">
          
            <option value="none">none</option>
          
            <option value="stripes">stripes</option>
          
            <option value="polka_dots">polka_dots</option>
          
            <option value="stars">stars</option>
          
            <option value="waves">waves</option>
          
            <option value="checker">checker</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PatternColor" labelFor="pattern_color">
        <Field name="pattern_color" id="pattern_color" component="select">
          
            <option value="white">white</option>
          
            <option value="black">black</option>
          
            <option value="red">red</option>
          
            <option value="blue">blue</option>
          
            <option value="green">green</option>
          
            <option value="yellow">yellow</option>
          
            <option value="custom">custom</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="PatternCustomHex"
    >
        <Field
            name="pattern_custom_hex"
            placeholder="PatternCustomHex"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="HatStyle" labelFor="hat_style">
        <Field name="hat_style" id="hat_style" component="select">
          
            <option value="none">none</option>
          
            <option value="cap">cap</option>
          
            <option value="beanie">beanie</option>
          
            <option value="crown">crown</option>
          
            <option value="graduation_cap">graduation_cap</option>
          
            <option value="headphones">headphones</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

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

  



  

  

  

  

  

  

  

  

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

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

export default EditAvatar_customizationsPage
