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/document_template_variables/document_template_variablesSlice'
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 EditDocument_template_variables = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    document_type: null,
    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    data_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    is_required_minimum: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    
    is_desired: false,
    

    

    

    

    

    

    
    
    

    
    help_text: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    organizations: null,
    

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

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

  const { document_template_variablesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Documenttype' labelFor='document_type'>
        <Field
            name='document_type'
            id='document_type'
            component={SelectField}
            options={initialValues.document_type}
            itemRef={'document_types'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="Variablename"
    >
        <Field
            name="name"
            placeholder="Variablename"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Key"
    >
        <Field
            name="key"
            placeholder="Key"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Datatype" labelFor="data_type">
        <Field name="data_type" id="data_type" component="select">
          
            <option value="string">string</option>
          
            <option value="number">number</option>
          
            <option value="date">date</option>
          
            <option value="datetime">datetime</option>
          
            <option value="boolean">boolean</option>
          
            <option value="currency">currency</option>
          
            <option value="person">person</option>
          
            <option value="address">address</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Isrequired(minimum)' labelFor='is_required_minimum'>
        <Field
            name='is_required_minimum'
            id='is_required_minimum'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Isdesired' labelFor='is_desired'>
        <Field
            name='is_desired'
            id='is_desired'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

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

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

export default EditDocument_template_variables
