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/economic_models/economic_modelsSlice'
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 EditEconomic_models = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    project: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    phase: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    flowsheet: null,
    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    currency: '',
    

    

    

    

    
    
    
    'gold_price': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'discount_rate_pct': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'throughput_tpd': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'recovery_pct': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'mine_life_years': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'capex_total': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'opex_per_tonne': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'npv': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'irr_pct': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'payback_years': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    assumptions_json: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    sensitivity_json: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { economic_modelsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Project' labelFor='project'>
        <Field
            name='project'
            id='project'
            component={SelectField}
            options={initialValues.project}
            itemRef={'projects'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Phase' labelFor='phase'>
        <Field
            name='phase'
            id='phase'
            component={SelectField}
            options={initialValues.phase}
            itemRef={'project_phases'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'gate_notes'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Currency" labelFor="currency">
        <Field name="currency" id="currency" component="select">
          
            <option value="USD">USD</option>
          
            <option value="CAD">CAD</option>
          
            <option value="EUR">EUR</option>
          
            <option value="AUD">AUD</option>
          
            <option value="ZAR">ZAR</option>
          
            <option value="BRL">BRL</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="GoldPrice"
    >
        <Field
            type="number"
            name="gold_price"
            placeholder="GoldPrice"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DiscountRate(%)"
    >
        <Field
            type="number"
            name="discount_rate_pct"
            placeholder="DiscountRate(%)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Throughput(tpd)"
    >
        <Field
            type="number"
            name="throughput_tpd"
            placeholder="Throughput(tpd)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Recovery(%)"
    >
        <Field
            type="number"
            name="recovery_pct"
            placeholder="Recovery(%)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="MineLife(years)"
    >
        <Field
            type="number"
            name="mine_life_years"
            placeholder="MineLife(years)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TotalCAPEX"
    >
        <Field
            type="number"
            name="capex_total"
            placeholder="TotalCAPEX"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="OPEXPerTonne"
    >
        <Field
            type="number"
            name="opex_per_tonne"
            placeholder="OPEXPerTonne"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="NPV"
    >
        <Field
            type="number"
            name="npv"
            placeholder="NPV"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="IRR(%)"
    >
        <Field
            type="number"
            name="irr_pct"
            placeholder="IRR(%)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Payback(years)"
    >
        <Field
            type="number"
            name="payback_years"
            placeholder="Payback(years)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="AssumptionsJSON" hasTextareaHeight>
        <Field name="assumptions_json" as="textarea" placeholder="AssumptionsJSON" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="SensitivityJSON" hasTextareaHeight>
        <Field name="sensitivity_json" as="textarea" placeholder="SensitivityJSON" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="draft">draft</option>
          
            <option value="in_review">in_review</option>
          
            <option value="approved">approved</option>
          
            <option value="archived">archived</option>
          
        </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('/economic_models/economic_models-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditEconomic_models
