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/files/filesSlice'
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 EditFilesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    owner: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    matter: null,
    

    
    
    
    'file_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'content_type': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'file_extension': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    size_bytes: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'storage_provider': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'storage_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'checksum_sha256': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_ocr_required: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    processing_status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    uploaded_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    tags: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Owner' labelFor='owner'>
        <Field
            name='owner'
            id='owner'
            component={SelectField}
            options={initialValues.owner}
            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="FileName"
    >
        <Field
            name="file_name"
            placeholder="FileName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ContentType"
    >
        <Field
            name="content_type"
            placeholder="ContentType"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="FileExtension"
    >
        <Field
            name="file_extension"
            placeholder="FileExtension"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SizeBytes"
    >
        <Field
            type="number"
            name="size_bytes"
            placeholder="SizeBytes"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="StorageProvider"
    >
        <Field
            name="storage_provider"
            placeholder="StorageProvider"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="StorageKey"
    >
        <Field
            name="storage_key"
            placeholder="StorageKey"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ChecksumSha256"
    >
        <Field
            name="checksum_sha256"
            placeholder="ChecksumSha256"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsOcrRequired' labelFor='is_ocr_required'>
        <Field
            name='is_ocr_required'
            id='is_ocr_required'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ProcessingStatus" labelFor="processing_status">
        <Field name="processing_status" id="processing_status" component="select">
          
            <option value="queued">queued</option>
          
            <option value="processing">processing</option>
          
            <option value="completed">completed</option>
          
            <option value="failed">failed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  
    <FormField label="Tags" hasTextareaHeight>
        <Field name="tags" as="textarea" placeholder="Tags" />
    </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('/files/files-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditFilesPage
