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_tasks/approval_tasksSlice'
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 EditApproval_tasks = () => {
  const router = useRouter()
  const dispatch = useAppDispatch()
  const initVals = {
      
    

    

    

    

    

    

    

    

    

    

    

    
    workflow: null,
    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    step: null,
    

    
    
    

    

    

    

    

    

    

    

    
    subject_type: '',
    

    

    

    

    
    
    
    'subject_reference': '',
    

    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    

    

    
    assignee: null,
    

    
    
    

    

    

    

    

    

    

    

    
    status: '',
    

    

    

    

    
    
    

    

    

    

    

    
    assigned_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    

    

    

    

    
    completed_at: new Date(),
    

    

    

    

    

    

    

    
    
    

    
    decision_comment: '',
    

    

    

    

    

    

    

    

    

    

    

    
    
    

    

    

    

    

    

    

    

    

    
    evidence_files: [],
    

    

    

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

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

  const { approval_tasksId } = router.query

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

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

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

          const newInitialVal = {...initVals};

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

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

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

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


  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Workflow' labelFor='workflow'>
        <Field
            name='workflow'
            id='workflow'
            component={SelectField}
            options={initialValues.workflow}
            itemRef={'approval_workflows'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Step' labelFor='step'>
        <Field
            name='step'
            id='step'
            component={SelectField}
            options={initialValues.step}
            itemRef={'approval_steps'}
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
            showField={'step_name'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="SubjectType" labelFor="subject_type">
        <Field name="subject_type" id="subject_type" component="select">
          
            <option value="document">document</option>
          
            <option value="document_version">document_version</option>
          
            <option value="change_request">change_request</option>
          
            <option value="training_assignment">training_assignment</option>
          
            <option value="record_batch">record_batch</option>
          
            <option value="risk_assessment">risk_assessment</option>
          
            <option value="capa">capa</option>
          
            <option value="nonconformance">nonconformance</option>
          
            <option value="audit">audit</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  
    <FormField
        label="SubjectReference"
    >
        <Field
            name="subject_reference"
            placeholder="SubjectReference"
        />
    </FormField>
  

  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  
  
    <FormField label='Assignee' labelFor='assignee'>
        <Field
            name='assignee'
            id='assignee'
            component={SelectField}
            options={initialValues.assignee}
            itemRef={'users'}
      
        
            showField={'firstName'}
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        
      
        ></Field>
    </FormField>
    

  

  

    

  



  

  

  

  

  

  

  

  
    <FormField label="Status" labelFor="status">
        <Field name="status" id="status" component="select">
          
            <option value="pending">pending</option>
          
            <option value="in_progress">in_progress</option>
          
            <option value="approved">approved</option>
          
            <option value="rejected">rejected</option>
          
            <option value="cancelled">cancelled</option>
          
        </Field>
    </FormField>
  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="AssignedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.assigned_at ?
                  new Date(
                      dayjs(initialValues.assigned_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'assigned_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  

  

  

  

  
      <FormField
          label="CompletedAt"
      >
          <DatePicker
              dateFormat="yyyy-MM-dd hh:mm"
              showTimeSelect
              selected={initialValues.completed_at ?
                  new Date(
                      dayjs(initialValues.completed_at).format('YYYY-MM-DD hh:mm'),
                  ) : null
              }
              onChange={(date) => setInitialValues({...initialValues, 'completed_at': date})}
          />
      </FormField>
  

  

  

  

  

  

    

  



  

  
    <FormField label="DecisionComment" hasTextareaHeight>
        <Field name="decision_comment" as="textarea" placeholder="DecisionComment" />
    </FormField>
  

  

  

  

  

  

  

  

  

  

    

  



  

  

  

  

  

  

  

  

  

  

  

    

  
        <FormField>
          <Field
            label='EvidenceFiles'
            color='info'
            icon={mdiUpload}
            path={'approval_tasks/evidence_files'}
            name='evidence_files'
            id='evidence_files'
            schema={{
                size: undefined,
                formats: undefined,
            }}
            component={FormFilePicker}
          ></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_tasks/approval_tasks-list')}/>
              </BaseButtons>
            </Form>
          </Formik>
        </CardBox>
      </SectionMain>
    </>
  )
}

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

export default EditApproval_tasks
