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/voice_assistant_sessions/voice_assistant_sessionsSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";



const EditVoice_assistant_sessions = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    patient: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    initiated_by_user: null,
    

    
    
    

    

    

    

    

    
    started_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    ended_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    channel: '',
    

    

    

    

    
    
    

    
    transcript: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    assistant_summary: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    outcome: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    accounts: null,
    

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

  const { voice_assistant_sessions } = useAppSelector((state) => state.voice_assistant_sessions)
  
  const { currentUser } = useAppSelector((state) => state.auth);
  

  const { voice_assistant_sessionsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Patient' labelFor='patient'>
        <Field
            name='patient'
            id='patient'
            component={SelectField}
            options={initialValues.patient}
            itemRef={'patients'}
      
        
      
        
      
        
      
        
      
        
            showField={'last_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Initiatedbyuser' labelFor='initiated_by_user'>
        <Field
            name='initiated_by_user'
            id='initiated_by_user'
            component={SelectField}
            options={initialValues.initiated_by_user}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></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="Endedat"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.ended_at ?
                  new Date(
                      dayjs(initialValues.ended_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'ended_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Channel" labelFor="channel">
        <Field name="channel" id="channel" component="select">
          
            <option value="mobile">mobile</option>
          
            <option value="web">web</option>
          
            <option value="smart_speaker">smart_speaker</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="Transcript" hasTextareaHeight>
        <Field name="transcript" as="textarea" placeholder="Transcript" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='Assistantsummary' hasTextareaHeight>
        <Field
            name='assistant_summary'
            id='assistant_summary'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Outcome" labelFor="outcome">
        <Field name="outcome" id="outcome" component="select">
          
            <option value="info">info</option>
          
            <option value="reminder_created">reminder_created</option>
          
            <option value="alert_created">alert_created</option>
          
            <option value="handoff_to_caregiver">handoff_to_caregiver</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='accounts' labelFor='accounts'>
        <Field
            name='accounts'
            id='accounts'
            component={SelectField}
            options={initialValues.accounts}
            itemRef={'accounts'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </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('/voice_assistant_sessions/voice_assistant_sessions-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditVoice_assistant_sessions
