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/equipment_items/equipment_itemsSlice'
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 EditEquipment_items = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    

    

    

    

    

    

    

    

    
    equipment_type: '',
    

    

    

    

    
    
    
    'manufacturer': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'model': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'capacity': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'capacity_unit': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'power_kw': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'base_capex_usd': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'install_factor': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'opex_per_unit': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'opex_unit': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'vendor_quote_reference': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    datasheet_files: [],
    

    

    

    
    
    

    

    

    

    

    

    
    is_active: false,
    

    

    

    

    

    

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

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

  const { equipment_itemsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit equipment_items')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit equipment_items'} 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="EquipmentType" labelFor="equipment_type">
        <Field name="equipment_type" id="equipment_type" component="select">
          
            <option value="crusher">crusher</option>
          
            <option value="grinder">grinder</option>
          
            <option value="screen">screen</option>
          
            <option value="classifier">classifier</option>
          
            <option value="pump">pump</option>
          
            <option value="thickener">thickener</option>
          
            <option value="filter">filter</option>
          
            <option value="flotation_cell">flotation_cell</option>
          
            <option value="leach_tank">leach_tank</option>
          
            <option value="adsorption_column">adsorption_column</option>
          
            <option value="elution">elution</option>
          
            <option value="electrowinning">electrowinning</option>
          
            <option value="smelter">smelter</option>
          
            <option value="conveyor">conveyor</option>
          
            <option value="reagent_system">reagent_system</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="Manufacturer"
    >
        <Field
            name="manufacturer"
            placeholder="Manufacturer"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Model"
    >
        <Field
            name="model"
            placeholder="Model"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Capacity"
    >
        <Field
            type="number"
            name="capacity"
            placeholder="Capacity"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="CapacityUnit"
    >
        <Field
            name="capacity_unit"
            placeholder="CapacityUnit"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Power(kW)"
    >
        <Field
            type="number"
            name="power_kw"
            placeholder="Power(kW)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="BaseCAPEX(USD)"
    >
        <Field
            type="number"
            name="base_capex_usd"
            placeholder="BaseCAPEX(USD)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="InstallFactor"
    >
        <Field
            type="number"
            name="install_factor"
            placeholder="InstallFactor"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="OPEXPerUnit"
    >
        <Field
            type="number"
            name="opex_per_unit"
            placeholder="OPEXPerUnit"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="OPEXUnit"
    >
        <Field
            name="opex_unit"
            placeholder="OPEXUnit"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="VendorQuoteReference"
    >
        <Field
            name="vendor_quote_reference"
            placeholder="VendorQuoteReference"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='DatasheetFiles'
            color='info'
            icon={mdiUpload}
            path={'equipment_items/datasheet_files'}
            name='datasheet_files'
            id='datasheet_files'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  

  

  

  

  

  

  

  

  
    <FormField label='IsActive' labelFor='is_active'>
        <Field
            name='is_active'
            id='is_active'
            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('/equipment_items/equipment_items-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditEquipment_items
