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/employee_loans/employee_loansSlice'
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 Employee_loansView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { employee_loans } = useAppSelector((state) => state.employee_loans)
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Employee</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{employee_loans?.employee?.employeeCode ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>LoanType</p>
                    <p>{employee_loans?.loanType ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>LoanSource</p>
                    <p>{employee_loans?.loanSource}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>LoanAmount</p>
                  <p>{employee_loans?.loanAmount || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>MonthlyAmortization</p>
                  <p>{employee_loans?.monthlyAmortization || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>RemainingBalance</p>
                  <p>{employee_loans?.remainingBalance || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>TermsMonths</p>
                  <p>{employee_loans?.termsMonths || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>LoanAccountNumber</p>
                    <p>{employee_loans?.loanAccountNumber}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Loan_deductions Loan</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>AmountDeducted</th>
                                
                                
                                
                                <th>DeductionDate</th>
                                
                                
                                
                                <th>BalanceBefore</th>
                                
                                
                                
                                <th>BalanceAfter</th>
                                
                                
                                
                                <th>IsDeferred</th>
                                
                                
                                
                                <th>DeferredReason</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {employee_loans.loan_deductions_loan && Array.isArray(employee_loans.loan_deductions_loan) &&
                              employee_loans.loan_deductions_loan.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/loan_deductions/loan_deductions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="amountDeducted">
                                        { item.amountDeducted }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="deductionDate">
                                        { dataFormatter.dateTimeFormatter(item.deductionDate) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="balanceBefore">
                                        { item.balanceBefore }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="balanceAfter">
                                        { item.balanceAfter }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="isDeferred">
                                        { dataFormatter.booleanFormatter(item.isDeferred) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="deferredReason">
                                        { item.deferredReason }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!employee_loans?.loan_deductions_loan?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Employee_loansView;