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/reading_sessions/reading_sessionsSlice'
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 EditReading_sessions = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    
    started_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    ended_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    reading_mode: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    page_fit: '',
    

    

    

    

    
    
    
    'zoom_level': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    direction: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    crop_borders: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    keep_screen_on: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    show_page_number: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    skip_read_chapters: false,
    

    

    

    

    

    

    
    
    

    

    

    
    start_page_index: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    end_page_index: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    chapter: null,
    

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

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

  const { reading_sessionsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ReadingMode" labelFor="reading_mode">
        <Field name="reading_mode" id="reading_mode" component="select">
          
            <option value="vertical_scroll">vertical_scroll</option>
          
            <option value="horizontal_paged">horizontal_paged</option>
          
            <option value="horizontal_scroll">horizontal_scroll</option>
          
            <option value="webtoon">webtoon</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PageFit" labelFor="page_fit">
        <Field name="page_fit" id="page_fit" component="select">
          
            <option value="fit_width">fit_width</option>
          
            <option value="fit_height">fit_height</option>
          
            <option value="fit_screen">fit_screen</option>
          
            <option value="original">original</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  
    <FormField
        label="ZoomLevel"
    >
        <Field
            type="number"
            name="zoom_level"
            placeholder="ZoomLevel"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Direction" labelFor="direction">
        <Field name="direction" id="direction" component="select">
          
            <option value="ltr">ltr</option>
          
            <option value="rtl">rtl</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='CropBorders' labelFor='crop_borders'>
        <Field
            name='crop_borders'
            id='crop_borders'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='KeepScreenOn' labelFor='keep_screen_on'>
        <Field
            name='keep_screen_on'
            id='keep_screen_on'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='ShowPageNumber' labelFor='show_page_number'>
        <Field
            name='show_page_number'
            id='show_page_number'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='SkipReadChapters' labelFor='skip_read_chapters'>
        <Field
            name='skip_read_chapters'
            id='skip_read_chapters'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  
    <FormField
        label="StartPageIndex"
    >
        <Field
            type="number"
            name="start_page_index"
            placeholder="StartPageIndex"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="EndPageIndex"
    >
        <Field
            type="number"
            name="end_page_index"
            placeholder="EndPageIndex"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

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

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

export default EditReading_sessions
