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/matter_parties/matter_partiesSlice'
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 EditMatter_partiesPage = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    matter: null,
    

    
    
    

    

    

    

    

    

    

    

    
    party_role: '',
    

    

    

    

    
    
    
    'display_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'organization_name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'email': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    
    'phone': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    
    address: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    
    notes: '',
    

    

    

    

    

    

    

    

    

    

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

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

  const { id } = router.query

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

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

  useEffect(() => {
      if (typeof matter_parties === 'object') {
          const newInitialVal = {...initVals};
          Object.keys(initVals).forEach(el => newInitialVal[el] = (matter_parties)[el])
          setInitialValues(newInitialVal);
      }
  }, [matter_parties])

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Matter' labelFor='matter'>
        <Field
            name='matter'
            id='matter'
            component={SelectField}
            options={initialValues.matter}
            itemRef={'matters'}
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'title'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="PartyRole" labelFor="party_role">
        <Field name="party_role" id="party_role" component="select">
          
            <option value="plaintiff">plaintiff</option>
          
            <option value="defendant">defendant</option>
          
            <option value="petitioner">petitioner</option>
          
            <option value="respondent">respondent</option>
          
            <option value="witness">witness</option>
          
            <option value="judge">judge</option>
          
            <option value="attorney">attorney</option>
          
            <option value="opposing_counsel">opposing_counsel</option>
          
            <option value="expert">expert</option>
          
            <option value="provider">provider</option>
          
            <option value="other">other</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="DisplayName"
    >
        <Field
            name="display_name"
            placeholder="DisplayName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="OrganizationName"
    >
        <Field
            name="organization_name"
            placeholder="OrganizationName"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Email"
    >
        <Field
            name="email"
            placeholder="Email"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  
    <FormField
        label="Phone"
    >
        <Field
            name="phone"
            placeholder="Phone"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  
    <FormField label="Address" hasTextareaHeight>
        <Field name="address" as="textarea" placeholder="Address" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  
    <FormField label='Notes' hasTextareaHeight>
        <Field
            name='notes'
            id='notes'
            component={RichTextField}
        ></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('/matter_parties/matter_parties-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditMatter_partiesPage
