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/stage_assessments/stage_assessmentsSlice'
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 EditStage_assessmentsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    patient: null,
    

    
    
    

    

    

    

    

    
    assessed_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    predicted_stage: '',
    

    

    

    

    
    
    
    'confidence_score': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    method: '',
    

    

    

    

    
    
    
    'model_version': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    explanation: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    reviewed_by_user: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    accounts: null,
    

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

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

  const { id } = router.query

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

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit stage_assessments')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit stage_assessments'} 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="Assessedat"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.assessed_at ?
                  new Date(
                      dayjs(initialValues.assessed_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'assessed_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Predictedstage" labelFor="predicted_stage">
        <Field name="predicted_stage" id="predicted_stage" component="select">
          
            <option value="mild">mild</option>
          
            <option value="moderate">moderate</option>
          
            <option value="severe">severe</option>
          
            <option value="unknown">unknown</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Confidencescore"
    >
        <Field
            type="number"
            name="confidence_score"
            placeholder="Confidencescore"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Method" labelFor="method">
        <Field name="method" id="method" component="select">
          
            <option value="questionnaire">questionnaire</option>
          
            <option value="cognitive_model">cognitive_model</option>
          
            <option value="clinician">clinician</option>
          
            <option value="hybrid">hybrid</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Modelversion"
    >
        <Field
            name="model_version"
            placeholder="Modelversion"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='Explanation' hasTextareaHeight>
        <Field
            name='explanation'
            id='explanation'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

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

export default EditStage_assessmentsPage
