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/fiscal_years/fiscal_yearsSlice'
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 Fiscal_yearsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { fiscal_years } = useAppSelector((state) => state.fiscal_years)
    

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

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Business</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{fiscal_years?.business?.trade_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

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

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='IsClosed'>
                    <SwitchField
                      field={{name: 'is_closed', value: fiscal_years?.is_closed}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

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

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Journal_vouchers FiscalYear</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>VoucherNumber</th>
                                
                                
                                
                                <th>VoucherDate</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Description</th>
                                
                                
                                
                                <th>TotalDebit</th>
                                
                                
                                
                                <th>TotalCredit</th>
                                
                                
                                
                                <th>IsBalanced</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {fiscal_years.journal_vouchers_fiscal_year && Array.isArray(fiscal_years.journal_vouchers_fiscal_year) &&
                              fiscal_years.journal_vouchers_fiscal_year.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/journal_vouchers/journal_vouchers-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="voucher_number">
                                        { item.voucher_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="voucher_date">
                                        { dataFormatter.dateTimeFormatter(item.voucher_date) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="description">
                                        { item.description }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total_debit">
                                        { item.total_debit }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="total_credit">
                                        { item.total_credit }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="is_balanced">
                                        { dataFormatter.booleanFormatter(item.is_balanced) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!fiscal_years?.journal_vouchers_fiscal_year?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Year_end_operations FiscalYear</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>OperationType</th>
                                
                                
                                
                                <th>OperationDate</th>
                                
                                
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {fiscal_years.year_end_operations_fiscal_year && Array.isArray(fiscal_years.year_end_operations_fiscal_year) &&
                              fiscal_years.year_end_operations_fiscal_year.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/year_end_operations/year_end_operations-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="operation_type">
                                        { item.operation_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="operation_date">
                                        { dataFormatter.dateTimeFormatter(item.operation_date) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!fiscal_years?.year_end_operations_fiscal_year?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Fiscal_yearsView;