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/cost_files/cost_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 EditCost_files = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    cost_item: null,
    

    
    
    

    

    

    

    

    

    

    

    
    provider: '',
    

    

    

    

    
    
    
    'file_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'file_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'mime_type': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    size_bytes: '',
    

    

    

    

    

    

    

    

    

    
    
    
    'file_url': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'checksum_sha256': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    uploaded_at: new Date(),
    

    

    

    

    

    

    

    
    
    
    'uploaded_by': '',
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { cost_filesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='CostItem' labelFor='cost_item'>
        <Field
            name='cost_item'
            id='cost_item'
            component={SelectField}
            options={initialValues.cost_item}
            itemRef={'cost_items'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'reference'}
        
      
        
      
        
      
        
      
        ></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>
  

  

  

  

  

  

  

  

  

  

  

    

  


  

              <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('/cost_files/cost_files-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditCost_files
