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/place_opening_hours/place_opening_hoursSlice'
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 EditPlace_opening_hours = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    place: null,
    

    
    
    

    

    

    

    

    

    

    

    
    day_of_week: '',
    

    

    

    

    
    
    
    'open_time': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'close_time': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_closed: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_24_hours: false,
    

    

    

    

    

    

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

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

  const { place_opening_hoursId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Place' labelFor='place'>
        <Field
            name='place'
            id='place'
            component={SelectField}
            options={initialValues.place}
            itemRef={'places'}
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="DayofWeek" labelFor="day_of_week">
        <Field name="day_of_week" id="day_of_week" component="select">
          
            <option value="monday">monday</option>
          
            <option value="tuesday">tuesday</option>
          
            <option value="wednesday">wednesday</option>
          
            <option value="thursday">thursday</option>
          
            <option value="friday">friday</option>
          
            <option value="saturday">saturday</option>
          
            <option value="sunday">sunday</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="OpenTime"
    >
        <Field
            name="open_time"
            placeholder="OpenTime"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="CloseTime"
    >
        <Field
            name="close_time"
            placeholder="CloseTime"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsClosed' labelFor='is_closed'>
        <Field
            name='is_closed'
            id='is_closed'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Is24Hours' labelFor='is_24_hours'>
        <Field
            name='is_24_hours'
            id='is_24_hours'
            component={SwitchField}
        ></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('/place_opening_hours/place_opening_hours-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditPlace_opening_hours
