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/transcription_jobs/transcription_jobsSlice'
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 EditTranscription_jobs = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    requested_by: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    matter: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    media_file: null,
    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    
    'source_language': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'target_language': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    speaker_diarization: false,
    

    

    

    

    

    

    
    
    
    'confidence_avg': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    transcript_text: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    summary_text: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    export_file: null,
    

    
    
    

    

    

    

    

    
    started_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    finished_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    error_message: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { transcription_jobsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='RequestedBy' labelFor='requested_by'>
        <Field
            name='requested_by'
            id='requested_by'
            component={SelectField}
            options={initialValues.requested_by}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Matter' labelFor='matter'>
        <Field
            name='matter'
            id='matter'
            component={SelectField}
            options={initialValues.matter}
            itemRef={'matters'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="queued">queued</option>
          
            <option value="running">running</option>
          
            <option value="completed">completed</option>
          
            <option value="failed">failed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="SourceLanguage"
    >
        <Field
            name="source_language"
            placeholder="SourceLanguage"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="TargetLanguage"
    >
        <Field
            name="target_language"
            placeholder="TargetLanguage"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='SpeakerDiarization' labelFor='speaker_diarization'>
        <Field
            name='speaker_diarization'
            id='speaker_diarization'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  
    <FormField
        label="ConfidenceAvg"
    >
        <Field
            type="number"
            name="confidence_avg"
            placeholder="ConfidenceAvg"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="TranscriptText" hasTextareaHeight>
        <Field name="transcript_text" as="textarea" placeholder="TranscriptText" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="SummaryText" hasTextareaHeight>
        <Field name="summary_text" as="textarea" placeholder="SummaryText" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  
    <FormField label="ErrorMessage" hasTextareaHeight>
        <Field name="error_message" as="textarea" placeholder="ErrorMessage" />
    </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('/transcription_jobs/transcription_jobs-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditTranscription_jobs
