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/sampled_subjects/sampled_subjectsSlice'
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 EditSampled_subjects = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    

    

    

    

    

    

    

    

    
    sample_type: '',
    

    

    

    

    
    
    
    'display_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'identifier': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'department': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'role_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'manager': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    event_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    risk_tier: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    workflow: null,
    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { sampled_subjectsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
    {hasPermission(currentUser, 'READ_ORGANIZATIONS') && 
    <FormField label='Organization' labelFor='organization'>
        <Field
            name='organization'
            id='organization'
            component={SelectField}
            options={initialValues.organization}
            itemRef={'organizations'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
     } 

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="SampleType" labelFor="sample_type">
        <Field name="sample_type" id="sample_type" component="select">
          
            <option value="terminated_user">terminated_user</option>
          
            <option value="onboarded_user">onboarded_user</option>
          
            <option value="privileged_admin">privileged_admin</option>
          
            <option value="reviewed_user">reviewed_user</option>
          
            <option value="service_account">service_account</option>
          
            <option value="exception_case">exception_case</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="DisplayName"
    >
        <Field
            name="display_name"
            placeholder="DisplayName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Identifier"
    >
        <Field
            name="identifier"
            placeholder="Identifier"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Department"
    >
        <Field
            name="department"
            placeholder="Department"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Role"
    >
        <Field
            name="role_name"
            placeholder="Role"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Manager"
    >
        <Field
            name="manager"
            placeholder="Manager"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RiskTier" labelFor="risk_tier">
        <Field name="risk_tier" id="risk_tier" component="select">
          
            <option value="low">low</option>
          
            <option value="medium">medium</option>
          
            <option value="high">high</option>
          
            <option value="critical">critical</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Workflow' labelFor='workflow'>
        <Field
            name='workflow'
            id='workflow'
            component={SelectField}
            options={initialValues.workflow}
            itemRef={'identity_workflows'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'workflow_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="new">new</option>
          
            <option value="in_progress">in_progress</option>
          
            <option value="packet_ready">packet_ready</option>
          
            <option value="closed">closed</option>
          
            <option value="archived">archived</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

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

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

export default EditSampled_subjects
