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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Requisition</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{purchase_orders?.requisition?.req_number ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              

              

              

              

              

              

              

              

              

              
              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Fournisseur</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        <p>{purchase_orders?.supplier?.legal_name ?? 'No data'}</p>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                </div>
                 
              

              

              

              
              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Numerobondecommande</p>
                    <p>{purchase_orders?.po_number}</p>
                </div>
              

              

              

              

              

              

              

              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>TotalUSD</p>
                  <p>{purchase_orders?.total_amount_usd || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

              
                <div className={'mb-4'}>
                    <p className={'block font-bold mb-2'}>Devise</p>
                    <p>{purchase_orders?.currency ?? 'No data'}</p>
                </div>
              

              

              

              

              

              
              

              

              

              

              
                <div className={'mb-4'}>
                  <p className={'block font-bold mb-2'}>Totaldevise</p>
                  <p>{purchase_orders?.total_amount_original || 'No data'}</p>
                </div>
              

              

              

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

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

              

              

              

              

              

              

              
              

              

              

              

              

              

              

              

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

              

              

              

              

              
              

              

              

              

              

              

              

              

              

              

              

              

              

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

              

              

              

              

              

              

              

              

              

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

              

              

              
              

                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Po_lines Bondecommande</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>Article</th>
                                
                                
                                
                                <th>Description</th>
                                
                                
                                
                                <th>Quantitecommandee</th>
                                
                                
                                
                                <th>Prixunitaire</th>
                                
                                
                                
                                <th>Totalligne</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {purchase_orders.po_lines_purchase_order && Array.isArray(purchase_orders.po_lines_purchase_order) &&
                              purchase_orders.po_lines_purchase_order.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/po_lines/po_lines-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="item_name">
                                        { item.item_name }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="description">
                                        { item.description }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="quantity_ordered">
                                        { item.quantity_ordered }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="unit_price">
                                        { item.unit_price }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="line_total">
                                        { item.line_total }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!purchase_orders?.po_lines_purchase_order?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Goods_receipt_notes Bondecommande</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>NumeroGRN</th>
                                
                                
                                
                                <th>Datereception</th>
                                
                                
                                
                                
                                
                                <th>Controlequalite</th>
                                
                                
                                
                                <th>Observations</th>
                                
                                
                                
                                
                                
                                <th>Signaturerecepteur</th>
                                
                                
                                
                                <th>Statut</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {purchase_orders.goods_receipt_notes_purchase_order && Array.isArray(purchase_orders.goods_receipt_notes_purchase_order) &&
                              purchase_orders.goods_receipt_notes_purchase_order.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/goods_receipt_notes/goods_receipt_notes-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="grn_number">
                                        { item.grn_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="received_at">
                                        { dataFormatter.dateTimeFormatter(item.received_at) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="quality_check">
                                        { item.quality_check }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="observations">
                                        { item.observations }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="receiver_signature">
                                        { item.receiver_signature }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!purchase_orders?.goods_receipt_notes_purchase_order?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                <>
                    <p className={'block font-bold mb-2'}>Service_acceptance_notes Bondecommande</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                <th>NumeroSAN</th>
                                
                                
                                
                                <th>Dateacceptation</th>
                                
                                
                                
                                
                                
                                <th>Checklistlivrables</th>
                                
                                
                                
                                <th>Criteresacceptation</th>
                                
                                
                                
                                <th>Statut</th>
                                
                                
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {purchase_orders.service_acceptance_notes_purchase_order && Array.isArray(purchase_orders.service_acceptance_notes_purchase_order) &&
                              purchase_orders.service_acceptance_notes_purchase_order.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/service_acceptance_notes/service_acceptance_notes-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="san_number">
                                        { item.san_number }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="acceptance_date">
                                        { dataFormatter.dateTimeFormatter(item.acceptance_date) }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="deliverable_checklist">
                                        { item.deliverable_checklist }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="acceptance_criteria">
                                        { item.acceptance_criteria }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!purchase_orders?.service_acceptance_notes_purchase_order?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                <>
                    <p className={'block font-bold mb-2'}>Discrepancy_cases Bondecommande</p>
                    <CardBox
                      className='mb-6 border border-gray-300 rounded overflow-hidden'
                      hasTable
                    >
                        <div className='overflow-x-auto'>
                            <table>
                            <thead>
                            <tr>
                                
                                
                                
                                
                                
                                
                                
                                
                                <th>Type</th>
                                
                                
                                
                                <th>Description</th>
                                
                                
                                
                                
                                
                                <th>Statut</th>
                                
                                
                                
                                
                                
                                <th>Datesignalement</th>
                                
                                
                                
                                <th>Dateresolution</th>
                                
                                
                                
                                <th>Notesderesolution</th>
                                
                                
                            </tr>
                            </thead>
                            <tbody>
                            {purchase_orders.discrepancy_cases_purchase_order && Array.isArray(purchase_orders.discrepancy_cases_purchase_order) &&
                              purchase_orders.discrepancy_cases_purchase_order.map((item: any) => (
                                <tr key={item.id} onClick={() => router.push(`/discrepancy_cases/discrepancy_cases-view/?id=${item.id}`)}>
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="type">
                                        { item.type }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="description">
                                        { item.description }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="status">
                                        { item.status }
                                    </td>
                                    
                                    
                                    
                                    
                                    
                                    <td data-label="reported_at">
                                        { dataFormatter.dateTimeFormatter(item.reported_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="resolved_at">
                                        { dataFormatter.dateTimeFormatter(item.resolved_at) }
                                    </td>
                                    
                                    
                                    
                                    <td data-label="resolution_notes">
                                        { item.resolution_notes }
                                    </td>
                                    
                                    
                                </tr>
                              ))}
                            </tbody>
                        </table>
                        </div>
                        {!purchase_orders?.discrepancy_cases_purchase_order?.length && <div className={'text-center py-4'}>No data</div>}
                    </CardBox>
                </>
                
                
                
                
                
                
                
                

                <BaseDivider />

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

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

export default Purchase_ordersView;