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/cosmetics/cosmeticsSlice'
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 EditCosmetics = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    cosmetic_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    rarity: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    icon_images: [],
    

    

    
    
    
    'asset_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_limited_time: false,
    

    

    

    

    

    

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

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

  const { cosmeticsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="CosmeticType" labelFor="cosmetic_type">
        <Field name="cosmetic_type" id="cosmetic_type" component="select">
          
            <option value="color">color</option>
          
            <option value="face">face</option>
          
            <option value="pattern">pattern</option>
          
            <option value="hat">hat</option>
          
            <option value="title">title</option>
          
            <option value="badge">badge</option>
          
        </Field>
    </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>
            <Field
                label='IconImages'
                color='info'
                icon={mdiUpload}
                path={'cosmetics/icon_images'}
                name='icon_images'
                id='icon_images'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></Field>
        </FormField>
    

  



  
    <FormField
        label="AssetKey"
    >
        <Field
            name="asset_key"
            placeholder="AssetKey"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

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

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

export default EditCosmetics
