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/template_fields/template_fieldsSlice'
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 EditTemplate_fields = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    template: null,
    

    
    
    
    'field_key': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'label': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    value_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    is_required: false,
    

    

    

    

    

    

    
    
    

    
    default_value: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    
    sort_order: '',
    

    

    

    

    

    

    

    

    

    
    
    

    
    help_text: '',
    

    

    

    

    

    

    

    

    

    

    

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

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

  const { template_fieldsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Template' labelFor='template'>
        <Field
            name='template'
            id='template'
            component={SelectField}
            options={initialValues.template}
            itemRef={'legal_templates'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'template_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  
    <FormField
        label="FieldKey"
    >
        <Field
            name="field_key"
            placeholder="FieldKey"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="ValueType" labelFor="value_type">
        <Field name="value_type" id="value_type" component="select">
          
            <option value="text">text</option>
          
            <option value="number">number</option>
          
            <option value="date">date</option>
          
            <option value="boolean">boolean</option>
          
            <option value="party">party</option>
          
            <option value="citation">citation</option>
          
            <option value="custom">custom</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='IsRequired' labelFor='is_required'>
        <Field
            name='is_required'
            id='is_required'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  
    <FormField label="DefaultValue" hasTextareaHeight>
        <Field name="default_value" as="textarea" placeholder="DefaultValue" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  
    <FormField
        label="SortOrder"
    >
        <Field
            type="number"
            name="sort_order"
            placeholder="SortOrder"
        />
    </FormField>
  

  

  

  

  

  

  

  

    

  



  

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

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

export default EditTemplate_fields
