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/approval_workflows/approval_workflowsSlice'
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 EditApproval_workflows = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    organization: null,
    

    
    
    
    'name': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    module: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    record_type: '',
    

    

    

    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

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

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

  const { approval_workflowsId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
    {hasPermission(currentUser, 'READ_ORGANIZATIONS') && 
    <FormField label='Organization' labelFor='organization'>
        <Field
            name='organization'
            id='organization'
            component={SelectField}
            options={initialValues.organization}
            itemRef={'organizations'}
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
     } 

  

  

    

  



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

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Module" labelFor="module">
        <Field name="module" id="module" component="select">
          
            <option value="budget">budget</option>
          
            <option value="procurement">procurement</option>
          
            <option value="contract">contract</option>
          
            <option value="project">project</option>
          
            <option value="grant">grant</option>
          
            <option value="finance">finance</option>
          
            <option value="vendor">vendor</option>
          
            <option value="document">document</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="RecordType" labelFor="record_type">
        <Field name="record_type" id="record_type" component="select">
          
            <option value="requisitions">requisitions</option>
          
            <option value="tenders">tenders</option>
          
            <option value="awards">awards</option>
          
            <option value="contracts">contracts</option>
          
            <option value="contract_amendments">contract_amendments</option>
          
            <option value="projects">projects</option>
          
            <option value="grants">grants</option>
          
            <option value="invoices">invoices</option>
          
            <option value="payment_requests">payment_requests</option>
          
            <option value="vendors">vendors</option>
          
            <option value="budget_reallocations">budget_reallocations</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="active">active</option>
          
            <option value="inactive">inactive</option>
          
        </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('/approval_workflows/approval_workflows-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditApproval_workflows
