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/clinical_reports/clinical_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";

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



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

    

    

    

    

    

    

    

    

    

    

    
    patient: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    generated_by_user: null,
    

    
    
    

    

    

    

    

    
    period_start_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    period_end_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'report_title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    summary: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    report_file: [],
    

    

    

    
    
    

    

    

    

    

    

    

    

    
    share_scope: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    accounts: null,
    

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

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

  const { clinical_reportsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit clinical_reports')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit clinical_reports'} 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='Generatedbyuser' labelFor='generated_by_user'>
        <Field
            name='generated_by_user'
            id='generated_by_user'
            component={SelectField}
            options={initialValues.generated_by_user}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  
    <FormField
        label="Reporttitle"
    >
        <Field
            name="report_title"
            placeholder="Reporttitle"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='Summary' hasTextareaHeight>
        <Field
            name='summary'
            id='summary'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

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



  

  

  

  

  

  

  

  
    <FormField label="Sharescope" labelFor="share_scope">
        <Field name="share_scope" id="share_scope" component="select">
          
            <option value="family">family</option>
          
            <option value="clinicians">clinicians</option>
          
            <option value="admin">admin</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('/clinical_reports/clinical_reports-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditClinical_reports
