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/challenge_definitions/challenge_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 EditChallenge_definitions = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    description: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    challenge_type: '',
    

    

    

    

    
    
    

    

    

    
    target_value: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    reward_coins: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    start_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    end_at: new Date(),
    

    

    

    

    

    

    

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

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

  const { challenge_definitionsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit challenge_definitions')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit challenge_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 label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="draft">draft</option>
          
            <option value="active">active</option>
          
            <option value="ended">ended</option>
          
            <option value="archived">archived</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Challengetype" labelFor="challenge_type">
        <Field name="challenge_type" id="challenge_type" component="select">
          
            <option value="read_books">read_books</option>
          
            <option value="read_minutes">read_minutes</option>
          
            <option value="scan_books">scan_books</option>
          
            <option value="complete_quizzes">complete_quizzes</option>
          
            <option value="earn_coins">earn_coins</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

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

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Rewardcoins"
    >
        <Field
            type="number"
            name="reward_coins"
            placeholder="Rewardcoins"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="Startat"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.start_at ?
                  new Date(
                      dayjs(initialValues.start_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'start_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="Endat"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.end_at ?
                  new Date(
                      dayjs(initialValues.end_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'end_at': date})}
          />
      </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('/challenge_definitions/challenge_definitions-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditChallenge_definitions
