import { ExporterDataInput } from '@gtpl/shared-models/logistics';
import { PlantInvoiceDetailsModel, PlantInvoiceDetailsRequest, SaleOrderDetailViewInfoDTO, UnitsOfWeightInput } from '@gtpl/shared-models/sale-management';
import { SaleOrderService } from '@gtpl/shared-services/sale-management';
import { AlertMessages } from '@gtpl/shared-utils/alert-messages';
import { Button, Card, Table } from 'antd'
import React, { useEffect, useState } from 'react'

export interface ShipmentDetailsPreProps {
  saleOrderId: number;
  soData: SaleOrderDetailViewInfoDTO;
}

const ShipmentDetailsPreNew = (props:ShipmentDetailsPreProps) => {

  const salOrderService = new SaleOrderService();
  const [plantDetails, setPlantDetails] = useState<PlantInvoiceDetailsModel>();
  // let address: string[];
  // address = (plantDetails?.saleOrderItems.length) ? plantDetails?.saleOrderItems[0].customeraddress : [];
  // let addr: string[];
  // addr = (plantDetails?.saleOrderItems.length) ? plantDetails?.saleOrderItems[0].customeraddress : [];
  let latinName = plantDetails?.saleOrderItems[0]?.itemName === "Vannamei" ? "LITOPENAEUS VANNAMEI" : plantDetails?.saleOrderItems[0]?.itemName === "Black Tiger" || "Sea Tiger" ? "PENAEUS MONODON" : "";


  const getData = (saleOrderId) => {
    const reqObj = new PlantInvoiceDetailsRequest(1);
    salOrderService.getPlantInvoiceDetails(new PlantInvoiceDetailsRequest(saleOrderId)).then((res) => {
      console.log(plantDetails)
      if (res.status) {
        console.log(res.data);
        setPlantDetails(res.data);
      } else {
        if (res.intlCode) {
          setPlantDetails(undefined);
          AlertMessages.getErrorMessage(res.internalMessage);
        } else {
          AlertMessages.getErrorMessage(res.internalMessage);
        }
      }
    })
      .catch((err) => {
        AlertMessages.getErrorMessage(err.message);
        setPlantDetails(undefined);
      });
  };

  useEffect(() => {
    if (props.saleOrderId) {
      getData(props.saleOrderId);
    }
    // window.print()
  }, [props.saleOrderId])

  let exporterDetails = ExporterDataInput;
  const exporterData = exporterDetails.find(
    (item) => item.value == plantDetails?.exporterId
  );
  // const uom = UnitsOfWeightInput.find(uom => uom.value == plantDetails.saleOrderItems[0].uomId);

  // const getCssFromComponent = (fromDoc, toDoc) => {
  //   Array.from(fromDoc.styleSheets).forEach((styleSheet: any) => {
  //     console.log(styleSheet.cssRules);
  //     if (styleSheet.cssRules) { // true for inline styles
  //       const newStyleElement = toDoc.createElement('style');
  //       Array.from(styleSheet.cssRules).forEach((cssRule: any) => {
  //         newStyleElement.appendChild(toDoc.createTextNode(cssRule.cssText));
  //       });
  //       toDoc.head.appendChild(newStyleElement);
  //     }
  //   });
  // }

  const getCssFromComponent = (fromDoc: Document, toDoc: Document) => {
    Array.from(fromDoc.styleSheets).forEach((styleSheet: CSSStyleSheet) => {
      try {
        if (styleSheet?.cssRules) { // true for inline styles and same-origin stylesheets
          const newStyleElement = toDoc.createElement("style");
          Array.from(styleSheet.cssRules).forEach((cssRule: CSSRule) => {
            newStyleElement.appendChild(toDoc.createTextNode(cssRule.cssText));
          });
          toDoc.head.appendChild(newStyleElement);
        }
      } catch (e) {
        console.warn("Could not access stylesheet rules for", styleSheet.href, e);
      }
    });
  };


  const printOrder = () => {
    const divContents = document.getElementById('printme').innerHTML;
    const secondPageContents = document.getElementById('secondPage').innerHTML;
    const element = window.open('', '', 'height=700, width=1024');
    // element.document.write(divContents,secondPageContents,thirdPageContents,forthPageContents);
    element.document.write(`
    <html>
      <head>
        <style>
          @media print {
            .page-break {
              page-break-before: always;
            }

            .no-page-break {
              page-break-inside: avoid;
            }

            #printme, #secondPage, #thirdPage, #forthPage {
              page-break-after: always;
            }
          }
        </style>
      </head>
      <body>
        <div id="printme">${divContents}</div>
        <div id="secondPage" class="page-break">${secondPageContents}</div>
      </body>
    </html>
  `);
    getCssFromComponent(document, element.document);
    element.document.close();
    setTimeout(() => {
      element.print();
      element.close(); // to close window when click on cancel/Save
// model should be open
    }, 1000);
  };


  return (
    <div>
             <Card title="Shipment Details"
          style={{ textAlign: 'center' }}
          headStyle={{ backgroundColor: '#69c0ff', border: 0 }}
          extra={
            <span style={{ color: 'white' }}>
              <Button onClick={printOrder} className="panel_button">
                Print
              </Button>
            </span>
          }>
            <html>
              <body id="printme">
              <div>
       <div style={{textAlign:"center", paddingTop:"40px"}}><h2><u>SHIPMENT DETAILS</u></h2></div>
       <div style={{ display: 'flex', alignItems: 'center' }}>
           <div style={{ width: '250px', marginRight: '10px' }}>
            <b> MBL</b>
           </div>
           <div>
             :{" "}{plantDetails?.trackingNo}
           </div>
           </div>
           <div style={{ display: 'flex', alignItems: 'center' }}>
           <div style={{ width: '250px', marginRight: '10px' }}>
            <b> PO Number</b>
           </div>
           <div>
             :{" "}{(props.soData) ? props.soData.poNo : '-'}
           </div>
         </div>
         <div style={{ display: 'flex', alignItems: 'center' }}>
           <div style={{ width: '250px', marginRight: '10px' }}>
            <b>No. of Cartons</b>
           </div>
           <div>
             :{" "}{plantDetails?.saleOrderItems[0]?.invoicedCases} M/C
           </div>
         </div>
         <div style={{ display: 'flex', alignItems: 'center' }}>
           <div style={{ width: '250px', marginRight: '10px' }}>
            <b>POL</b>
           </div>
           <div>
             :{" "}{plantDetails?.portofloading ? (plantDetails?.portofloading)?.toUpperCase() + ', INDIA' : ''}
           </div>
         </div>
         <div style={{ display: 'flex', alignItems: 'center' }}>
           <div style={{ width: '250px', marginRight: '10px' }}>
            <b>POD</b>
           </div>
           <div>
             :{" "}{(plantDetails?.portOfDischarge ? (plantDetails?.portOfDischarge)?.toUpperCase() : '') + "," + (plantDetails?.country ? plantDetails?.country : '')}
           </div>
         </div>
         <div style={{ display: 'flex', alignItems: 'center' }}>
           <div style={{ width: '250px', marginRight: '10px' }}>
            <b>Container No</b>
           </div>
           <div>
             :{" "}{plantDetails?.saleOrderItems[0]?.containerNo}
           </div>
         </div>
         <div style={{ display: 'flex', alignItems: 'center' }}>
           <div style={{ width: '250px', marginRight: '10px' }}>
            <b>Liner Seal No</b>
           </div>
           <div>
             :{" "}{plantDetails?.saleOrderItems[0]?.linearSelaNo}
           </div>
         </div>
         <div style={{ display: 'flex', alignItems: 'center' }}>
           <div style={{ width: '250px', marginRight: '10px' }}>
            <b>Stuffing place</b>
           </div>
           <div>
             :{" "}
           </div>
         </div>
         </div>
         </body>
            </html>
   </Card>
   {plantDetails?.consignee === "HIGH LINER FOODS INC" && (
  <div id="secondPage">
    <Card className="page-break">
      <div style={{ textAlign: "center", paddingTop: "40px" }}>
        <u>SHIPMENT ADVISE FOR HIGHLINER</u>
      </div>
      <br />
      <div>
        <table style={{ width: "70%" }}>
          <tr>
            <td style={{ width: "15%" }}>Date Of Shipment</td>
            <td style={{ width: "55%", border: "1px solid black", textAlign: "center" }}></td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>PO#</td>
            <td style={{ width: "55%", border: "1px solid black", textAlign: "center" }}>
              {props.soData ? props.soData.poNo : '-'}
            </td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>Packer</td>
            <td style={{ width: "55%", border: "1px solid black", textAlign: "center" }}>
              {plantDetails?.invoicedPlant?.company}
            </td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>Product</td>
            <td style={{ width: "55%", border: "1px solid black", textAlign: "center" }}>
              {((plantDetails?.saleOrderItems[0].foodTypeCode.toLocaleUpperCase('en-US').charAt(0) === "C")
                ? 'COOKED'
                : plantDetails?.saleOrderItems[0].foodTypeName?.toLocaleUpperCase('en-US')) + ' FROZEN ' +
                ((plantDetails?.saleOrderItems[0].valueAdditionOneIdCode === 'EZPeel') ? ' HEADLESS ' : '') +
                plantDetails?.saleOrderItems[0].valueAdditionOneIdName?.toLocaleUpperCase('en-US') + ' ' +
                ((!['HLSO', 'HOSO', 'EZPeel', 'N/A'].includes(plantDetails?.saleOrderItems[0].valueAdditionOneIdCode)) ? " AND " : '') +
                (plantDetails?.saleOrderItems[0].valueAdditionTwoIdCode
                  ? plantDetails?.saleOrderItems[0].valueAdditionTwoIdName?.toLocaleUpperCase('en-US')
                  : '') + '<br />' +
                (plantDetails?.saleOrderItems[0].valueAdditionThreeIdCode
                  ? plantDetails?.saleOrderItems[0].valueAdditionThreeIdName?.toLocaleUpperCase('en-US')
                  : '') + ' ' +
                (plantDetails?.saleOrderItems[0].valueAdditionFiveIdCode
                  ? plantDetails?.saleOrderItems[0].valueAdditionFiveIdName?.toLocaleUpperCase('en-US')
                  : '') + ' ' +
                plantDetails?.saleOrderItems[0].itemName.toLocaleUpperCase('en-US') + ' ' + ',' +
                plantDetails?.saleOrderItems[0].valueAdditionFourIdName?.toLocaleUpperCase('en-US')}
            </td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>Species</td>
            <td style={{ width: "55%", border: "1px solid black", textAlign: "center" }}>
              {latinName}
            </td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>Brand</td>
            <td style={{ width: "55%", border: "1px solid black", textAlign: "center" }}>
              {plantDetails?.saleOrderItems[0].brandName}
            </td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>Packing</td>
            <td style={{ width: "55%", border: "1px solid black", textAlign: "center" }}>
              {plantDetails?.saleOrderItems[0].pouches + "*" + Number(plantDetails?.saleOrderItems[0].pouchWeight)}
            </td>
          </tr>
        </table>
        <br />
        <table style={{ width: "70%" }}>
          <tr style={{ width: "70%" }}>
            <td style={{ width: "15%" }}>Size</td>
            <td style={{ width: "55%", border: "1px solid black", textAlign: "center" }}>
              {((plantDetails?.saleOrderItems[0].valueAdditionOneIdCode === 'EZPeel')
                ? ' HEADLESS '
                : plantDetails?.saleOrderItems[0].valueAdditionOneIdName?.toLocaleUpperCase('en-US')) + ' ' +
                ((!['HLSO', 'HOSO', 'EZPeel', 'N/A'].includes(plantDetails?.saleOrderItems[0].valueAdditionOneIdCode)) ? " AND " : '') +
                (plantDetails?.saleOrderItems[0].valueAdditionTwoIdCode
                  ? plantDetails?.saleOrderItems[0].valueAdditionTwoIdName?.toLocaleUpperCase('en-US')
                  : '') + '<br />' +
                (plantDetails?.saleOrderItems[0].valueAdditionThreeIdCode
                  ? plantDetails?.saleOrderItems[0].valueAdditionThreeIdName?.toLocaleUpperCase('en-US')
                  : '')}
            </td>
          </tr>
          <tr style={{ width: "70%" }}>
            <td style={{ width: "15%" }}>No.of cartons as per PO</td>
            <td style={{ width: "55%", border: "1px solid black", textAlign: "center" }}></td>
          </tr>
          <tr style={{ width: "70%" }}>
            <td style={{ width: "15%" }}>No.of cartons Shipped</td>
            <td style={{ width: "55%", border: "1px solid black", textAlign: "center" }}></td>
          </tr>
        </table>
        <br />
        <table style={{ width: "70%" }}>
          <tr>
            <td style={{ width: "15%" }}>Harvest Date</td>
            <td style={{ width: "18%", border: "1px solid black", textAlign: "center" }}></td>
            <td style={{ width: "18%", border: "1px solid black", textAlign: "center" }}></td>
            <td style={{ width: "19%", border: "1px solid black", textAlign: "center" }}></td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>Entity Name (farm)</td>
            <td style={{ width: "18%", border: "1px solid black", textAlign: "center" }}></td>
            <td style={{ width: "18%", border: "1px solid black", textAlign: "center" }}></td>
            <td style={{ width: "19%", border: "1px solid black", textAlign: "center" }}></td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>Weight</td>
            <td style={{ width: "18%", border: "1px solid black", textAlign: "center" }}></td>
            <td style={{ width: "18%", border: "1px solid black", textAlign: "center" }}></td>
            <td style={{ width: "19%", border: "1px solid black", textAlign: "center" }}></td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>Production date</td>
            <td style={{ width: "18%", border: "1px solid black", textAlign: "center" }}></td>
            <td style={{ width: "18%", border: "1px solid black", textAlign: "center" }}></td>
            <td style={{ width: "19%", border: "1px solid black", textAlign: "center" }}></td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>Prodn Julian Code(YJJJ)</td>
            <td style={{ width: "18%", border: "1px solid black", textAlign: "center" }}></td>
            <td style={{ width: "18%", border: "1px solid black", textAlign: "center" }}></td>
            <td style={{ width: "19%", border: "1px solid black", textAlign: "center" }}></td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>No.of Cartons</td>
            <td style={{ width: "18%", border: "1px solid black", textAlign: "center" }}></td>
            <td style={{ width: "18%", border: "1px solid black", textAlign: "center" }}></td>
            <td style={{ width: "19%", border: "1px solid black", textAlign: "center" }}></td>
          </tr>
        </table>
        <br />
        <table style={{ width: "51%" }}>
          <tr>
            <td style={{ width: "15%" }}>Country of Origin</td>
            <td style={{ width: "36%", border: "1px solid black", textAlign: "left" }}></td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>Method Of Production</td>
            <td style={{ width: "36%", border: "1px solid black", textAlign: "left" }}></td>
          </tr>
        </table>
        <br />
        <table style={{ width: "100%" }}>
          <tr>
            <td style={{ width: "15%" }}>Certification</td>
            <td style={{ width: "18%", border: "1px solid black", textAlign: "center" }}>BAP1*</td>
            <td style={{ width: "18%", border: "1px solid black", textAlign: "center" }}>BAP2*</td>
            <td style={{ width: "19%", border: "1px solid black", textAlign: "center" }}>BAP3*</td>
            <td style={{ width: "15%", border: "1px solid black", textAlign: "center" }}>BAP4*</td>
            <td style={{ width: "15%", border: "1px solid black", textAlign: "center" }}>ASC</td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>Method Of Production</td>
            <td style={{ width: "18%", border: "1px solid black", textAlign: "center" }}></td>
            <td style={{ width: "18%", border: "1px solid black", textAlign: "center" }}></td>
            <td style={{ width: "19%", border: "1px solid black", textAlign: "center" }}></td>
            <td style={{ width: "15%", border: "1px solid black", textAlign: "center" }}></td>
            <td style={{ width: "15%", border: "1px solid black", textAlign: "center" }}></td>
          </tr>
        </table>
        <br />
        <table style={{ width: "100%" }}>
          <tr>
            <td style={{ width: "15%" }}>Shipment start date/time</td>
            <td style={{ width: "75%", border: "1px solid black", textAlign: "center" }}></td>
          </tr>
        </table>
        <br />
        <table style={{ width: "100%" }}>
          <tr>
            <td style={{ width: "15%" }}>Carrier</td>
            <td style={{ width: "75%", border: "1px solid black", textAlign: "center" }}></td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>Master Way bill(BL)#</td>
            <td style={{ width: "75%", border: "1px solid black", textAlign: "center" }}>
              {plantDetails?.trackingNo}
            </td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>House way bill#</td>
            <td style={{ width: "75%", border: "1px solid black", textAlign: "center" }}></td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>Container#</td>
            <td style={{ width: "75%", border: "1px solid black", textAlign: "center" }}>
              {plantDetails?.saleOrderItems[0]?.containerNo}
            </td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>Container Seal#</td>
            <td style={{ width: "75%", border: "1px solid black", textAlign: "center" }}>
              {plantDetails?.saleOrderItems[0]?.linearSelaNo}
            </td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>ETD</td>
            <td style={{ width: "36%", border: "1px solid black", textAlign: "center" }}></td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>ETA</td>
            <td style={{ width: "36%", border: "1px solid black", textAlign: "center" }}></td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>Customs Port(destination)</td>
            <td style={{ width: "75%", border: "1px solid black", textAlign: "center" }}>
              {(plantDetails?.saleOrderItems[0]?.destinationDetails
                ? plantDetails?.saleOrderItems[0]?.destinationDetails
                : '') + ',' + (plantDetails?.country ? plantDetails?.country : '')}
            </td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}>Temperature recorder</td>
            <td style={{ width: "75%", border: "1px solid black", textAlign: "center" }}></td>
          </tr>
        </table>
        <br />
        <table style={{ width: "100%" }}>
          <tr>
            <td style={{ width: "15%" }}>
              <div>OK PO#</div>
              <div>()for GFF's purpose</div>
            </td>
            <td style={{ width: "75%", border: "1px solid black", textAlign: "center" }}>
              {props.soData ? props.soData.poNo : '-'}
            </td>
          </tr>
        </table>
        <br />
        <table style={{ width: "100%" }}>
          <tr>
            <td style={{ width: "15%" }}>Remarks</td>
            <td style={{ width: "75%", border: "1px solid black", textAlign: "center", borderBottom: "none" }}></td>
          </tr>
          <tr>
            <td style={{ width: "15%" }}></td>
            <td style={{ width: "75%", border: "1px solid black", textAlign: "center", borderTop: "none" }}></td>
          </tr>
        </table>
      </div>
    </Card>
  </div>
)}

 
    </div>
  )
}

export default ShipmentDetailsPreNew