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/screener_runs/screener_runsSlice'
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 Screener_runsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { screener_runs } = useAppSelector((state) => state.screener_runs)
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ScreenerFilter</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{screener_runs?.screener_filter?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>ResultCount</p>
                  <p>{screener_runs?.result_count || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Screener_results ScreenerRun</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>LastPrice</th>
                                
                                
                                
                                <th>MarketCap</th>
                                
                                
                                
                                <th>P/ERatio</th>
                                
                                
                                
                                <th>Volume</th>
                                
                                
                                
                                <th>52-WeekPerformance</th>
                                
                                
                                
                                <th>Sector</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {screener_runs.screener_results_screener_run && Array.isArray(screener_runs.screener_results_screener_run) &&
                              screener_runs.screener_results_screener_run.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/screener_results/screener_results-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="last_price">
                                        { item.last_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="market_cap">
                                        { item.market_cap }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="pe_ratio">
                                        { item.pe_ratio }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="volume">
                                        { item.volume }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="performance_52w">
                                        { item.performance_52w }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="sector">
                                        { item.sector }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!screener_runs?.screener_results_screener_run?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Screener_runsView;