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/server_caches/server_cachesSlice'
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 EditServer_cachesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    
    'cache_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    cache_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    namespace: '',
    

    

    

    

    
    
    

    

    

    

    

    
    expires_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    refreshed_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'value_hash': '',
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  
    <FormField
        label="CacheKey"
    >
        <Field
            name="cache_key"
            placeholder="CacheKey"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="CacheType" labelFor="cache_type">
        <Field name="cache_type" id="cache_type" component="select">
          
            <option value="redis">redis</option>
          
            <option value="memory">memory</option>
          
            <option value="database">database</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Namespace" labelFor="namespace">
        <Field name="namespace" id="namespace" component="select">
          
            <option value="mapper_totals">mapper_totals</option>
          
            <option value="artist_totals">artist_totals</option>
          
            <option value="game_pool">game_pool</option>
          
            <option value="osu_token">osu_token</option>
          
            <option value="rate_limits">rate_limits</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  
    <FormField
        label="ValueHash"
    >
        <Field
            name="value_hash"
            placeholder="ValueHash"
        />
    </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('/server_caches/server_caches-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditServer_cachesPage
