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/connected_systems/connected_systemsSlice'
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 EditConnected_systems = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    
    'system_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    system_type: '',
    

    

    

    

    
    
    
    'vendor': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    environment: '',
    

    

    

    

    
    
    
    'owner': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    connection_status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    last_successful_sync_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    connection_health_notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    source_of_truth_flag: false,
    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { connected_systemsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="SystemType" labelFor="system_type">
        <Field name="system_type" id="system_type" component="select">
          
            <option value="entra_id">entra_id</option>
          
            <option value="microsoft_365_audit">microsoft_365_audit</option>
          
            <option value="okta">okta</option>
          
            <option value="google_workspace">google_workspace</option>
          
            <option value="hris">hris</option>
          
            <option value="ticketing_psa">ticketing_psa</option>
          
            <option value="pam">pam</option>
          
            <option value="endpoint_mdm">endpoint_mdm</option>
          
            <option value="siem">siem</option>
          
            <option value="backup_recovery">backup_recovery</option>
          
            <option value="custom_source">custom_source</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Vendor"
    >
        <Field
            name="vendor"
            placeholder="Vendor"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Environment" labelFor="environment">
        <Field name="environment" id="environment" component="select">
          
            <option value="production">production</option>
          
            <option value="staging">staging</option>
          
            <option value="sandbox">sandbox</option>
          
            <option value="unknown">unknown</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Owner"
    >
        <Field
            name="owner"
            placeholder="Owner"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ConnectionStatus" labelFor="connection_status">
        <Field name="connection_status" id="connection_status" component="select">
          
            <option value="connected">connected</option>
          
            <option value="degraded">degraded</option>
          
            <option value="disconnected">disconnected</option>
          
            <option value="not_configured">not_configured</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  
    <FormField label="ConnectionHealthNotes" hasTextareaHeight>
        <Field name="connection_health_notes" as="textarea" placeholder="ConnectionHealthNotes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='SourceofTruthFlag' labelFor='source_of_truth_flag'>
        <Field
            name='source_of_truth_flag'
            id='source_of_truth_flag'
            component={SwitchField}
        ></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('/connected_systems/connected_systems-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditConnected_systems
