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/products/productsSlice'
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 ProductsView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { products } = useAppSelector((state) => state.products)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ProductName</p>
                    <p>{products?.product_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ProductCode</p>
                    <p>{products?.product_code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>RiskClassification</p>
                    <p>{products?.risk_classification ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>UDI-DI</p>
                    <p>{products?.udi_di}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Active'>
                    <SwitchField
                      field={{name: 'active', value: products?.active}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ProductFiles</p>
                    {products?.product_files?.length
                      ? dataFormatter.filesFormatter(products.product_files).map(link => (
                        <button
                          key={link.publicUrl}
                          onClick={(e) => saveFile(e, link.publicUrl, link.name)}
                        >
                            {link.name}
                        </button>
                      )) : <p>No ProductFiles</p>
                    }
                </div>
              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Batches Product</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>BatchNumber</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>ManufactureStartAt</th>
                                
                                
                                
                                <th>ManufactureEndAt</th>
                                
                                
                                
                                <th>QuantityPlanned</th>
                                
                                
                                
                                <th>QuantityProduced</th>
                                
                                
                                
                                <th>QuantityReleased</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.batches_product && Array.isArray(products.batches_product) &&
                              products.batches_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/batches/batches-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="batch_number">
                                        { item.batch_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="manufacture_start_at">
                                        { dataFormatter.dateTimeFormatter(item.manufacture_start_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="manufacture_end_at">
                                        { dataFormatter.dateTimeFormatter(item.manufacture_end_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quantity_planned">
                                        { item.quantity_planned }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quantity_produced">
                                        { item.quantity_produced }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quantity_released">
                                        { item.quantity_released }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.batches_product?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Device_master_records Product</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>DMRNumber</th>
                                
                                
                                
                                <th>Title</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>EffectiveAt</th>
                                
                                
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.device_master_records_product && Array.isArray(products.device_master_records_product) &&
                              products.device_master_records_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/device_master_records/device_master_records-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="dmr_number">
                                        { item.dmr_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="title">
                                        { item.title }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="effective_at">
                                        { dataFormatter.dateTimeFormatter(item.effective_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.device_master_records_product?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Risk_assessments Product</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>RiskFileName</th>
                                
                                
                                
                                <th>RiskFileCode</th>
                                
                                
                                
                                <th>Methodology</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                
                                
                                <th>CreatedOn</th>
                                
                                
                                
                                <th>ReviewDueAt</th>
                                
                                
                                
                                <th>Scope</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.risk_assessments_product && Array.isArray(products.risk_assessments_product) &&
                              products.risk_assessments_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/risk_assessments/risk_assessments-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="risk_file_name">
                                        { item.risk_file_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="risk_file_code">
                                        { item.risk_file_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="methodology">
                                        { item.methodology }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="created_on">
                                        { dataFormatter.dateTimeFormatter(item.created_on) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="review_due_at">
                                        { dataFormatter.dateTimeFormatter(item.review_due_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="scope">
                                        { item.scope }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.risk_assessments_product?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Nonconformances Product</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>NCNumber</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                <th>NonconformanceType</th>
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Description</th>
                                
                                
                                
                                <th>Disposition</th>
                                
                                
                                
                                
                                
                                <th>DetectedAt</th>
                                
                                
                                
                                <th>ClosedAt</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.nonconformances_product && Array.isArray(products.nonconformances_product) &&
                              products.nonconformances_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/nonconformances/nonconformances-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="nc_number">
                                        { item.nc_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="nonconformance_type">
                                        { item.nonconformance_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="description">
                                        { item.description }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="disposition">
                                        { item.disposition }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="detected_at">
                                        { dataFormatter.dateTimeFormatter(item.detected_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="closed_at">
                                        { dataFormatter.dateTimeFormatter(item.closed_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.nonconformances_product?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Complaints Product</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>ComplaintNumber</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                
                                
                                <th>ReporterName</th>
                                
                                
                                
                                <th>ReporterContact</th>
                                
                                
                                
                                <th>ComplaintDescription</th>
                                
                                
                                
                                <th>SeriousInjuryOrDeath</th>
                                
                                
                                
                                <th>Malfunction</th>
                                
                                
                                
                                <th>ReportableToAuthority</th>
                                
                                
                                
                                <th>ReceivedAt</th>
                                
                                
                                
                                <th>ClosedAt</th>
                                
                                
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.complaints_product && Array.isArray(products.complaints_product) &&
                              products.complaints_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/complaints/complaints-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="complaint_number">
                                        { item.complaint_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="reporter_name">
                                        { item.reporter_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reporter_contact">
                                        { item.reporter_contact }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="complaint_description">
                                        { item.complaint_description }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="serious_injury_or_death">
                                        { dataFormatter.booleanFormatter(item.serious_injury_or_death) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="malfunction">
                                        { dataFormatter.booleanFormatter(item.malfunction) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reportable_to_authority">
                                        { dataFormatter.booleanFormatter(item.reportable_to_authority) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="received_at">
                                        { dataFormatter.dateTimeFormatter(item.received_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="closed_at">
                                        { dataFormatter.dateTimeFormatter(item.closed_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.complaints_product?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Regulatory_submissions Product</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>SubmissionName</th>
                                
                                
                                
                                <th>Jurisdiction</th>
                                
                                
                                
                                <th>SubmissionType</th>
                                
                                
                                
                                <th>Status</th>
                                
                                
                                
                                
                                
                                
                                
                                <th>PlannedSubmissionAt</th>
                                
                                
                                
                                <th>SubmittedAt</th>
                                
                                
                                
                                <th>DecisionAt</th>
                                
                                
                                
                                <th>Notes</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {products.regulatory_submissions_product && Array.isArray(products.regulatory_submissions_product) &&
                              products.regulatory_submissions_product.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/regulatory_submissions/regulatory_submissions-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="submission_name">
                                        { item.submission_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="jurisdiction">
                                        { item.jurisdiction }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="submission_type">
                                        { item.submission_type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="planned_submission_at">
                                        { dataFormatter.dateTimeFormatter(item.planned_submission_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="submitted_at">
                                        { dataFormatter.dateTimeFormatter(item.submitted_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="decision_at">
                                        { dataFormatter.dateTimeFormatter(item.decision_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!products?.regulatory_submissions_product?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                

                <BaseDivider />

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

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

export default ProductsView;