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/storage_objects/storage_objectsSlice'
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";

import {hasPermission} from "../../helpers/userPermissions";



const EditStorage_objects = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    bucket: null,
    

    
    
    
    'object_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'content_type': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    size_bytes: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'etag': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'sha256': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    
    thumbnail_image: [],
    

    

    
    
    

    

    

    

    

    

    

    

    

    
    preview_file: [],
    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    original_file: [],
    

    

    

    
    
    
    'compression_ratio': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    trashed_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    purge_after: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

  const { storage_objects } = useAppSelector((state) => state.storage_objects)
  
  const { currentUser } = useAppSelector((state) => state.auth);
  

  const { storage_objectsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Bucket' labelFor='bucket'>
        <Field
            name='bucket'
            id='bucket'
            component={SelectField}
            options={initialValues.bucket}
            itemRef={'storage_buckets'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'bucket_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="ObjectKey"
    >
        <Field
            name="object_key"
            placeholder="ObjectKey"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

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

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="ETag"
    >
        <Field
            name="etag"
            placeholder="ETag"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="SHA256"
    >
        <Field
            name="sha256"
            placeholder="SHA256"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    
        <FormField>
            <Field
                label='ThumbnailImage'
                color='info'
                icon={mdiUpload}
                path={'storage_objects/thumbnail_image'}
                name='thumbnail_image'
                id='thumbnail_image'
                schema={{
                    size: undefined,
                    formats: undefined,
                }}
                component={FormImagePicker}
            ></Field>
        </FormField>
    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='PreviewFile'
            color='info'
            icon={mdiUpload}
            path={'storage_objects/preview_file'}
            name='preview_file'
            id='preview_file'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='OriginalFile'
            color='info'
            icon={mdiUpload}
            path={'storage_objects/original_file'}
            name='original_file'
            id='original_file'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></Field>
        </FormField>
  



  

  

  

  
    <FormField
        label="CompressionRatio"
    >
        <Field
            type="number"
            name="compression_ratio"
            placeholder="CompressionRatio"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="uploading">uploading</option>
          
            <option value="ready">ready</option>
          
            <option value="processing">processing</option>
          
            <option value="failed">failed</option>
          
            <option value="deleted">deleted</option>
          
            <option value="trashed">trashed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

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

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

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

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

export default EditStorage_objects
