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/leave_types/leave_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";


const Leave_typesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { leave_types } = useAppSelector((state) => state.leave_types)
    

    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 leave_types')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View leave_types')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/leave_types/leave_types-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

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

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>DaysPerYear</p>
                  <p>{leave_types?.daysPerYear || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsConvertible'>
                    <SwitchField
                      field={{name: 'isConvertible', value: leave_types?.isConvertible}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='RequiresMedicalCertificate'>
                    <SwitchField
                      field={{name: 'requiresMedCert', value: leave_types?.requiresMedCert}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>MinimumServiceMonths</p>
                  <p>{leave_types?.minServiceMonths || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsActive'>
                    <SwitchField
                      field={{name: 'isActive', value: leave_types?.isActive}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Leave_balances LeaveType</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Year</th>
                                
                                
                                
                                <th>OpeningBalance</th>
                                
                                
                                
                                <th>EarnedThisYear</th>
                                
                                
                                
                                <th>UsedYTD</th>
                                
                                
                                
                                <th>Forfeited</th>
                                
                                
                                
                                <th>ConvertedToCash</th>
                                
                                
                                
                                <th>CurrentBalance</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {leave_types.leave_balances_leaveType && Array.isArray(leave_types.leave_balances_leaveType) &&
                              leave_types.leave_balances_leaveType.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/leave_balances/leave_balances-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="year">
                                        { item.year }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="openingBalance">
                                        { item.openingBalance }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="earnedThisYear">
                                        { item.earnedThisYear }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="usedYtd">
                                        { item.usedYtd }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="forfeited">
                                        { item.forfeited }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="convertedToCash">
                                        { item.convertedToCash }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="currentBalance">
                                        { item.currentBalance }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!leave_types?.leave_balances_leaveType?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Leave_applications LeaveType</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>DateFrom</th>
                                
                                
                                
                                <th>DateTo</th>
                                
                                
                                
                                <th>TotalDays</th>
                                
                                
                                
                                <th>Reason</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>ManagerNote</th>
                                
                                
                                
                                <th>HRNote</th>
                                
                                
                                
                                
                                
                                <th>ApprovedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {leave_types.leave_applications_leaveType && Array.isArray(leave_types.leave_applications_leaveType) &&
                              leave_types.leave_applications_leaveType.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/leave_applications/leave_applications-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="dateFrom">
                                        { dataFormatter.dateTimeFormatter(item.dateFrom) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="dateTo">
                                        { dataFormatter.dateTimeFormatter(item.dateTo) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="totalDays">
                                        { item.totalDays }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reason">
                                        { item.reason }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="managerNote">
                                        { item.managerNote }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="hrNote">
                                        { item.hrNote }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="approvedAt">
                                        { dataFormatter.dateTimeFormatter(item.approvedAt) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!leave_types?.leave_applications_leaveType?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Leave_typesView;