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/pod_files/pod_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 EditPod_filesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    company: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    stop: null,
    

    
    
    

    

    

    

    

    

    

    

    
    type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    provider: '',
    

    

    

    

    
    
    
    'file_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'file_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'mime_type': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    size_bytes: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'file_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'checksum_sha256': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    uploaded_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'uploaded_by': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Company' labelFor='company'>
        <Field
            name='company'
            id='company'
            component={SelectField}
            options={initialValues.company}
            itemRef={'companies'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Stop' labelFor='stop'>
        <Field
            name='stop'
            id='stop'
            component={SelectField}
            options={initialValues.stop}
            itemRef={'load_stops'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PODType" labelFor="type">
        <Field name="type" id="type" component="select">
          
            <option value="POD_SIGNED">POD_SIGNED</option>
          
            <option value="CMR_SIGNED">CMR_SIGNED</option>
          
            <option value="DELIVERY_NOTE">DELIVERY_NOTE</option>
          
            <option value="DAMAGE_REPORT">DAMAGE_REPORT</option>
          
            <option value="OTHER">OTHER</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="StorageProvider" labelFor="provider">
        <Field name="provider" id="provider" component="select">
          
            <option value="S3">S3</option>
          
            <option value="AZURE_BLOB">AZURE_BLOB</option>
          
            <option value="GCS">GCS</option>
          
            <option value="LOCAL">LOCAL</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="FileKey"
    >
        <Field
            name="file_key"
            placeholder="FileKey"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="FileName"
    >
        <Field
            name="file_name"
            placeholder="FileName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="MIMEType"
    >
        <Field
            name="mime_type"
            placeholder="MIMEType"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Size(bytes)"
    >
        <Field
            type="number"
            name="size_bytes"
            placeholder="Size(bytes)"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="FileURL"
    >
        <Field
            name="file_url"
            placeholder="FileURL"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ChecksumSHA-256"
    >
        <Field
            name="checksum_sha256"
            placeholder="ChecksumSHA-256"
        />
    </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="UploadedBy"
    >
        <Field
            name="uploaded_by"
            placeholder="UploadedBy"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

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

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

export default EditPod_filesPage
