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/camera_scan_sessions/camera_scan_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 EditCamera_scan_sessions = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    user: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    book: null,
    

    
    
    

    

    

    

    

    

    

    

    
    scan_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    started_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    ended_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    
    frames_analyzed: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    pages_detected: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'confidence_score': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    page_turning_detected: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    duplicate_page_detected: false,
    

    

    

    

    

    

    
    
    

    
    fraud_reason: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    coins_awarded: '',
    

    

    

    

    

    

    

    

    

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

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

  const { camera_scan_sessionsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Book' labelFor='book'>
        <Field
            name='book'
            id='book'
            component={SelectField}
            options={initialValues.book}
            itemRef={'books'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Scantype" labelFor="scan_type">
        <Field name="scan_type" id="scan_type" component="select">
          
            <option value="cover">cover</option>
          
            <option value="page">page</option>
          
            <option value="shelf">shelf</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="started">started</option>
          
            <option value="verifying">verifying</option>
          
            <option value="failed">failed</option>
          
            <option value="completed">completed</option>
          
            <option value="flagged">flagged</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  
      <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="Framesanalyzed"
    >
        <Field
            type="number"
            name="frames_analyzed"
            placeholder="Framesanalyzed"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Pagesdetected"
    >
        <Field
            type="number"
            name="pages_detected"
            placeholder="Pagesdetected"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Confidencescore"
    >
        <Field
            type="number"
            name="confidence_score"
            placeholder="Confidencescore"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Pageturningdetected' labelFor='page_turning_detected'>
        <Field
            name='page_turning_detected'
            id='page_turning_detected'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Duplicatepagedetected' labelFor='duplicate_page_detected'>
        <Field
            name='duplicate_page_detected'
            id='duplicate_page_detected'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  
    <FormField label="Fraudreason" hasTextareaHeight>
        <Field name="fraud_reason" as="textarea" placeholder="Fraudreason" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Coinsawarded"
    >
        <Field
            type="number"
            name="coins_awarded"
            placeholder="Coinsawarded"
        />
    </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('/camera_scan_sessions/camera_scan_sessions-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditCamera_scan_sessions
