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/avatars/avatarsSlice'
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 EditAvatars = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    rarity: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    source_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    is_collectible: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    preview_images: [],
    

    

    
    
    
    'model_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'drop_weight': '',
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { avatarsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Rarity" labelFor="rarity">
        <Field name="rarity" id="rarity" component="select">
          
            <option value="common">common</option>
          
            <option value="uncommon">uncommon</option>
          
            <option value="rare">rare</option>
          
            <option value="epic">epic</option>
          
            <option value="legendary">legendary</option>
          
            <option value="mythic">mythic</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="SourceType" labelFor="source_type">
        <Field name="source_type" id="source_type" component="select">
          
            <option value="starter">starter</option>
          
            <option value="box">box</option>
          
            <option value="event">event</option>
          
            <option value="achievement">achievement</option>
          
            <option value="shop">shop</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsCollectible' labelFor='is_collectible'>
        <Field
            name='is_collectible'
            id='is_collectible'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

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

  



  
    <FormField
        label="ModelKey"
    >
        <Field
            name="model_key"
            placeholder="ModelKey"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

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

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

export default EditAvatars
