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/capex_items/capex_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 EditCapex_items = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    economic_model: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    equipment_item: null,
    

    
    
    
    'cost_category': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    description: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    quantity: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'unit_cost': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'install_factor': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'total_cost': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    estimate_class: '',
    

    

    

    

    
    
    

    

    
    basis_notes: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { capex_itemsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='EconomicModel' labelFor='economic_model'>
        <Field
            name='economic_model'
            id='economic_model'
            component={SelectField}
            options={initialValues.economic_model}
            itemRef={'economic_models'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </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="CostCategory"
    >
        <Field
            name="cost_category"
            placeholder="CostCategory"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='Description' hasTextareaHeight>
        <Field
            name='description'
            id='description'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Quantity"
    >
        <Field
            type="number"
            name="quantity"
            placeholder="Quantity"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="UnitCost"
    >
        <Field
            type="number"
            name="unit_cost"
            placeholder="UnitCost"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

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

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TotalCost"
    >
        <Field
            type="number"
            name="total_cost"
            placeholder="TotalCost"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="EstimateClass" labelFor="estimate_class">
        <Field name="estimate_class" id="estimate_class" component="select">
          
            <option value="class_5">class_5</option>
          
            <option value="class_4">class_4</option>
          
            <option value="class_3">class_3</option>
          
            <option value="class_2">class_2</option>
          
            <option value="class_1">class_1</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  
    <FormField label='BasisNotes' hasTextareaHeight>
        <Field
            name='basis_notes'
            id='basis_notes'
            component={RichTextField}
        ></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('/capex_items/capex_items-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditCapex_items
