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/cognitive_attempts/cognitive_attemptsSlice'
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 EditCognitive_attempts = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    session: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    exercise: null,
    

    
    
    

    

    

    

    

    
    attempted_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'score': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'accuracy': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    duration_seconds: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    result: '',
    

    

    

    

    
    
    

    
    raw_response: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    accounts: null,
    

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

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

  const { cognitive_attemptsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Session' labelFor='session'>
        <Field
            name='session'
            id='session'
            component={SelectField}
            options={initialValues.session}
            itemRef={'cognitive_sessions'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'completion_status'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Exercise' labelFor='exercise'>
        <Field
            name='exercise'
            id='exercise'
            component={SelectField}
            options={initialValues.exercise}
            itemRef={'cognitive_exercises'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'exercise_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Score"
    >
        <Field
            type="number"
            name="score"
            placeholder="Score"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Accuracy"
    >
        <Field
            type="number"
            name="accuracy"
            placeholder="Accuracy"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Durationseconds"
    >
        <Field
            type="number"
            name="duration_seconds"
            placeholder="Durationseconds"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Result" labelFor="result">
        <Field name="result" id="result" component="select">
          
            <option value="success">success</option>
          
            <option value="partial">partial</option>
          
            <option value="failed">failed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

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

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

export default EditCognitive_attempts
