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/identity_workflows/identity_workflowsSlice'
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 EditIdentity_workflowsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    
    'workflow_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    workflow_type: '',
    

    

    

    

    
    
    

    
    description: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    risk_level: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    owner_role: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    source_trigger_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    recurrence_type: '',
    

    

    

    

    
    
    

    

    

    
    target_sla_hours: '',
    

    

    

    

    

    

    

    

    

    
    
    

    
    coverage_gap_notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    linked_systems: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit identity_workflows')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit identity_workflows'} 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="WorkflowName"
    >
        <Field
            name="workflow_name"
            placeholder="WorkflowName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="WorkflowType" labelFor="workflow_type">
        <Field name="workflow_type" id="workflow_type" component="select">
          
            <option value="joiner">joiner</option>
          
            <option value="mover">mover</option>
          
            <option value="leaver">leaver</option>
          
            <option value="privileged_access_assignment">privileged_access_assignment</option>
          
            <option value="privileged_access_removal">privileged_access_removal</option>
          
            <option value="mfa_enforcement">mfa_enforcement</option>
          
            <option value="quarterly_access_review">quarterly_access_review</option>
          
            <option value="service_account_review">service_account_review</option>
          
            <option value="break_glass_exception">break_glass_exception</option>
          
            <option value="emergency_access_use">emergency_access_use</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="Description" hasTextareaHeight>
        <Field name="description" as="textarea" placeholder="Description" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RiskLevel" labelFor="risk_level">
        <Field name="risk_level" id="risk_level" 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="OwnerRole" labelFor="owner_role">
        <Field name="owner_role" id="owner_role" component="select">
          
            <option value="org_admin">org_admin</option>
          
            <option value="compliance_manager">compliance_manager</option>
          
            <option value="security_manager">security_manager</option>
          
            <option value="iam_operator">iam_operator</option>
          
            <option value="auditor">auditor</option>
          
            <option value="read_only_client">read_only_client</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="SourceTriggerType" labelFor="source_trigger_type">
        <Field name="source_trigger_type" id="source_trigger_type" component="select">
          
            <option value="HR">HR</option>
          
            <option value="Ticket">Ticket</option>
          
            <option value="Manual">Manual</option>
          
            <option value="Automated">Automated</option>
          
            <option value="Scheduled">Scheduled</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="draft">draft</option>
          
            <option value="active">active</option>
          
            <option value="deprecated">deprecated</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RecurrenceType" labelFor="recurrence_type">
        <Field name="recurrence_type" id="recurrence_type" component="select">
          
            <option value="none">none</option>
          
            <option value="weekly">weekly</option>
          
            <option value="monthly">monthly</option>
          
            <option value="quarterly">quarterly</option>
          
            <option value="semi_annual">semi_annual</option>
          
            <option value="annual">annual</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TargetSLAHours"
    >
        <Field
            type="number"
            name="target_sla_hours"
            placeholder="TargetSLAHours"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="CoverageGapNotes" hasTextareaHeight>
        <Field name="coverage_gap_notes" as="textarea" placeholder="CoverageGapNotes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='LinkedSystems' labelFor='linked_systems'>
          <Field
              name='linked_systems'
              id='linked_systems'
              component={SelectFieldMany}
              options={initialValues.linked_systems}
              itemRef={'connected_systems'}
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'system_name'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </FormField>
  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='LinkedControls' labelFor='linked_controls'>
          <Field
              name='linked_controls'
              id='linked_controls'
              component={SelectFieldMany}
              options={initialValues.linked_controls}
              itemRef={'control_requirements'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'control_code'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></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('/identity_workflows/identity_workflows-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditIdentity_workflowsPage
