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/contract_documents/contract_documentsSlice'
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 EditContract_documents = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    contract_folder: null,
    

    
    
    
    'title': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    document_source: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    original_file: [],
    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    working_file: [],
    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    final_signed_file: [],
    

    

    

    
    
    
    'content_hash': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    current_revision_number: '',
    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_locked: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { contract_documentsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Contractfolder' labelFor='contract_folder'>
        <Field
            name='contract_folder'
            id='contract_folder'
            component={SelectField}
            options={initialValues.contract_folder}
            itemRef={'contract_folders'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="Documenttitle"
    >
        <Field
            name="title"
            placeholder="Documenttitle"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Documentsource" labelFor="document_source">
        <Field name="document_source" id="document_source" component="select">
          
            <option value="from_template">from_template</option>
          
            <option value="uploaded">uploaded</option>
          
            <option value="generated">generated</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

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



  

  

  

  

  

  

  

  

  

  

  

    

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



  

  

  

  

  

  

  

  

  

  

  

    

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



  
    <FormField
        label="Contenthash"
    >
        <Field
            name="content_hash"
            placeholder="Contenthash"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="Currentrevisionnumber"
    >
        <Field
            type="number"
            name="current_revision_number"
            placeholder="Currentrevisionnumber"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Islocked' labelFor='is_locked'>
        <Field
            name='is_locked'
            id='is_locked'
            component={SwitchField}
        ></Field>
    </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('/contract_documents/contract_documents-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditContract_documents
