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";

import {hasPermission} from "../../helpers/userPermissions";


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

    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>
            

              

              

              

              

              

              

              

              

              

              
                {hasPermission(currentUser, 'READ_ORGANIZATIONS') && 
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Organization</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{approval_workflows?.organization?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                  } 
              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                <>
                    <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>MinimumAmount</th>
                                
                                
                                
                                <th>MaximumAmount</th>
                                
                                
                                
                                <th>RequiresComment</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="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="min_amount">
                                        { item.min_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="max_amount">
                                        { item.max_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="requires_comment">
                                        { dataFormatter.booleanFormatter(item.requires_comment) }
                                    </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'}>Approvals Workflow</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>RecordType</th>
                                
                                
                                
                                <th>RecordKey</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>RequestedAt</th>
                                
                                
                                
                                <th>DecidedAt</th>
                                
                                
                                
                                <th>DecisionComment</th>
                                
                                
                                
                                <th>RejectionReason</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {approval_workflows.approvals_workflow && Array.isArray(approval_workflows.approvals_workflow) &&
                              approval_workflows.approvals_workflow.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/approvals/approvals-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="record_type">
                                        { item.record_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="record_key">
                                        { item.record_key }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="requested_at">
                                        { dataFormatter.dateTimeFormatter(item.requested_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="decided_at">
                                        { dataFormatter.dateTimeFormatter(item.decided_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="decision_comment">
                                        { item.decision_comment }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rejection_reason">
                                        { item.rejection_reason }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!approval_workflows?.approvals_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;