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

              

              

              

              

              

              

              

              

              

              
                {hasPermission(currentUser, 'READ_ORGANIZATIONS') && 
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Organization</p>
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{receipts?.organization?.name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                  } 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Payment</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{receipts?.payment?.payment_reference ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Invoice</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{receipts?.invoice?.invoice_number ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ReceiptNumber</p>
                    <p>{receipts?.receipt_number}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>ReceiptAmount</p>
                  <p>{receipts?.receipt_amount || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Efris_submissions Receipt</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>DocumentType</th>
                                
                                
                                
                                <th>SubmissionStatus</th>
                                
                                
                                
                                <th>SubmittedAt</th>
                                
                                
                                
                                <th>LastAttemptAt</th>
                                
                                
                                
                                <th>AttemptCount</th>
                                
                                
                                
                                <th>RequestPayloadJSON</th>
                                
                                
                                
                                <th>ResponsePayloadJSON</th>
                                
                                
                                
                                <th>EFRISInvoiceNumber</th>
                                
                                
                                
                                <th>EFRISReceiptNumber</th>
                                
                                
                                
                                <th>VerificationCode</th>
                                
                                
                                
                                <th>QRCodeData</th>
                                
                                
                                
                                <th>ErrorCode</th>
                                
                                
                                
                                <th>ErrorMessage</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {receipts.efris_submissions_receipt && Array.isArray(receipts.efris_submissions_receipt) &&
                              receipts.efris_submissions_receipt.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/efris_submissions/efris_submissions-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="document_type">
                                        { item.document_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="submission_status">
                                        { item.submission_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="submitted_at">
                                        { dataFormatter.dateTimeFormatter(item.submitted_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="last_attempt_at">
                                        { dataFormatter.dateTimeFormatter(item.last_attempt_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="attempt_count">
                                        { item.attempt_count }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="request_payload_json">
                                        { item.request_payload_json }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="response_payload_json">
                                        { item.response_payload_json }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="efris_invoice_no">
                                        { item.efris_invoice_no }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="efris_receipt_no">
                                        { item.efris_receipt_no }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="verification_code">
                                        { item.verification_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="qr_code_data">
                                        { item.qr_code_data }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="error_code">
                                        { item.error_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="error_message">
                                        { item.error_message }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!receipts?.efris_submissions_receipt?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                

                <BaseDivider />

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

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

export default ReceiptsView;