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/tax_periods/tax_periodsSlice'
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 Tax_periodsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { tax_periods } = useAppSelector((state) => state.tax_periods)
    
    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 tax_periods')}</title>
          </Head>
          <SectionMain>
            <SectionTitleLineWithButton icon={mdiChartTimelineVariant} title={removeLastCharacter('View tax_periods')} main>
                <BaseButton
                  color='info'
                  label='Edit'
                  href={`/tax_periods/tax_periods-edit/?id=${id}`}
                />
            </SectionTitleLineWithButton>
            <CardBox>
            

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Merchant</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{tax_periods?.merchant?.business_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>PeriodType</p>
                    <p>{tax_periods?.period_type ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>YearNumber</p>
                  <p>{tax_periods?.year_number || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>PeriodNumber</p>
                  <p>{tax_periods?.period_number || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Vat_declarations TaxPeriod</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>TotalSalesAmount</th>
                                
                                
                                
                                <th>TaxableSalesAmount</th>
                                
                                
                                
                                <th>OutputVATAmount</th>
                                
                                
                                
                                <th>TotalPurchasesAmount</th>
                                
                                
                                
                                <th>InputVATAmount</th>
                                
                                
                                
                                <th>VATPayableAmount</th>
                                
                                
                                
                                
                                
                                <th>GeneratedAt</th>
                                
                                
                                
                                
                                
                                <th>ReviewedAt</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {tax_periods.vat_declarations_tax_period && Array.isArray(tax_periods.vat_declarations_tax_period) &&
                              tax_periods.vat_declarations_tax_period.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/vat_declarations/vat_declarations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total_sales_amount">
                                        { item.total_sales_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="taxable_sales_amount">
                                        { item.taxable_sales_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="output_vat_amount">
                                        { item.output_vat_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total_purchases_amount">
                                        { item.total_purchases_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="input_vat_amount">
                                        { item.input_vat_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="vat_payable_amount">
                                        { item.vat_payable_amount }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="generated_at">
                                        { dataFormatter.dateTimeFormatter(item.generated_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="reviewed_at">
                                        { dataFormatter.dateTimeFormatter(item.reviewed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!tax_periods?.vat_declarations_tax_period?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Tot_declarations TaxPeriod</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>GrossSalesAmount</th>
                                
                                
                                
                                <th>TOTRatePercent</th>
                                
                                
                                
                                <th>TOTPayableAmount</th>
                                
                                
                                
                                
                                
                                <th>GeneratedAt</th>
                                
                                
                                
                                
                                
                                <th>ReviewedAt</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {tax_periods.tot_declarations_tax_period && Array.isArray(tax_periods.tot_declarations_tax_period) &&
                              tax_periods.tot_declarations_tax_period.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/tot_declarations/tot_declarations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="gross_sales_amount">
                                        { item.gross_sales_amount }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tot_rate_percent">
                                        { item.tot_rate_percent }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="tot_payable_amount">
                                        { item.tot_payable_amount }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="generated_at">
                                        { dataFormatter.dateTimeFormatter(item.generated_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="reviewed_at">
                                        { dataFormatter.dateTimeFormatter(item.reviewed_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!tax_periods?.tot_declarations_tax_period?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Declaration_submissions TaxPeriod</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>DeclarationType</th>
                                
                                
                                
                                <th>SubmissionReference</th>
                                
                                
                                
                                <th>SubmittedAt</th>
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>ResponseMessage</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {tax_periods.declaration_submissions_tax_period && Array.isArray(tax_periods.declaration_submissions_tax_period) &&
                              tax_periods.declaration_submissions_tax_period.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/declaration_submissions/declaration_submissions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="declaration_type">
                                        { item.declaration_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="submission_reference">
                                        { item.submission_reference }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="submitted_at">
                                        { dataFormatter.dateTimeFormatter(item.submitted_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="response_message">
                                        { item.response_message }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!tax_periods?.declaration_submissions_tax_period?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                

                <BaseDivider />

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

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

export default Tax_periodsView;