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/ai_models/ai_modelsSlice'
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 Ai_modelsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { ai_models } = useAppSelector((state) => state.ai_models)
    

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

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Provider</p>
                    <p>{ai_models?.provider ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ModelVersion</p>
                    <p>{ai_models?.model_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>APIBaseURL</p>
                    <p>{ai_models?.base_url}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              
                <FormField label='Multi Text' hasTextareaHeight>
                  <textarea className={'w-full'} disabled value={ai_models?.default_config} />
                </FormField>
              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>TypicalLatency(ms)</p>
                  <p>{ai_models?.typical_latency_ms || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Enabled'>
                    <SwitchField
                      field={{name: 'is_enabled', value: ai_models?.is_enabled}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Api_keys Model</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>KeyName</th>
                                
                                
                                
                                <th>EncryptedKey</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>ExpiresAt</th>
                                
                                
                                
                                <th>Active</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {ai_models.api_keys_model && Array.isArray(ai_models.api_keys_model) &&
                              ai_models.api_keys_model.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/api_keys/api_keys-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="encrypted_key">
                                        { item.encrypted_key }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="expires_at">
                                        { dataFormatter.dateTimeFormatter(item.expires_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_active">
                                        { dataFormatter.booleanFormatter(item.is_active) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!ai_models?.api_keys_model?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Fill_sessions AIModel</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>
                            {ai_models.fill_sessions_ai_model && Array.isArray(ai_models.fill_sessions_ai_model) &&
                              ai_models.fill_sessions_ai_model.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>
                        {!ai_models?.fill_sessions_ai_model?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Ai_modelsView;