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_templates/document_templatesSlice'
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 EditDocument_templates = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    document_type: null,
    

    
    
    
    'template_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'template_code': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    template_family: '',
    

    

    

    

    
    
    

    
    purpose: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    template_body: '',
    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    template_files: [],
    

    

    

    
    
    

    

    

    

    

    

    

    

    
    format: '',
    

    

    

    

    
    
    

    

    

    

    

    

    
    active: false,
    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    

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

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

  const { document_templatesId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

  return (
    <>
      <Head>
        <title>{getPageTitle('Edit document_templates')}</title>
      </Head>
      <SectionMain>
        <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={'Edit document_templates'} 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="TemplateName"
    >
        <Field
            name="template_name"
            placeholder="TemplateName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="TemplateCode"
    >
        <Field
            name="template_code"
            placeholder="TemplateCode"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="TemplateFamily" labelFor="template_family">
        <Field name="template_family" id="template_family" component="select">
          
            <option value="sop">sop</option>
          
            <option value="bmr">bmr</option>
          
            <option value="coa">coa</option>
          
            <option value="doc">doc</option>
          
            <option value="risk_file">risk_file</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  
    <FormField label="Purpose" hasTextareaHeight>
        <Field name="purpose" as="textarea" placeholder="Purpose" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='TemplateBody' hasTextareaHeight>
        <Field
            name='template_body'
            id='template_body'
            component={RichTextField}
        ></Field>
    </FormField>
  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

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



  

  

  

  

  

  

  

  
    <FormField label="Format" labelFor="format">
        <Field name="format" id="format" component="select">
          
            <option value="html">html</option>
          
            <option value="docx">docx</option>
          
            <option value="xlsx">xlsx</option>
          
            <option value="pdf">pdf</option>
          
            <option value="mixed">mixed</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  

  
    <FormField label='Active' labelFor='active'>
        <Field
            name='active'
            id='active'
            component={SwitchField}
        ></Field>
    </FormField>
  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  
      <FormField label='MappedClauses' labelFor='mapped_clauses'>
          <Field
              name='mapped_clauses'
              id='mapped_clauses'
              component={SelectFieldMany}
              options={initialValues.mapped_clauses}
              itemRef={'clauses'}
            
            
            
            
            
            
            
            
            
            
            
            
              showField={'clause_code'}
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
          ></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_templates/document_templates-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditDocument_templates
