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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              
                <FormField label='PeriodEnd'>
                    {kpi_periods.period_end ? <DatePicker
                      dateFormat="yyyy-MM-dd hh:mm"
                      showTimeSelect
                      selected={kpi_periods.period_end ?
                        new Date(
                          dayjs(kpi_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>{kpi_periods?.outlet?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Kpi_scores KPIPeriod</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>AbsensiScore</th>
                                
                                
                                
                                <th>DisiplinScore</th>
                                
                                
                                
                                <th>ProduktivitasScore</th>
                                
                                
                                
                                <th>AttitudeScore</th>
                                
                                
                                
                                <th>SOPScore</th>
                                
                                
                                
                                <th>FinalScore</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                
                                
                                <th>ScoredAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {kpi_periods.kpi_scores_kpi_period && Array.isArray(kpi_periods.kpi_scores_kpi_period) &&
                              kpi_periods.kpi_scores_kpi_period.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/kpi_scores/kpi_scores-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="absensi_score">
                                        { item.absensi_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="disiplin_score">
                                        { item.disiplin_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="produktif_score">
                                        { item.produktif_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="attitude_score">
                                        { item.attitude_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sop_score">
                                        { item.sop_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="final_score">
                                        { item.final_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="scored_at">
                                        { dataFormatter.dateTimeFormatter(item.scored_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!kpi_periods?.kpi_scores_kpi_period?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Kpi_periodsView;