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/workflow_templates/workflow_templatesSlice'
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 EditWorkflow_templates = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    
    'template_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    workflow_type: '',
    

    

    

    

    
    
    

    
    description: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    risk_level: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    owner_role: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    source_trigger_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    recurrence_type: '',
    

    

    

    

    
    
    

    
    default_required_fields: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { workflow_templatesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit workflow_templates')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit workflow_templates'} 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="TemplateName"
    >
        <Field
            name="template_name"
            placeholder="TemplateName"
        />
    </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="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="DefaultRequiredFields" hasTextareaHeight>
        <Field name="default_required_fields" as="textarea" placeholder="DefaultRequiredFields" />
    </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('/workflow_templates/workflow_templates-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditWorkflow_templates
