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_insight_items/ai_insight_itemsSlice'
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_insight_itemsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { ai_insight_items } = useAppSelector((state) => state.ai_insight_items)
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>AIInsightRun</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{ai_insight_items?.ai_insight_run?.output_summary ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Category</p>
                    <p>{ai_insight_items?.category ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Severity</p>
                    <p>{ai_insight_items?.severity ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Title</p>
                    <p>{ai_insight_items?.title}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              
                <>
                    <p className={'block font-bold mb-2'}>RelatedTrades</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                    <th>Direction</th>
                                
                                
                                
                                    <th>Status</th>
                                
                                
                                
                                    <th>Timeframe</th>
                                
                                
                                
                                    <th>EntryAt</th>
                                
                                
                                
                                    <th>ExitAt</th>
                                
                                
                                
                                    <th>EntryPrice</th>
                                
                                
                                
                                    <th>ExitPrice</th>
                                
                                
                                
                                    <th>StopLossPrice</th>
                                
                                
                                
                                    <th>TakeProfitPrice</th>
                                
                                
                                
                                    <th>PositionSize</th>
                                
                                
                                
                                    <th>PositionSizeUnit</th>
                                
                                
                                
                                    <th>Fees</th>
                                
                                
                                
                                    <th>Slippage</th>
                                
                                
                                
                                    <th>PnLAmount</th>
                                
                                
                                
                                    <th>PnLPercent</th>
                                
                                
                                
                                    <th>RiskAmount</th>
                                
                                
                                
                                    <th>RiskRewardRatio</th>
                                
                                
                                
                                    <th>MaxAdverseExcursion</th>
                                
                                
                                
                                    <th>MaxFavorableExcursion</th>
                                
                                
                                
                                    <th>DurationMinutes</th>
                                
                                
                                
                                    <th>IsWin</th>
                                
                                
                                
                                    <th>Notes</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {ai_insight_items.related_trades && Array.isArray(ai_insight_items.related_trades) &&
                              ai_insight_items.related_trades.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/trades/trades-view/?id=${item.id}`)}>
                                  
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                  
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="direction">
                                        { item.direction }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="timeframe">
                                        { item.timeframe }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="entry_at">
                                        { dataFormatter.dateTimeFormatter(item.entry_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="exit_at">
                                        { dataFormatter.dateTimeFormatter(item.exit_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="entry_price">
                                        { item.entry_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="exit_price">
                                        { item.exit_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="stop_loss_price">
                                        { item.stop_loss_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="take_profit_price">
                                        { item.take_profit_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="position_size">
                                        { item.position_size }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="position_size_unit">
                                        { item.position_size_unit }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="fees">
                                        { item.fees }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="slippage">
                                        { item.slippage }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="pnl_amount">
                                        { item.pnl_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="pnl_percent">
                                        { item.pnl_percent }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="risk_amount">
                                        { item.risk_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rr_ratio">
                                        { item.rr_ratio }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="mae_amount">
                                        { item.mae_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="mfe_amount">
                                        { item.mfe_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="duration_minutes">
                                        { item.duration_minutes }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_win">
                                        { dataFormatter.booleanFormatter(item.is_win) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!ai_insight_items?.related_trades?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Ai_insight_itemsView;