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/evidence_models/evidence_modelsSlice'
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 EditEvidence_models = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    workflow: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    control_requirement: null,
    

    
    
    

    
    evidence_claim: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    source_system: null,
    

    
    
    

    

    

    

    

    

    

    

    
    artifact_type: '',
    

    

    

    

    
    
    

    
    required_fields: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    approval_required_flag: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    retention_period: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    evidence_strength: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    collection_method: '',
    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { evidence_modelsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit evidence_models')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit evidence_models'} 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='Workflow' labelFor='workflow'>
        <Field
            name='workflow'
            id='workflow'
            component={SelectField}
            options={initialValues.workflow}
            itemRef={'identity_workflows'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'workflow_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='ControlRequirement' labelFor='control_requirement'>
        <Field
            name='control_requirement'
            id='control_requirement'
            component={SelectField}
            options={initialValues.control_requirement}
            itemRef={'control_requirements'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'control_code'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  
    <FormField label="EvidenceClaim" hasTextareaHeight>
        <Field name="evidence_claim" as="textarea" placeholder="EvidenceClaim" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='SourceSystem' labelFor='source_system'>
        <Field
            name='source_system'
            id='source_system'
            component={SelectField}
            options={initialValues.source_system}
            itemRef={'connected_systems'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'system_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ArtifactType" labelFor="artifact_type">
        <Field name="artifact_type" id="artifact_type" component="select">
          
            <option value="screenshot">screenshot</option>
          
            <option value="export_csv">export_csv</option>
          
            <option value="export_json">export_json</option>
          
            <option value="audit_log">audit_log</option>
          
            <option value="ticket">ticket</option>
          
            <option value="attestation">attestation</option>
          
            <option value="configuration">configuration</option>
          
            <option value="policy_document">policy_document</option>
          
            <option value="api_snapshot">api_snapshot</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="RequiredFields" hasTextareaHeight>
        <Field name="required_fields" as="textarea" placeholder="RequiredFields" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='ApprovalRequiredFlag' labelFor='approval_required_flag'>
        <Field
            name='approval_required_flag'
            id='approval_required_flag'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RetentionPeriod" labelFor="retention_period">
        <Field name="retention_period" id="retention_period" component="select">
          
            <option value="30_days">30_days</option>
          
            <option value="90_days">90_days</option>
          
            <option value="180_days">180_days</option>
          
            <option value="1_year">1_year</option>
          
            <option value="3_years">3_years</option>
          
            <option value="7_years">7_years</option>
          
            <option value="indefinite">indefinite</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="EvidenceStrength" labelFor="evidence_strength">
        <Field name="evidence_strength" id="evidence_strength" component="select">
          
            <option value="weak">weak</option>
          
            <option value="moderate">moderate</option>
          
            <option value="strong">strong</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="CollectionMethod" labelFor="collection_method">
        <Field name="collection_method" id="collection_method" component="select">
          
            <option value="manual">manual</option>
          
            <option value="api">api</option>
          
            <option value="export">export</option>
          
            <option value="screenshot">screenshot</option>
          
            <option value="ticket">ticket</option>
          
            <option value="log">log</option>
          
            <option value="attestation">attestation</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('/evidence_models/evidence_models-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditEvidence_models
