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/dialogue_lines/dialogue_linesSlice'
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 EditDialogue_lines = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    topic: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    npc: null,
    

    
    
    

    

    
    line_text: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    mood_tag: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    voice_files: [],
    

    

    

    
    
    

    

    

    
    priority: '',
    

    

    

    

    

    

    

    

    

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

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

  const { dialogue_linesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Topic' labelFor='topic'>
        <Field
            name='topic'
            id='topic'
            component={SelectField}
            options={initialValues.topic}
            itemRef={'dialogue_topics'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='NPC' labelFor='npc'>
        <Field
            name='npc'
            id='npc'
            component={SelectField}
            options={initialValues.npc}
            itemRef={'npcs'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  
    <FormField label='LineText' hasTextareaHeight>
        <Field
            name='line_text'
            id='line_text'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="MoodTag" labelFor="mood_tag">
        <Field name="mood_tag" id="mood_tag" component="select">
          
            <option value="neutral">neutral</option>
          
            <option value="happy">happy</option>
          
            <option value="sad">sad</option>
          
            <option value="angry">angry</option>
          
            <option value="funny">funny</option>
          
            <option value="romantic">romantic</option>
          
            <option value="serious">serious</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='VoiceFiles'
            color='info'
            icon={mdiUpload}
            path={'dialogue_lines/voice_files'}
            name='voice_files'
            id='voice_files'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  

  

  

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

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

export default EditDialogue_lines
