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/water_quality_logs/water_quality_logsSlice'
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 EditWater_quality_logsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    tenant: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    pond: null,
    

    
    
    

    

    

    

    

    
    sampled_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'temperature_c': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'ph': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'dissolved_oxygen_mg_l': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'salinity_ppt': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'ammonia_mg_l': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'nitrite_mg_l': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'nitrate_mg_l': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'turbidity_ntu': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    recorded_by: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Tenant' labelFor='tenant'>
        <Field
            name='tenant'
            id='tenant'
            component={SelectField}
            options={initialValues.tenant}
            itemRef={'tenants'}
      
        
      
        
      
        
      
        
      
        
            showField={'tenant_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Pond' labelFor='pond'>
        <Field
            name='pond'
            id='pond'
            component={SelectField}
            options={initialValues.pond}
            itemRef={'ponds'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'pond_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TemperatureC"
    >
        <Field
            type="number"
            name="temperature_c"
            placeholder="TemperatureC"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="pH"
    >
        <Field
            type="number"
            name="ph"
            placeholder="pH"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="DissolvedOxygenMgL"
    >
        <Field
            type="number"
            name="dissolved_oxygen_mg_l"
            placeholder="DissolvedOxygenMgL"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SalinityPpt"
    >
        <Field
            type="number"
            name="salinity_ppt"
            placeholder="SalinityPpt"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="AmmoniaMgL"
    >
        <Field
            type="number"
            name="ammonia_mg_l"
            placeholder="AmmoniaMgL"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="NitriteMgL"
    >
        <Field
            type="number"
            name="nitrite_mg_l"
            placeholder="NitriteMgL"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="NitrateMgL"
    >
        <Field
            type="number"
            name="nitrate_mg_l"
            placeholder="NitrateMgL"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="TurbidityNTU"
    >
        <Field
            type="number"
            name="turbidity_ntu"
            placeholder="TurbidityNTU"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Notes" hasTextareaHeight>
        <Field name="notes" as="textarea" placeholder="Notes" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='RecordedBy' labelFor='recorded_by'>
        <Field
            name='recorded_by'
            id='recorded_by'
            component={SelectField}
            options={initialValues.recorded_by}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></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('/water_quality_logs/water_quality_logs-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditWater_quality_logsPage
