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/suppliers/suppliersSlice'
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 SuppliersView = () => {
    const router = useRouter()
    const dispatch = useAppDispatch()
    const { suppliers } = useAppSelector((state) => state.suppliers)
    

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

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SupplierName</p>
                    <p>{suppliers?.name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>SupplierCode</p>
                    <p>{suppliers?.supplier_code}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>ContactName</p>
                    <p>{suppliers?.contact_name}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Email</p>
                    <p>{suppliers?.email}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Phone</p>
                    <p>{suppliers?.phone}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

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

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              
                <FormField label='Approved'>
                    <SwitchField
                      field={{name: 'approved', value: suppliers?.approved}}
                      form={{setFieldValue: () => null}}
                      disabled
                    />
                </FormField>
              

              

              

              

              
              

                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Materials PreferredSupplier</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                <th>MaterialCode</th>
                                
                                
                                
                                <th>MaterialName</th>
                                
                                
                                
                                <th>MaterialType</th>
                                
                                
                                
                                
                                
                                <th>StandardCost</th>
                                
                                
                                
                                <th>StandardLeadTimeDays</th>
                                
                                
                                
                                <th>LotTracked</th>
                                
                                
                                
                                <th>SerialTracked</th>
                                
                                
                                
                                <th>ReorderPoint</th>
                                
                                
                                
                                <th>ReorderQuantity</th>
                                
                                
                                
                                
                                
                                <th>Specification</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {suppliers.materials_preferred_supplier && Array.isArray(suppliers.materials_preferred_supplier) &&
                              suppliers.materials_preferred_supplier.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/materials/materials-view/?id=${item.id}`)}>
                                    
                                    
                                    <td data-label="material_code">
                                        { item.material_code }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="name">
                                        { item.name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="material_type">
                                        { item.material_type }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="standard_cost">
                                        { item.standard_cost }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="standard_lead_time_days">
                                        { item.standard_lead_time_days }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="lot_tracked">
                                        { dataFormatter.booleanFormatter(item.lot_tracked) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="serial_tracked">
                                        { dataFormatter.booleanFormatter(item.serial_tracked) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reorder_point">
                                        { item.reorder_point }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="reorder_quantity">
                                        { item.reorder_quantity }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="specification">
                                        { item.specification }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!suppliers?.materials_preferred_supplier?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Inventory_lots Supplier</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                <th>LotNumber</th>
                                
                                
                                
                                <th>SupplierLotNumber</th>
                                
                                
                                
                                
                                
                                <th>ReceivedOn</th>
                                
                                
                                
                                <th>ManufacturedOn</th>
                                
                                
                                
                                <th>ExpiresOn</th>
                                
                                
                                
                                <th>LotStatus</th>
                                
                                
                                
                                <th>CertificateNumber</th>
                                
                                
                                
                                
                                
                                <th>Notes</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {suppliers.inventory_lots_supplier && Array.isArray(suppliers.inventory_lots_supplier) &&
                              suppliers.inventory_lots_supplier.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/inventory_lots/inventory_lots-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    <td data-label="lot_number">
                                        { item.lot_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="supplier_lot_number">
                                        { item.supplier_lot_number }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="received_on">
                                        { dataFormatter.dateTimeFormatter(item.received_on) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="manufactured_on">
                                        { dataFormatter.dateTimeFormatter(item.manufactured_on) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="expires_on">
                                        { dataFormatter.dateTimeFormatter(item.expires_on) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="lot_status">
                                        { item.lot_status }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="certificate_number">
                                        { item.certificate_number }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="notes">
                                        { item.notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!suppliers?.inventory_lots_supplier?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default SuppliersView;