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/api_keys/api_keysSlice'
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 Api_keysView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { api_keys } = useAppSelector((state) => state.api_keys)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>EncryptedKey</p>
                    <p>{api_keys?.encrypted_key}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Owner</p>
                    
                    
                        <p>{api_keys?.owner?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Fill_sessions APIKey</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>SessionName</th>
                                
                                
                                
                                
                                
                                <th>Started</th>
                                
                                
                                
                                <th>Ended</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Progress(%)</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {api_keys.fill_sessions_api_key && Array.isArray(api_keys.fill_sessions_api_key) &&
                              api_keys.fill_sessions_api_key.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/fill_sessions/fill_sessions-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="session_name">
                                        { item.session_name }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="started">
                                        { dataFormatter.dateTimeFormatter(item.started) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ended">
                                        { dataFormatter.dateTimeFormatter(item.ended) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="progress_percent">
                                        { item.progress_percent }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!api_keys?.fill_sessions_api_key?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Api_keysView;