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/flowsheet_nodes/flowsheet_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 EditFlowsheet_nodesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    flowsheet: null,
    

    
    
    
    'node_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    node_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    unit_operation: '',
    

    

    

    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'position_x': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'position_y': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    parameters_json: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    equipment_item: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

    
    test_basis: [],
    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Flowsheet' labelFor='flowsheet'>
        <Field
            name='flowsheet'
            id='flowsheet'
            component={SelectField}
            options={initialValues.flowsheet}
            itemRef={'flowsheets'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="NodeKey"
    >
        <Field
            name="node_key"
            placeholder="NodeKey"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="NodeType" labelFor="node_type">
        <Field name="node_type" id="node_type" component="select">
          
            <option value="unit_operation">unit_operation</option>
          
            <option value="source">source</option>
          
            <option value="sink">sink</option>
          
            <option value="junction">junction</option>
          
            <option value="annotation">annotation</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="UnitOperation" labelFor="unit_operation">
        <Field name="unit_operation" id="unit_operation" component="select">
          
            <option value="crushing">crushing</option>
          
            <option value="sag_mill">sag_mill</option>
          
            <option value="ball_mill">ball_mill</option>
          
            <option value="hpgr">hpgr</option>
          
            <option value="cyclone">cyclone</option>
          
            <option value="screen">screen</option>
          
            <option value="gravity_concentrator">gravity_concentrator</option>
          
            <option value="shaking_table">shaking_table</option>
          
            <option value="flotation">flotation</option>
          
            <option value="cil">cil</option>
          
            <option value="cip">cip</option>
          
            <option value="heap_leach">heap_leach</option>
          
            <option value="thickener">thickener</option>
          
            <option value="filter">filter</option>
          
            <option value="adsorption">adsorption</option>
          
            <option value="elution">elution</option>
          
            <option value="electrowinning">electrowinning</option>
          
            <option value="smelting">smelting</option>
          
            <option value="tailings">tailings</option>
          
            <option value="reagent_makeup">reagent_makeup</option>
          
            <option value="water_balance">water_balance</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PositionX"
    >
        <Field
            type="number"
            name="position_x"
            placeholder="PositionX"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="PositionY"
    >
        <Field
            type="number"
            name="position_y"
            placeholder="PositionY"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="ParametersJSON" hasTextareaHeight>
        <Field name="parameters_json" as="textarea" placeholder="ParametersJSON" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='EquipmentItem' labelFor='equipment_item'>
        <Field
            name='equipment_item'
            id='equipment_item'
            component={SelectField}
            options={initialValues.equipment_item}
            itemRef={'equipment_items'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'model'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='TestBasis' labelFor='test_basis'>
          <Field
              name='test_basis'
              id='test_basis'
              component={SelectFieldMany}
              options={initialValues.test_basis}
              itemRef={'metallurgical_tests'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'lab_reference'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></Field>
      </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('/flowsheet_nodes/flowsheet_nodes-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditFlowsheet_nodesPage
