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/departments/departmentsSlice'
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 DepartmentsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { departments } = useAppSelector((state) => state.departments)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Employees Department</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>EmployeeCode</th>
                                
                                
                                
                                <th>FirstName</th>
                                
                                
                                
                                <th>MiddleName</th>
                                
                                
                                
                                <th>LastName</th>
                                
                                
                                
                                <th>NameExtension</th>
                                
                                
                                
                                <th>DateOfBirth</th>
                                
                                
                                
                                <th>PlaceOfBirth</th>
                                
                                
                                
                                <th>Sex</th>
                                
                                
                                
                                <th>CivilStatus</th>
                                
                                
                                
                                <th>Nationality</th>
                                
                                
                                
                                <th>Religion</th>
                                
                                
                                
                                <th>BloodType</th>
                                
                                
                                
                                
                                
                                <th>HomeStreet</th>
                                
                                
                                
                                <th>HomeBarangay</th>
                                
                                
                                
                                <th>HomeCity</th>
                                
                                
                                
                                <th>HomeProvince</th>
                                
                                
                                
                                <th>HomeZipCode</th>
                                
                                
                                
                                <th>PermanentAddressSame</th>
                                
                                
                                
                                <th>PermanentStreet</th>
                                
                                
                                
                                <th>PermanentBarangay</th>
                                
                                
                                
                                <th>PermanentCity</th>
                                
                                
                                
                                <th>PermanentProvince</th>
                                
                                
                                
                                <th>PermanentZipCode</th>
                                
                                
                                
                                <th>PersonalEmail</th>
                                
                                
                                
                                <th>PersonalMobile</th>
                                
                                
                                
                                <th>EmergencyContactName</th>
                                
                                
                                
                                <th>EmergencyContactRelationship</th>
                                
                                
                                
                                <th>EmergencyContactMobile</th>
                                
                                
                                
                                <th>SSSNumber</th>
                                
                                
                                
                                <th>PhilHealthNumber</th>
                                
                                
                                
                                <th>PagIbigNumber</th>
                                
                                
                                
                                <th>TINNumber</th>
                                
                                
                                
                                <th>BankName</th>
                                
                                
                                
                                <th>BankAccountNumber</th>
                                
                                
                                
                                <th>BankAccountName</th>
                                
                                
                                
                                <th>EmploymentDate</th>
                                
                                
                                
                                <th>RegularizationDate</th>
                                
                                
                                
                                <th>EmploymentStatus</th>
                                
                                
                                
                                <th>EmploymentType</th>
                                
                                
                                
                                <th>JobLevel</th>
                                
                                
                                
                                <th>SalaryType</th>
                                
                                
                                
                                <th>BasicSalary</th>
                                
                                
                                
                                <th>TaxStatus</th>
                                
                                
                                
                                <th>WorkLocation</th>
                                
                                
                                
                                <th>SeparationDate</th>
                                
                                
                                
                                <th>SeparationType</th>
                                
                                
                                
                                <th>SeparationReason</th>
                                
                                
                                
                                <th>ClearanceStatus</th>
                                
                                
                                
                                <th>FinalPayAmount</th>
                                
                                
                                
                                <th>FinalPayReleasedDate</th>
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {departments.employees_department && Array.isArray(departments.employees_department) &&
                              departments.employees_department.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/employees/employees-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="employeeCode">
                                        { item.employeeCode }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="firstName">
                                        { item.firstName }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="middleName">
                                        { item.middleName }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="lastName">
                                        { item.lastName }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="nameExtension">
                                        { item.nameExtension }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="dateOfBirth">
                                        { dataFormatter.dateTimeFormatter(item.dateOfBirth) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="placeOfBirth">
                                        { item.placeOfBirth }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sex">
                                        { item.sex }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="civilStatus">
                                        { item.civilStatus }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="nationality">
                                        { item.nationality }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="religion">
                                        { item.religion }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="bloodType">
                                        { item.bloodType }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="homeStreet">
                                        { item.homeStreet }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="homeBarangay">
                                        { item.homeBarangay }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="homeCity">
                                        { item.homeCity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="homeProvince">
                                        { item.homeProvince }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="homeZipCode">
                                        { item.homeZipCode }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="permanentAddressSame">
                                        { dataFormatter.booleanFormatter(item.permanentAddressSame) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="permanentStreet">
                                        { item.permanentStreet }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="permanentBarangay">
                                        { item.permanentBarangay }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="permanentCity">
                                        { item.permanentCity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="permanentProvince">
                                        { item.permanentProvince }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="permanentZipCode">
                                        { item.permanentZipCode }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="personalEmail">
                                        { item.personalEmail }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="personalMobile">
                                        { item.personalMobile }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="emergencyContactName">
                                        { item.emergencyContactName }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="emergencyContactRel">
                                        { item.emergencyContactRel }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="emergencyContactMobile">
                                        { item.emergencyContactMobile }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sssNumber">
                                        { item.sssNumber }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="philhealthNumber">
                                        { item.philhealthNumber }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="pagibigNumber">
                                        { item.pagibigNumber }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tinNumber">
                                        { item.tinNumber }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="bankName">
                                        { item.bankName }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="bankAccountNumber">
                                        { item.bankAccountNumber }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="bankAccountName">
                                        { item.bankAccountName }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="employmentDate">
                                        { dataFormatter.dateTimeFormatter(item.employmentDate) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="regularizationDate">
                                        { dataFormatter.dateTimeFormatter(item.regularizationDate) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="employmentStatus">
                                        { item.employmentStatus }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="employmentType">
                                        { item.employmentType }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="jobLevel">
                                        { item.jobLevel }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="salaryType">
                                        { item.salaryType }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="basicSalary">
                                        { item.basicSalary }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="taxStatus">
                                        { item.taxStatus }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="workLocation">
                                        { item.workLocation }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="separationDate">
                                        { dataFormatter.dateTimeFormatter(item.separationDate) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="separationType">
                                        { item.separationType }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="separationReason">
                                        { item.separationReason }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="clearanceStatus">
                                        { item.clearanceStatus }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="finalPayAmount">
                                        { item.finalPayAmount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="finalPayReleasedDate">
                                        { dataFormatter.dateTimeFormatter(item.finalPayReleasedDate) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="isActive">
                                        { dataFormatter.booleanFormatter(item.isActive) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!departments?.employees_department?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default DepartmentsView;