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/medical_camps/medical_campsSlice'
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 EditMedical_campsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'camp_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    created_by_admin: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    lead_city_doctor: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    lead_rural_doctor: null,
    

    
    
    

    

    

    

    

    
    start_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    end_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    
    'location_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'address': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'latitude': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'longitude': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    expected_attendees: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    actual_attendees: '',
    

    

    

    

    

    

    

    

    

    
    
    

    
    services_offered: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  
    <FormField
        label="CampName"
    >
        <Field
            name="camp_name"
            placeholder="CampName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='CreatedByAdmin' labelFor='created_by_admin'>
        <Field
            name='created_by_admin'
            id='created_by_admin'
            component={SelectField}
            options={initialValues.created_by_admin}
            itemRef={'admins'}
      
        
      
        
      
        
      
        
            showField={'full_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='LeadCityDoctor' labelFor='lead_city_doctor'>
        <Field
            name='lead_city_doctor'
            id='lead_city_doctor'
            component={SelectField}
            options={initialValues.lead_city_doctor}
            itemRef={'city_doctors'}
      
        
      
        
      
        
      
        
      
        
            showField={'full_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='LeadRuralDoctor' labelFor='lead_rural_doctor'>
        <Field
            name='lead_rural_doctor'
            id='lead_rural_doctor'
            component={SelectField}
            options={initialValues.lead_rural_doctor}
            itemRef={'rural_doctors'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'full_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="planned">planned</option>
          
            <option value="scheduled">scheduled</option>
          
            <option value="active">active</option>
          
            <option value="completed">completed</option>
          
            <option value="cancelled">cancelled</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="LocationName"
    >
        <Field
            name="location_name"
            placeholder="LocationName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Address"
    >
        <Field
            name="address"
            placeholder="Address"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Latitude"
    >
        <Field
            type="number"
            name="latitude"
            placeholder="Latitude"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Longitude"
    >
        <Field
            type="number"
            name="longitude"
            placeholder="Longitude"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ExpectedAttendees"
    >
        <Field
            type="number"
            name="expected_attendees"
            placeholder="ExpectedAttendees"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ActualAttendees"
    >
        <Field
            type="number"
            name="actual_attendees"
            placeholder="ActualAttendees"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="ServicesOffered" hasTextareaHeight>
        <Field name="services_offered" as="textarea" placeholder="ServicesOffered" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

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

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

export default EditMedical_campsPage
