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/service_types/service_typesSlice'
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 Service_typesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { service_types } = useAppSelector((state) => state.service_types)
    
    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 service_types')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View service_types')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/service_types/service_types-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Code</p>
                    <p>{service_types?.code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Signaturelevel</p>
                    <p>{service_types?.signature_level ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Isactive'>
                    <SwitchField
                      field={{name: 'is_active', value: service_types?.is_active}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>organizations</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{service_types?.organizations?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Service_auth_policies Servicetype</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Policyname</th>
                                
                                
                                
                                <th>Enforcement</th>
                                
                                
                                
                                <th>Minmethodsrequired</th>
                                
                                
                                
                                <th>Allowprecheck</th>
                                
                                
                                
                                <th>Isactive</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {service_types.service_auth_policies_service_type && Array.isArray(service_types.service_auth_policies_service_type) &&
                              service_types.service_auth_policies_service_type.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/service_auth_policies/service_auth_policies-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="enforcement">
                                        { item.enforcement }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="min_methods_required">
                                        { item.min_methods_required }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="allow_precheck">
                                        { dataFormatter.booleanFormatter(item.allow_precheck) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!service_types?.service_auth_policies_service_type?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Workflow_definitions Servicetype</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Name</th>
                                
                                
                                
                                <th>Code</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Defaultdeadline(hours)</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {service_types.workflow_definitions_service_type && Array.isArray(service_types.workflow_definitions_service_type) &&
                              service_types.workflow_definitions_service_type.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/workflow_definitions/workflow_definitions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="code">
                                        { item.code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="default_deadline_hours">
                                        { item.default_deadline_hours }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!service_types?.workflow_definitions_service_type?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Price_rules Servicetype</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Minparticipants</th>
                                
                                
                                
                                <th>Maxparticipants</th>
                                
                                
                                
                                <th>Priceamount</th>
                                
                                
                                
                                <th>Splitpaymentallowed</th>
                                
                                
                                
                                <th>Isactive</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {service_types.price_rules_service_type && Array.isArray(service_types.price_rules_service_type) &&
                              service_types.price_rules_service_type.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/price_rules/price_rules-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="min_participants">
                                        { item.min_participants }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="max_participants">
                                        { item.max_participants }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="price_amount">
                                        { item.price_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_split_payment_allowed">
                                        { dataFormatter.booleanFormatter(item.is_split_payment_allowed) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!service_types?.price_rules_service_type?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Contract_folders Servicetype</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Foldertitle</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Startedat</th>
                                
                                
                                
                                <th>Deadlineat</th>
                                
                                
                                
                                <th>Completedat</th>
                                
                                
                                
                                <th>Requiresnotary</th>
                                
                                
                                
                                <th>Servicewarning</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {service_types.contract_folders_service_type && Array.isArray(service_types.contract_folders_service_type) &&
                              service_types.contract_folders_service_type.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/contract_folders/contract_folders-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="started_at">
                                        { dataFormatter.dateTimeFormatter(item.started_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="deadline_at">
                                        { dataFormatter.dateTimeFormatter(item.deadline_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="completed_at">
                                        { dataFormatter.dateTimeFormatter(item.completed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="requires_notary">
                                        { dataFormatter.booleanFormatter(item.requires_notary) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="service_warning">
                                        { item.service_warning }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!service_types?.contract_folders_service_type?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Service_typesView;