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/geofence_alerts/geofence_alertsSlice'
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 EditGeofence_alertsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    restaurant: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    staff_user: null,
    

    
    
    
    'distance_meters': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'latitude': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'longitude': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    alert_time: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    severity: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    is_acknowledged: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    acknowledged_by_user: null,
    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Restaurant' labelFor='restaurant'>
        <Field
            name='restaurant'
            id='restaurant'
            component={SelectField}
            options={initialValues.restaurant}
            itemRef={'restaurants'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  
    <FormField
        label="Distance(mètres)"
    >
        <Field
            type="number"
            name="distance_meters"
            placeholder="Distance(mètres)"
        />
    </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="Date/heurealerte"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.alert_time ?
                  new Date(
                      dayjs(initialValues.alert_time).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'alert_time': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Sévérité" labelFor="severity">
        <Field name="severity" id="severity" component="select">
          
            <option value="info">info</option>
          
            <option value="warning">warning</option>
          
            <option value="critical">critical</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Alerteacquittée' labelFor='is_acknowledged'>
        <Field
            name='is_acknowledged'
            id='is_acknowledged'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

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

export default EditGeofence_alertsPage
