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/admin_metrics_snapshots/admin_metrics_snapshotsSlice'
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 EditAdmin_metrics_snapshotsPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    
    snapshot_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    
    total_accounts: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    total_users: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    total_patients: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    active_devices: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    alerts_last_24h: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    cognitive_sessions_last_7d: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    reminders_sent_last_24h: '',
    

    

    

    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    accounts: null,
    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Totalaccounts"
    >
        <Field
            type="number"
            name="total_accounts"
            placeholder="Totalaccounts"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Totalusers"
    >
        <Field
            type="number"
            name="total_users"
            placeholder="Totalusers"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Totalpatients"
    >
        <Field
            type="number"
            name="total_patients"
            placeholder="Totalpatients"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Activedevices"
    >
        <Field
            type="number"
            name="active_devices"
            placeholder="Activedevices"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Alertslast24h"
    >
        <Field
            type="number"
            name="alerts_last_24h"
            placeholder="Alertslast24h"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Cognitivesessionslast7d"
    >
        <Field
            type="number"
            name="cognitive_sessions_last_7d"
            placeholder="Cognitivesessionslast7d"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Reminderssentlast24h"
    >
        <Field
            type="number"
            name="reminders_sent_last_24h"
            placeholder="Reminderssentlast24h"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

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

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

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

export default EditAdmin_metrics_snapshotsPage
