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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='PeriodStart'>
                    {payroll_periods.period_start ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={payroll_periods.period_start ?
                        new Date(
                          dayjs(payroll_periods.period_start).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No PeriodStart</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='PeriodEnd'>
                    {payroll_periods.period_end ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={payroll_periods.period_end ?
                        new Date(
                          dayjs(payroll_periods.period_end).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No PeriodEnd</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>StandardWorkdays</p>
                  <p>{payroll_periods?.standard_workdays || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='GeneratedAt'>
                    {payroll_periods.generated_at ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={payroll_periods.generated_at ?
                        new Date(
                          dayjs(payroll_periods.generated_at).format('YYYY-MM-DD hh:mm'),
                        ) : null
                      }
                      disabled
                    /> : <p>No GeneratedAt</p>}
                </FormField>
              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>GeneratedByUser</p>
                    
                    
                        <p>{payroll_periods?.generated_by_user?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Payroll_items PayrollPeriod</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Workdays</th>
                                
                                
                                
                                <th>SickDays</th>
                                
                                
                                
                                <th>LeaveDays</th>
                                
                                
                                
                                <th>AbsentDays</th>
                                
                                
                                
                                <th>LateCount</th>
                                
                                
                                
                                <th>LateMinutesTotal</th>
                                
                                
                                
                                <th>BaseSalary(Monthly)</th>
                                
                                
                                
                                <th>DailyRate</th>
                                
                                
                                
                                <th>BaseSalaryTotal</th>
                                
                                
                                
                                <th>Meal+TransportTotal</th>
                                
                                
                                
                                <th>PositionAllowanceTotal</th>
                                
                                
                                
                                <th>KPIScore</th>
                                
                                
                                
                                <th>KPIAllowanceTotal</th>
                                
                                
                                
                                <th>OvertimeHoursTotal</th>
                                
                                
                                
                                <th>OvertimeRate</th>
                                
                                
                                
                                <th>OvertimeTotal</th>
                                
                                
                                
                                <th>RevenueBonusTotal</th>
                                
                                
                                
                                <th>ServiceChargeTotal</th>
                                
                                
                                
                                <th>DeductionTotal</th>
                                
                                
                                
                                <th>TakeHomePay</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>AdjustmentNotes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {payroll_periods.payroll_items_payroll_period && Array.isArray(payroll_periods.payroll_items_payroll_period) &&
                              payroll_periods.payroll_items_payroll_period.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/payroll_items/payroll_items-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="workdays">
                                        { item.workdays }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sick_days">
                                        { item.sick_days }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="leave_days">
                                        { item.leave_days }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="absent_days">
                                        { item.absent_days }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="late_count">
                                        { item.late_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="late_minutes_total">
                                        { item.late_minutes_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="base_salary_monthly">
                                        { item.base_salary_monthly }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="daily_rate">
                                        { item.daily_rate }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="base_salary_total">
                                        { item.base_salary_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="meal_transport_total">
                                        { item.meal_transport_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="position_allowance_total">
                                        { item.position_allowance_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="kpi_score">
                                        { item.kpi_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="kpi_allowance_total">
                                        { item.kpi_allowance_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="overtime_hours_total">
                                        { item.overtime_hours_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="overtime_rate">
                                        { item.overtime_rate }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="overtime_total">
                                        { item.overtime_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="revenue_bonus_total">
                                        { item.revenue_bonus_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="service_charge_total">
                                        { item.service_charge_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="deduction_total">
                                        { item.deduction_total }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="take_home_pay">
                                        { item.take_home_pay }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="adjustment_notes">
                                        { item.adjustment_notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!payroll_periods?.payroll_items_payroll_period?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Payroll_periodsView;