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/mock_trial_turns/mock_trial_turnsSlice'
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 EditMock_trial_turnsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    mock_trial: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    role: null,
    

    
    
    

    

    

    

    

    

    

    

    
    turn_type: '',
    

    

    

    

    
    
    

    

    
    content: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    audio_file: null,
    

    
    
    

    

    

    

    

    
    spoken_at: new Date(),
    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='MockTrial' labelFor='mock_trial'>
        <Field
            name='mock_trial'
            id='mock_trial'
            component={SelectField}
            options={initialValues.mock_trial}
            itemRef={'mock_trials'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'session_title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Role' labelFor='role'>
        <Field
            name='role'
            id='role'
            component={SelectField}
            options={initialValues.role}
            itemRef={'mock_trial_roles'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'display_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="TurnType" labelFor="turn_type">
        <Field name="turn_type" id="turn_type" component="select">
          
            <option value="opening">opening</option>
          
            <option value="direct_exam">direct_exam</option>
          
            <option value="cross_exam">cross_exam</option>
          
            <option value="argument">argument</option>
          
            <option value="ruling">ruling</option>
          
            <option value="objection">objection</option>
          
            <option value="sidebar">sidebar</option>
          
            <option value="closing">closing</option>
          
            <option value="general">general</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  
    <FormField label='Content' hasTextareaHeight>
        <Field
            name='content'
            id='content'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='AudioFile' labelFor='audio_file'>
        <Field
            name='audio_file'
            id='audio_file'
            component={SelectField}
            options={initialValues.audio_file}
            itemRef={'files'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'file_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

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

export default EditMock_trial_turnsPage
