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_chat_sessions/ai_chat_sessionsSlice'
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_chat_sessionsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { ai_chat_sessions } = useAppSelector((state) => state.ai_chat_sessions)
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Customer</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{ai_chat_sessions?.customer?.customer_code ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Channel</p>
                    <p>{ai_chat_sessions?.channel ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Summary</p>
                  {ai_chat_sessions.summary
                    ? <p dangerouslySetInnerHTML={{__html: ai_chat_sessions.summary}}/>
                    : <p>No data</p>
                  }
                </div>
              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Ai_chat_messages ChatSession</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>Sender</th>
                                
                                
                                
                                <th>MessageText</th>
                                
                                
                                
                                <th>SentAt</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {ai_chat_sessions.ai_chat_messages_ai_chat_session && Array.isArray(ai_chat_sessions.ai_chat_messages_ai_chat_session) &&
                              ai_chat_sessions.ai_chat_messages_ai_chat_session.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/ai_chat_messages/ai_chat_messages-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="sender">
                                        { item.sender }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="message_text">
                                        { item.message_text }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sent_at">
                                        { dataFormatter.dateTimeFormatter(item.sent_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!ai_chat_sessions?.ai_chat_messages_ai_chat_session?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                

                <BaseDivider />

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

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

export default Ai_chat_sessionsView;