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_tasks/ai_tasksSlice'
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_tasksView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { ai_tasks } = useAppSelector((state) => state.ai_tasks)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TaskName</p>
                    <p>{ai_tasks?.task_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>TaskType</p>
                    <p>{ai_tasks?.task_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='StoresPrivateOutput'>
                    <SwitchField
                      field={{name: 'stores_private_output', value: ai_tasks?.stores_private_output}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='AllowedForFreeUsers'>
                    <SwitchField
                      field={{name: 'allowed_for_free_users', value: ai_tasks?.allowed_for_free_users}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>PerUserDailyLimit</p>
                  <p>{ai_tasks?.per_user_daily_limit || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Ai_requests AITask</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>RequestedAt</th>
                                
                                
                                
                                <th>CompletedAt</th>
                                
                                
                                
                                <th>PromptTokens</th>
                                
                                
                                
                                <th>CompletionTokens</th>
                                
                                
                                
                                <th>EstimatedCost</th>
                                
                                
                                
                                <th>ModelUsed</th>
                                
                                
                                
                                <th>ErrorMessage</th>
                                
                                
                                
                                <th>ContainsPrivateData</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {ai_tasks.ai_requests_ai_task && Array.isArray(ai_tasks.ai_requests_ai_task) &&
                              ai_tasks.ai_requests_ai_task.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/ai_requests/ai_requests-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="requested_at">
                                        { dataFormatter.dateTimeFormatter(item.requested_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="completed_at">
                                        { dataFormatter.dateTimeFormatter(item.completed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="prompt_tokens">
                                        { item.prompt_tokens }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="completion_tokens">
                                        { item.completion_tokens }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="estimated_cost">
                                        { item.estimated_cost }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="model_used">
                                        { item.model_used }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="error_message">
                                        { item.error_message }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="contains_private_data">
                                        { dataFormatter.booleanFormatter(item.contains_private_data) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!ai_tasks?.ai_requests_ai_task?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Ai_tasksView;