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/mole_lesion_logs/mole_lesion_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";



const EditMole_lesion_logs = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    
    'lesion_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    body_location: '',
    

    

    

    

    
    
    

    

    

    

    

    
    photo_date: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    photos: [],
    

    

    
    
    

    

    

    

    

    

    

    

    
    monitoring_interval: '',
    

    

    

    

    
    
    

    

    

    

    

    
    next_check_due: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    abcde_notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

  const { mole_lesion_logs } = useAppSelector((state) => state.mole_lesion_logs)
  

  const { mole_lesion_logsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  
    <FormField
        label="LesionName"
    >
        <Field
            name="lesion_name"
            placeholder="LesionName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="BodyLocation" labelFor="body_location">
        <Field name="body_location" id="body_location" component="select">
          
            <option value="head_neck">head_neck</option>
          
            <option value="chest">chest</option>
          
            <option value="abdomen">abdomen</option>
          
            <option value="back">back</option>
          
            <option value="left_arm">left_arm</option>
          
            <option value="right_arm">right_arm</option>
          
            <option value="left_leg">left_leg</option>
          
            <option value="right_leg">right_leg</option>
          
            <option value="hands_feet">hands_feet</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='Photos'
                color='info'
                icon={mdiUpload}
                path={'mole_lesion_logs/photos'}
                name='photos'
                id='photos'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></Field>
        </FormField>
    

  



  

  

  

  

  

  

  

  
    <FormField label="MonitoringInterval" labelFor="monitoring_interval">
        <Field name="monitoring_interval" id="monitoring_interval" component="select">
          
            <option value="3_months">3_months</option>
          
            <option value="6_months">6_months</option>
          
            <option value="12_months">12_months</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  
    <FormField label="ABCDENotes" hasTextareaHeight>
        <Field name="abcde_notes" as="textarea" placeholder="ABCDENotes" />
    </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('/mole_lesion_logs/mole_lesion_logs-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditMole_lesion_logs
