import React, { ReactElement, useEffect } from 'react';
import Head from 'next/head'
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import dayjs from "dayjs";
import {useAppDispatch, useAppSelector} from "../../stores/hooks";
import {useRouter} from "next/router";
import { fetch } from '../../stores/approval_workflows/approval_workflowsSlice'
import {saveFile} from "../../helpers/fileSaver";
import dataFormatter from '../../helpers/dataFormatter';
import ImageField from "../../components/ImageField";
import LayoutAuthenticated from "../../layouts/Authenticated";
import {getPageTitle} from "../../config";
import SectionTitleLineWithButton from "../../components/SectionTitleLineWithButton";
import SectionMain from "../../components/SectionMain";
import CardBox from "../../components/CardBox";
import BaseButton from "../../components/BaseButton";
import BaseDivider from "../../components/BaseDivider";
import {mdiChartTimelineVariant} from "@mdi/js";
import {SwitchField} from "../../components/SwitchField";
import FormField from "../../components/FormField";


const Approval_workflowsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { approval_workflows } = useAppSelector((state) => state.approval_workflows)
    

    const { id } = router.query;
    
    function removeLastCharacter(str) {
      console.log(str,`str`)
      return str.slice(0, -1);
    }

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


    return (
      <>
          <Head>
              <title>{getPageTitle('View approval_workflows')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View approval_workflows')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/approval_workflows/approval_workflows-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Name</p>
                    <p>{approval_workflows?.name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AppliesTo</p>
                    <p>{approval_workflows?.applies_to ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='RequiresE-Signature'>
                    <SwitchField
                      field={{name: 'requires_esignature', value: approval_workflows?.requires_esignature}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='RequiresTwoFactor'>
                    <SwitchField
                      field={{name: 'requires_two_factor', value: approval_workflows?.requires_two_factor}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>MinimumApprovals</p>
                  <p>{approval_workflows?.min_approvals || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              
                <FormField label='Multi Text' hasTextareaHeight>
                  <textarea className={'w-full'} disabled value={approval_workflows?.instructions} />
                </FormField>
              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Active'>
                    <SwitchField
                      field={{name: 'active', value: approval_workflows?.active}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Approval_steps Workflow</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>StepOrder</th>
                                
                                
                                
                                <th>StepName</th>
                                
                                
                                
                                <th>StepType</th>
                                
                                
                                
                                
                                
                                <th>AssignmentRule</th>
                                
                                
                                
                                <th>SLADays</th>
                                
                                
                                
                                <th>Required</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {approval_workflows.approval_steps_workflow && Array.isArray(approval_workflows.approval_steps_workflow) &&
                              approval_workflows.approval_steps_workflow.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/approval_steps/approval_steps-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="step_order">
                                        { item.step_order }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="step_name">
                                        { item.step_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="step_type">
                                        { item.step_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="assignment_rule">
                                        { item.assignment_rule }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sla_days">
                                        { item.sla_days }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="required">
                                        { dataFormatter.booleanFormatter(item.required) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!approval_workflows?.approval_steps_workflow?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Approval_tasks Workflow</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>SubjectType</th>
                                
                                
                                
                                <th>SubjectReference</th>
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>AssignedAt</th>
                                
                                
                                
                                <th>CompletedAt</th>
                                
                                
                                
                                <th>DecisionComment</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {approval_workflows.approval_tasks_workflow && Array.isArray(approval_workflows.approval_tasks_workflow) &&
                              approval_workflows.approval_tasks_workflow.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/approval_tasks/approval_tasks-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="subject_type">
                                        { item.subject_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="subject_reference">
                                        { item.subject_reference }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="assigned_at">
                                        { dataFormatter.dateTimeFormatter(item.assigned_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="completed_at">
                                        { dataFormatter.dateTimeFormatter(item.completed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="decision_comment">
                                        { item.decision_comment }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!approval_workflows?.approval_tasks_workflow?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

                <BaseButton
                    color='info'
                    label='Back'
                    onClick={() => router.push('/approval_workflows/approval_workflows-list')}
                />
              </CardBox>
          </SectionMain>
      </>
    );
};

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

export default Approval_workflowsView;