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/conference_nodes/conference_nodesSlice'
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 EditConference_nodesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    node_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    region: '',
    

    

    

    

    
    
    
    'zone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'endpoint': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    health_status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    last_heartbeat_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'cpu_utilization': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'memory_utilization': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    current_conferences: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { id } = router.query

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

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

  useEffect(() => {
      if (typeof conference_nodes === 'object') {
          const newInitialVal = {...initVals};
          Object.keys(initVals).forEach(el => newInitialVal[el] = (conference_nodes)[el])
          setInitialValues(newInitialVal);
      }
  }, [conference_nodes])

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

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


  
    <FormField
        label="NodeName"
    >
        <Field
            name="name"
            placeholder="NodeName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="NodeType" labelFor="node_type">
        <Field name="node_type" id="node_type" component="select">
          
            <option value="sfu">sfu</option>
          
            <option value="turn">turn</option>
          
            <option value="signaling_gateway">signaling_gateway</option>
          
            <option value="recorder_worker">recorder_worker</option>
          
            <option value="ai_service">ai_service</option>
          
            <option value="edge_cache">edge_cache</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Region" labelFor="region">
        <Field name="region" id="region" component="select">
          
            <option value="us">us</option>
          
            <option value="eu">eu</option>
          
            <option value="apac">apac</option>
          
            <option value="sa">sa</option>
          
            <option value="me">me</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Zone"
    >
        <Field
            name="zone"
            placeholder="Zone"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Endpoint"
    >
        <Field
            name="endpoint"
            placeholder="Endpoint"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="HealthStatus" labelFor="health_status">
        <Field name="health_status" id="health_status" component="select">
          
            <option value="healthy">healthy</option>
          
            <option value="degraded">degraded</option>
          
            <option value="unhealthy">unhealthy</option>
          
            <option value="draining">draining</option>
          
            <option value="offline">offline</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="CPUUtilization"
    >
        <Field
            type="number"
            name="cpu_utilization"
            placeholder="CPUUtilization"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MemoryUtilization"
    >
        <Field
            type="number"
            name="memory_utilization"
            placeholder="MemoryUtilization"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="CurrentConferences"
    >
        <Field
            type="number"
            name="current_conferences"
            placeholder="CurrentConferences"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

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

export default EditConference_nodesPage
