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/achievement_definitions/achievement_definitionsSlice'
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 EditAchievement_definitionsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    description: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    badge_images: [],
    

    

    
    
    

    

    

    

    

    

    

    

    
    category: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    rarity: '',
    

    

    

    

    
    
    

    

    

    
    target_value: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    metric: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    active: false,
    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='Description' hasTextareaHeight>
        <Field
            name='description'
            id='description'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

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

  



  

  

  

  

  

  

  

  
    <FormField label="Category" labelFor="category">
        <Field name="category" id="category" component="select">
          
            <option value="reading">reading</option>
          
            <option value="streak">streak</option>
          
            <option value="coins">coins</option>
          
            <option value="social">social</option>
          
            <option value="quiz">quiz</option>
          
            <option value="challenge">challenge</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

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

  

  

  

    

  



  

  

  

  
    <FormField
        label="Targetvalue"
    >
        <Field
            type="number"
            name="target_value"
            placeholder="Targetvalue"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Metric" labelFor="metric">
        <Field name="metric" id="metric" component="select">
          
            <option value="books_completed">books_completed</option>
          
            <option value="minutes_read">minutes_read</option>
          
            <option value="coins_earned">coins_earned</option>
          
            <option value="streak_days">streak_days</option>
          
            <option value="quizzes_passed">quizzes_passed</option>
          
            <option value="challenges_completed">challenges_completed</option>
          
            <option value="friends_added">friends_added</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

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

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

export default EditAchievement_definitionsPage
