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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>PayrollWeight</p>
                  <p>{job_positions?.payroll_weight || 'No data'}</p>
                </div>

                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>ShiftSchedule</p>
                  <p>{job_positions?.shift_schedule || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Employees JobPosition</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>FullName</th>
                                
                                
                                
                                <th>Phone</th>
                                
                                
                                
                                <th>NIK</th>
                                
                                
                                
                                <th>NPWP</th>
                                
                                
                                
                                <th>BankName</th>
                                
                                
                                
                                <th>BankAccountNumber</th>
                                
                                
                                
                                <th>BankAccountName</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>EmploymentStatus</th>
                                
                                
                                
                                <th>JoinDate</th>
                                
                                
                                
                                <th>ResignDate</th>
                                
                                
                                
                                <th>BaseSalary(Monthly)</th>
                                
                                
                                
                                <th>PositionAllowance(Fixed)</th>
                                
                                
                                
                                <th>MealAllowance(Daily)</th>
                                
                                
                                
                                <th>TransportAllowance(Daily)</th>
                                
                                
                                
                                <th>ProratePositionAllowanceUnder20Days</th>
                                
                                
                                
                                <th>IsActive</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {job_positions.employees_job_position && Array.isArray(job_positions.employees_job_position) &&
                              job_positions.employees_job_position.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/employees/employees-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="employee_code">
                                        { item.employee_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="full_name">
                                        { item.full_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="phone">
                                        { item.phone }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="nik">
                                        { item.nik }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="npwp">
                                        { item.npwp }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="bank_name">
                                        { item.bank_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="bank_account_number">
                                        { item.bank_account_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="bank_account_name">
                                        { item.bank_account_name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="employment_status">
                                        { item.employment_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="join_date">
                                        { dataFormatter.dateTimeFormatter(item.join_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="resign_date">
                                        { dataFormatter.dateTimeFormatter(item.resign_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="base_salary_monthly">
                                        { item.base_salary_monthly }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="position_allowance_fixed">
                                        { item.position_allowance_fixed }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="meal_allowance_daily">
                                        { item.meal_allowance_daily }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="transport_allowance_daily">
                                        { item.transport_allowance_daily }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="prorate_position_allowance_under_20_days">
                                        { dataFormatter.booleanFormatter(item.prorate_position_allowance_under_20_days) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!job_positions?.employees_job_position?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Job_positionsView;