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/data_sources/data_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";

import {hasPermission} from "../../helpers/userPermissions";


const Data_sourcesView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { data_sources } = useAppSelector((state) => state.data_sources)
    
    const { currentUser } = useAppSelector((state) => state.auth);
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Declaração</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{data_sources?.declaration?.status ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Instituição</p>
                    <p>{data_sources?.institution_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Transactions Fontededados</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Data</th>
                                
                                
                                
                                <th>Descrição</th>
                                
                                
                                
                                <th>Valor</th>
                                
                                
                                
                                <th>Tipo</th>
                                
                                
                                
                                <th>Categoria(IA)</th>
                                
                                
                                
                                <th>CampoIR</th>
                                
                                
                                
                                <th>FichaIR</th>
                                
                                
                                
                                <th>Tributação</th>
                                
                                
                                
                                <th>Confiança(0-100)</th>
                                
                                
                                
                                <th>Revisadopelousuário</th>
                                
                                
                                
                                <th>Requerrevisãohumana</th>
                                
                                
                                
                                <th>Motivodarevisão</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {data_sources.transactions_data_source && Array.isArray(data_sources.transactions_data_source) &&
                              data_sources.transactions_data_source.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/transactions/transactions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="transaction_date">
                                        { dataFormatter.dateTimeFormatter(item.transaction_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="description">
                                        { item.description }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="amount">
                                        { item.amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="direction">
                                        { item.direction }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ai_category">
                                        { item.ai_category }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ir_field">
                                        { item.ir_field }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="ir_form">
                                        { item.ir_form }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="taxation_type">
                                        { item.taxation_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="confidence_score">
                                        { item.confidence_score }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reviewed_by_user">
                                        { dataFormatter.booleanFormatter(item.reviewed_by_user) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="requires_human_review">
                                        { dataFormatter.booleanFormatter(item.requires_human_review) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="review_reason">
                                        { item.review_reason }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!data_sources?.transactions_data_source?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Data_sourcesView;