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/audit_logs/audit_logsSlice'
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 EditAudit_logs = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    
    event_time: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    project: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    

    

    

    
    action: '',
    

    

    

    

    
    
    
    'entity_type': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'entity_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    old_value_json: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    new_value_json: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    change_summary: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'ip_address': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    user_agent: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'session_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'checksum': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'previous_checksum': '',
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { audit_logsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit audit_logs')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit audit_logs'} main>
        {''}
        </SectionTitleLineWithButton>
        <CardBox>
          <Formik
            enableReinitialize
            initialValues={initialValues}
            onSubmit={(values) => handleSubmit(values)}
          >
            <Form>


  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
    {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='Project' labelFor='project'>
        <Field
            name='project'
            id='project'
            component={SelectField}
            options={initialValues.project}
            itemRef={'projects'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='User' labelFor='user'>
        <Field
            name='user'
            id='user'
            component={SelectField}
            options={initialValues.user}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Action" labelFor="action">
        <Field name="action" id="action" component="select">
          
            <option value="create">create</option>
          
            <option value="update">update</option>
          
            <option value="delete">delete</option>
          
            <option value="approve">approve</option>
          
            <option value="reject">reject</option>
          
            <option value="export">export</option>
          
            <option value="import">import</option>
          
            <option value="login">login</option>
          
            <option value="logout">logout</option>
          
            <option value="download">download</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="EntityType"
    >
        <Field
            name="entity_type"
            placeholder="EntityType"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="EntityKey"
    >
        <Field
            name="entity_key"
            placeholder="EntityKey"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="OldValueJSON" hasTextareaHeight>
        <Field name="old_value_json" as="textarea" placeholder="OldValueJSON" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="NewValueJSON" hasTextareaHeight>
        <Field name="new_value_json" as="textarea" placeholder="NewValueJSON" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="ChangeSummary" hasTextareaHeight>
        <Field name="change_summary" as="textarea" placeholder="ChangeSummary" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="IPAddress"
    >
        <Field
            name="ip_address"
            placeholder="IPAddress"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="UserAgent" hasTextareaHeight>
        <Field name="user_agent" as="textarea" placeholder="UserAgent" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="SessionKey"
    >
        <Field
            name="session_key"
            placeholder="SessionKey"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Checksum"
    >
        <Field
            name="checksum"
            placeholder="Checksum"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="PreviousChecksum"
    >
        <Field
            name="previous_checksum"
            placeholder="PreviousChecksum"
        />
    </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('/audit_logs/audit_logs-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditAudit_logs
