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/citator_checks/citator_checksSlice'
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 EditCitator_checks = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    requested_by: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    matter: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    legal_source: null,
    

    
    
    

    

    

    

    

    

    

    

    
    standing: '',
    

    

    

    

    
    
    

    
    analysis: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    conflicts_summary: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    checked_at: new Date(),
    

    

    

    

    

    

    

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

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

  const { citator_checksId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

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

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Matter' labelFor='matter'>
        <Field
            name='matter'
            id='matter'
            component={SelectField}
            options={initialValues.matter}
            itemRef={'matters'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='LegalSource' labelFor='legal_source'>
        <Field
            name='legal_source'
            id='legal_source'
            component={SelectField}
            options={initialValues.legal_source}
            itemRef={'legal_sources'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Standing" labelFor="standing">
        <Field name="standing" id="standing" component="select">
          
            <option value="good_law">good_law</option>
          
            <option value="caution">caution</option>
          
            <option value="overruled">overruled</option>
          
            <option value="distinguished">distinguished</option>
          
            <option value="unknown">unknown</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="Analysis" hasTextareaHeight>
        <Field name="analysis" as="textarea" placeholder="Analysis" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="ConflictsSummary" hasTextareaHeight>
        <Field name="conflicts_summary" as="textarea" placeholder="ConflictsSummary" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="CheckedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.checked_at ?
                  new Date(
                      dayjs(initialValues.checked_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'checked_at': date})}
          />
      </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('/citator_checks/citator_checks-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditCitator_checks
