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/legal_sources/legal_sourcesSlice'
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 Legal_sourcesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { legal_sources } = useAppSelector((state) => state.legal_sources)
    

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

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SourceType</p>
                    <p>{legal_sources?.source_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Jurisdiction</p>
                    <p>{legal_sources?.jurisdiction}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Citation</p>
                    <p>{legal_sources?.citation}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Court</p>
                    <p>{legal_sources?.court}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SourceUrl</p>
                    <p>{legal_sources?.source_url}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Research_results LegalSource</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>RelevanceScore</th>
                                
                                
                                
                                <th>Snippet</th>
                                
                                
                                
                                <th>RuleStatement</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {legal_sources.research_results_legal_source && Array.isArray(legal_sources.research_results_legal_source) &&
                              legal_sources.research_results_legal_source.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/research_results/research_results-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="relevance_score">
                                        { item.relevance_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="snippet">
                                        { item.snippet }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="rule_statement">
                                        { item.rule_statement }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!legal_sources?.research_results_legal_source?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Citator_checks LegalSource</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Standing</th>
                                
                                
                                
                                <th>Analysis</th>
                                
                                
                                
                                <th>ConflictsSummary</th>
                                
                                
                                
                                <th>CheckedAt</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {legal_sources.citator_checks_legal_source && Array.isArray(legal_sources.citator_checks_legal_source) &&
                              legal_sources.citator_checks_legal_source.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/citator_checks/citator_checks-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="standing">
                                        { item.standing }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="analysis">
                                        { item.analysis }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="conflicts_summary">
                                        { item.conflicts_summary }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="checked_at">
                                        { dataFormatter.dateTimeFormatter(item.checked_at) }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!legal_sources?.citator_checks_legal_source?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Legal_sourcesView;