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/wheel_events/wheel_eventsSlice'
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 EditWheel_eventsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    match: null,
    

    
    
    

    

    

    
    trigger_question_index: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    reward_type: '',
    

    

    

    

    
    
    
    'multiplier_value': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    bonus_points: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    bonus_yux: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    music_track: '',
    

    

    

    

    
    
    

    

    

    

    

    
    event_started_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    event_ended_at: new Date(),
    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Match' labelFor='match'>
        <Field
            name='match'
            id='match'
            component={SelectField}
            options={initialValues.match}
            itemRef={'matches'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'game_code'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  
    <FormField
        label="TriggerQuestionIndex"
    >
        <Field
            type="number"
            name="trigger_question_index"
            placeholder="TriggerQuestionIndex"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RewardType" labelFor="reward_type">
        <Field name="reward_type" id="reward_type" component="select">
          
            <option value="bonus_points">bonus_points</option>
          
            <option value="double_points">double_points</option>
          
            <option value="triple_points">triple_points</option>
          
            <option value="bonus_yux">bonus_yux</option>
          
            <option value="mystery_prize">mystery_prize</option>
          
            <option value="lucky_multiplier">lucky_multiplier</option>
          
            <option value="jackpot">jackpot</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MultiplierValue"
    >
        <Field
            type="number"
            name="multiplier_value"
            placeholder="MultiplierValue"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="BonusPoints"
    >
        <Field
            type="number"
            name="bonus_points"
            placeholder="BonusPoints"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="BonusYUX"
    >
        <Field
            type="number"
            name="bonus_yux"
            placeholder="BonusYUX"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="MusicTrack" labelFor="music_track">
        <Field name="music_track" id="music_track" component="select">
          
            <option value="track_a">track_a</option>
          
            <option value="track_b">track_b</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

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

export default EditWheel_eventsPage
