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/paper_accounts/paper_accountsSlice'
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 Paper_accountsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { paper_accounts } = useAppSelector((state) => state.paper_accounts)
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>User</p>
                    
                    
                        <p>{paper_accounts?.user?.firstName ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>BaseCurrency</p>
                    <p>{paper_accounts?.base_currency ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>StartingCash</p>
                  <p>{paper_accounts?.starting_cash || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>CashBalance</p>
                  <p>{paper_accounts?.cash_balance || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>BuyingPower</p>
                  <p>{paper_accounts?.buying_power || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Paper_orders PaperAccount</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>PlacedAt</th>
                                
                                
                                
                                <th>FilledAt</th>
                                
                                
                                
                                <th>Side</th>
                                
                                
                                
                                <th>OrderType</th>
                                
                                
                                
                                <th>Quantity</th>
                                
                                
                                
                                <th>LimitPrice</th>
                                
                                
                                
                                <th>StopPrice</th>
                                
                                
                                
                                <th>FilledQuantity</th>
                                
                                
                                
                                <th>AverageFillPrice</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>RejectionReason</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {paper_accounts.paper_orders_paper_account && Array.isArray(paper_accounts.paper_orders_paper_account) &&
                              paper_accounts.paper_orders_paper_account.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/paper_orders/paper_orders-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="placed_at">
                                        { dataFormatter.dateTimeFormatter(item.placed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="filled_at">
                                        { dataFormatter.dateTimeFormatter(item.filled_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="side">
                                        { item.side }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="order_type">
                                        { item.order_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quantity">
                                        { item.quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="limit_price">
                                        { item.limit_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="stop_price">
                                        { item.stop_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="filled_quantity">
                                        { item.filled_quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="average_fill_price">
                                        { item.average_fill_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rejection_reason">
                                        { item.rejection_reason }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!paper_accounts?.paper_orders_paper_account?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Paper_positions PaperAccount</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Quantity</th>
                                
                                
                                
                                <th>AverageCost</th>
                                
                                
                                
                                <th>MarketValue</th>
                                
                                
                                
                                <th>UnrealizedP&L</th>
                                
                                
                                
                                <th>RealizedP&L</th>
                                
                                
                                
                                <th>DayP&L</th>
                                
                                
                                
                                <th>LastPricedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {paper_accounts.paper_positions_paper_account && Array.isArray(paper_accounts.paper_positions_paper_account) &&
                              paper_accounts.paper_positions_paper_account.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/paper_positions/paper_positions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="quantity">
                                        { item.quantity }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="average_cost">
                                        { item.average_cost }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="market_value">
                                        { item.market_value }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="unrealized_pl">
                                        { item.unrealized_pl }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="realized_pl">
                                        { item.realized_pl }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="day_pl">
                                        { item.day_pl }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_priced_at">
                                        { dataFormatter.dateTimeFormatter(item.last_priced_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!paper_accounts?.paper_positions_paper_account?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Portfolio_snapshots PaperAccount</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>SnapshottedAt</th>
                                
                                
                                
                                <th>TotalValue</th>
                                
                                
                                
                                <th>CashValue</th>
                                
                                
                                
                                <th>PositionsValue</th>
                                
                                
                                
                                <th>DayP&L</th>
                                
                                
                                
                                <th>All-timeP&L</th>
                                
                                
                                
                                <th>DayP&LPercent</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {paper_accounts.portfolio_snapshots_paper_account && Array.isArray(paper_accounts.portfolio_snapshots_paper_account) &&
                              paper_accounts.portfolio_snapshots_paper_account.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/portfolio_snapshots/portfolio_snapshots-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="snapshotted_at">
                                        { dataFormatter.dateTimeFormatter(item.snapshotted_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total_value">
                                        { item.total_value }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="cash_value">
                                        { item.cash_value }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="positions_value">
                                        { item.positions_value }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="day_pl">
                                        { item.day_pl }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="all_time_pl">
                                        { item.all_time_pl }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="day_pl_percent">
                                        { item.day_pl_percent }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!paper_accounts?.portfolio_snapshots_paper_account?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Paper_accountsView;