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/metallurgical_tests/metallurgical_testsSlice'
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 EditMetallurgical_testsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    project: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    phase: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    sample: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    laboratory: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    test_type_schema: null,
    

    
    
    
    'lab_reference': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    date_submitted: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    date_completed: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    data_source: '',
    

    

    

    

    
    
    

    
    parameters_json: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    results_json: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    qaqc_status: '',
    

    

    

    

    
    
    

    

    
    qaqc_notes: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    entered_by: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    reviewed_by: null,
    

    
    
    

    

    

    

    

    
    reviewed_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    approved_by: null,
    

    
    
    

    

    

    

    

    
    approved_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_deleted: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { id } = router.query

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

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit metallurgical_tests')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit metallurgical_tests'} 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='Sample' labelFor='sample'>
        <Field
            name='sample'
            id='sample'
            component={SelectField}
            options={initialValues.sample}
            itemRef={'samples'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'sample_code'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Laboratory' labelFor='laboratory'>
        <Field
            name='laboratory'
            id='laboratory'
            component={SelectField}
            options={initialValues.laboratory}
            itemRef={'laboratories'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='TestTypeSchema' labelFor='test_type_schema'>
        <Field
            name='test_type_schema'
            id='test_type_schema'
            component={SelectField}
            options={initialValues.test_type_schema}
            itemRef={'test_type_schemas'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'test_type'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="LabReference"
    >
        <Field
            name="lab_reference"
            placeholder="LabReference"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="draft">draft</option>
          
            <option value="submitted">submitted</option>
          
            <option value="in_progress">in_progress</option>
          
            <option value="completed">completed</option>
          
            <option value="reviewed">reviewed</option>
          
            <option value="approved">approved</option>
          
            <option value="rejected">rejected</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="DataSource" labelFor="data_source">
        <Field name="data_source" id="data_source" component="select">
          
            <option value="manual">manual</option>
          
            <option value="csv_import">csv_import</option>
          
            <option value="lims_import">lims_import</option>
          
            <option value="api">api</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

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

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="ResultsJSON" hasTextareaHeight>
        <Field name="results_json" as="textarea" placeholder="ResultsJSON" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="QAQCStatus" labelFor="qaqc_status">
        <Field name="qaqc_status" id="qaqc_status" component="select">
          
            <option value="pending">pending</option>
          
            <option value="passed">passed</option>
          
            <option value="failed">failed</option>
          
            <option value="needs_review">needs_review</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  
    <FormField label='QAQCNotes' hasTextareaHeight>
        <Field
            name='qaqc_notes'
            id='qaqc_notes'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='EnteredBy' labelFor='entered_by'>
        <Field
            name='entered_by'
            id='entered_by'
            component={SelectField}
            options={initialValues.entered_by}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='ReviewedBy' labelFor='reviewed_by'>
        <Field
            name='reviewed_by'
            id='reviewed_by'
            component={SelectField}
            options={initialValues.reviewed_by}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='ApprovedBy' labelFor='approved_by'>
        <Field
            name='approved_by'
            id='approved_by'
            component={SelectField}
            options={initialValues.approved_by}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsDeleted' labelFor='is_deleted'>
        <Field
            name='is_deleted'
            id='is_deleted'
            component={SwitchField}
        ></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('/metallurgical_tests/metallurgical_tests-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditMetallurgical_testsPage
