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/telemetry_events/telemetry_eventsSlice'
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 EditTelemetry_events = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    data_source: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    asset: null,
    

    
    
    

    

    

    

    

    
    observed_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    event_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    severity: '',
    

    

    

    

    
    
    
    'summary': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    raw_payload: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'hash': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    flagged: false,
    

    

    

    

    

    

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

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

  const { telemetry_eventsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Asset' labelFor='asset'>
        <Field
            name='asset'
            id='asset'
            component={SelectField}
            options={initialValues.asset}
            itemRef={'assets'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="EventType" labelFor="event_type">
        <Field name="event_type" id="event_type" component="select">
          
            <option value="auth">auth</option>
          
            <option value="process">process</option>
          
            <option value="network">network</option>
          
            <option value="dns">dns</option>
          
            <option value="http">http</option>
          
            <option value="email">email</option>
          
            <option value="file">file</option>
          
            <option value="cloud">cloud</option>
          
            <option value="kubernetes">kubernetes</option>
          
            <option value="iam">iam</option>
          
            <option value="vulnerability">vulnerability</option>
          
            <option value="threat_feed">threat_feed</option>
          
            <option value="dark_web">dark_web</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Severity" labelFor="severity">
        <Field name="severity" id="severity" component="select">
          
            <option value="informational">informational</option>
          
            <option value="low">low</option>
          
            <option value="medium">medium</option>
          
            <option value="high">high</option>
          
            <option value="critical">critical</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Summary"
    >
        <Field
            name="summary"
            placeholder="Summary"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="RawPayload" hasTextareaHeight>
        <Field name="raw_payload" as="textarea" placeholder="RawPayload" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="EventHash"
    >
        <Field
            name="hash"
            placeholder="EventHash"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

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

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

export default EditTelemetry_events
