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/assets/assetsSlice'
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 EditAssets = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    asset_type: '',
    

    

    

    

    
    
    
    'environment': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'ip_address': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'hostname': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'os': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'cloud_provider': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    criticality: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    internet_exposed: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    sensor: null,
    

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

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

  const { assetsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="AssetType" labelFor="asset_type">
        <Field name="asset_type" id="asset_type" component="select">
          
            <option value="host">host</option>
          
            <option value="server">server</option>
          
            <option value="workstation">workstation</option>
          
            <option value="container">container</option>
          
            <option value="kubernetes_cluster">kubernetes_cluster</option>
          
            <option value="database">database</option>
          
            <option value="application">application</option>
          
            <option value="api">api</option>
          
            <option value="network_device">network_device</option>
          
            <option value="iot">iot</option>
          
            <option value="edge_sensor">edge_sensor</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Environment"
    >
        <Field
            name="environment"
            placeholder="Environment"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Hostname"
    >
        <Field
            name="hostname"
            placeholder="Hostname"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="OperatingSystem"
    >
        <Field
            name="os"
            placeholder="OperatingSystem"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="CloudProvider"
    >
        <Field
            name="cloud_provider"
            placeholder="CloudProvider"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Criticality" labelFor="criticality">
        <Field name="criticality" id="criticality" 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='InternetExposed' labelFor='internet_exposed'>
        <Field
            name='internet_exposed'
            id='internet_exposed'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='EdgeSensor' labelFor='sensor'>
        <Field
            name='sensor'
            id='sensor'
            component={SelectField}
            options={initialValues.sensor}
            itemRef={'edge_sensors'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        ></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('/assets/assets-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditAssets
