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_runs/payroll_runsSlice'
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 Payroll_runsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { payroll_runs } = useAppSelector((state) => state.payroll_runs)
    

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

    function formatEmployeeName(employee) {
      if (!employee) return 'No data';

      const fullName = [employee.firstName, employee.lastName]
        .filter(Boolean)
        .join(' ')
        .trim();

      return fullName || employee.email || 'No data';
    }

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


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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Payroll_line_items PayrollRun</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                <th>Employee</th>
                                <th>TotalHours</th>
                                <th>GrossPay</th>
                                <th>TotalCommissionBase</th>
                            </tr>
                            </thead>
                            <tbody>
                            {payroll_runs.payroll_line_items_payroll_run && Array.isArray(payroll_runs.payroll_line_items_payroll_run) &&
                              payroll_runs.payroll_line_items_payroll_run.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/payroll_line_items/payroll_line_items-view/?id=${item.id}`)}>
                                    <td data-label="employee">
                                        {formatEmployeeName(item.employee)}
                                    </td>
                                    <td data-label="total_hours">
                                        { item.total_hours }
                                    </td>
                                    <td data-label="gross_pay">
                                        { item.gross_pay }
                                    </td>
                                    <td data-label="total_commission_base">
                                        { item.total_commission_base }
                                    </td>
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!payroll_runs?.payroll_line_items_payroll_run?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                

                <BaseDivider />

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

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

export default Payroll_runsView;