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/medical_chronology_reports/medical_chronology_reportsSlice'
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 EditMedical_chronology_reports = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    requested_by: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    matter: null,
    

    
    
    
    'report_title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    
    overview: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    source_files: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    
    export_file: null,
    

    
    
    

    

    

    

    

    
    generated_at: new Date(),
    

    

    

    

    

    

    

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

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

  const { medical_chronology_reportsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit medical_chronology_reports')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit medical_chronology_reports'} 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="ReportTitle"
    >
        <Field
            name="report_title"
            placeholder="ReportTitle"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="draft">draft</option>
          
            <option value="generated">generated</option>
          
            <option value="exported">exported</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  
    <FormField label='Overview' hasTextareaHeight>
        <Field
            name='overview'
            id='overview'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='SourceFiles' labelFor='source_files'>
          <Field
              name='source_files'
              id='source_files'
              component={SelectFieldMany}
              options={initialValues.source_files}
              itemRef={'files'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'file_name'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </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="GeneratedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.generated_at ?
                  new Date(
                      dayjs(initialValues.generated_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'generated_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('/medical_chronology_reports/medical_chronology_reports-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditMedical_chronology_reports
