import React, {useState, useEffect} from 'react';
import './inspection-grid.css';
import { Descriptions, Badge, Layout, Row, Col, Card, Button, Spin } from 'antd';
import { PrinterOutlined } from '@ant-design/icons'
import { AlertMessages } from '@gtpl/shared-utils/alert-messages';
import ReactHTMLTableToExcel from 'react-html-table-to-excel';
import { ProductInspecReq, ProductInspectionDto } from '@gtpl/shared-models/production-management';
import { ProductInspecService } from '@gtpl/shared-services/production';
import moment from 'moment';
export interface InspectionGridProps {
  InspectionId: number,
}

export function InspectionGrid
  (props:InspectionGridProps )
  {
  let temp=4.0;
  const [InspectionDetails, setInspectionDetails] = useState<any>();
  const service = new ProductInspecService();
  const [isModalVisible, setIsModalVisible] = useState<boolean>(false);

  // const getCssFromComponent = (fromDoc, toDoc) => {
  //   Array.from(fromDoc.styleSheets).forEach((styleSheet: any) => {
  //     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 element = window.open('', '', 'height=700, width=1024');
     element.document.write(divContents);
     getCssFromComponent(document, element.document);
     element.document.close();
     element.print();
     element.close(); 
     setIsModalVisible(true); 
   };

  // const getData = (InspectionId) => {
  //   const req = new ProductInspecReq(InspectionId);
  //   service.getEvaluation(req).then(res => {
  //     if(res.status){
  //       console.log(res.data);
  //       setInspectionDetails(res.data)
  //     }else{
  //       if(res.intlCode){
  //         setInspectionDetails(undefined);
  //         AlertMessages.getErrorMessage(res.internalMessage);
  //       }else{
  //         AlertMessages.getErrorMessage(res.internalMessage);
  //       }
  //     }
  //   }).catch(err => {
  //     setInspectionDetails(undefined);
  //     AlertMessages.getErrorMessage(err.message);
  //   })
  // }

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

  // function formatAMPM(date) {
  //   var hours = date.getHours();
  //   var minutes = date.getMinutes();
  //   var ampm = hours >= 12 ? 'PM' : 'AM';
  //   hours = hours % 12;
  //   hours = hours ? hours : 12; // the hour '0' should be '12'
  //   minutes = minutes < 10 ? '0'+minutes : minutes;
  //   var strTime = hours + ':' + minutes + ' ' + ampm;
  //   return strTime;
  // }
  
  // console.log(formatAMPM(new Date));
  return (    
  <div>
   <>
   <br/><br/>
   <html>
      <head>      
      </head>
      <body id ='printme'>
      <Button onClick={printOrder} className='noprint' style={{float:'right', backgroundColor:'#69c0ff'}}><span style={{ color: 'white' }}><PrinterOutlined /> Print</span></Button>
      <br/><br/>
        <table style={{width : '100%'}}>
          <tr style={{borderBottom: '0px'}}>
            <td colSpan={4} style={{fontSize: '17px', lineHeight:'12px', paddingTop:'20px', textAlign: 'center' }}>
            <h2 >SANDHYA AQUA EXPORTS PVT. LIMITED, PAMARRU</h2>
            <h3 ><br/><span style={{backgroundColor: '#57595A', color: '#D4E1E7', margin: '4px'}}>PRODUCT-INSPECTION REPORT</span></h3>
          </td>
          </tr>  
          <tr>
            <td colSpan = {4}>
               <h3 style= {{display:'inline'}}>Date: </h3>&ensp;{moment(new Date()).format('YYYY-MM-DD')}
            </td>
          </tr>   
          <tr><td colSpan={4} style={{fontSize: '17px', lineHeight:'12px', paddingTop:'20px', textAlign: 'center' }}>       
          </td></tr>
          <tr>
            <td colSpan={4}>
              <table style={{display: 'table', border: '1px solid black'}}>
                <tr style={{display: 'table-cell'}}>
                  <td style={{display: 'block', border: '1px solid black'}} ><h3>Sample No.</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Time</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Product/Variety</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Temperature</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Code and S.Code</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Packing</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Brand</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Buyer</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Grade</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Species</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Filling Weight</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Frozen Weight </h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Daglazer Weight</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Net Weight</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Sub sample Weight</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Frozen Count</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Deglazer Count</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Net Count</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Total Pieces</h3></td>  
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Glaze %</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Total Odour</h3></td>  
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Cluster 2pcs </h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>General Appearance</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>General Odour</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Freshness</h3></td>
				          <td style={{display: 'block', border: '1px solid black'}}><h3>Texture</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Dehydration</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Discolouration</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Deterioration</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Soft/Semi soft/Peeling</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Hanging Meat/Th Meat</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Black Spot/Tail</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Broken/Damaged pcs.</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Wrong Cut/Button hole</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Legs/Veins/Shells</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Foriegn Matter</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Uniformty(Big/Small)</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Uniformty Ratio</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Product met spec-(Y/M/N)</h3></td>
                  <td style={{display: 'block', border: '1px solid black'}}><h3>Best Before date</h3></td>                 
                </tr> 
                <tr style={{display: 'table-cell'}}>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>Spec.</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
				          <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
				          <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>	
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>				
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>5%</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>5%</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>Nil</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>2%</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>2%</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>5%</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>6%</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>5%</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>2%</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>Nil</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp;</h3></td>
                  <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>&nbsp; </h3></td>
                </tr> 
                {InspectionDetails?.InspectionSamplesInfo.map(sample => {
                  return(                   
                      <tr style={{display: 'table-cell'}}>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}><h3>{sample.InspectionSampleId}</h3></td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.time}</td>                  
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.species}</td>
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.Product}</td>
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.temperature}</td>
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.Code}</td>
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.Packing}</td>	
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.Brand}</td>
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.Buyer}</td>
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.Grade}</td>	
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.Species}</td>
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.FillingWeight}</td>
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.FrozenWeight }</td>	
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.DaglazerWeight}</td>	
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.NetWeight}</td>	
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.SubsampleWeight}</td>	
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.FrozenCount}</td>	
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.DeglazerCount}</td>	
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.NetCount}</td>	
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.TotalPieces}</td>	
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.Glaze }</td>	
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.TotalOdour}</td>	
                        <td style={{display: 'block', textAlign: 'right', border: '1px solid black'}}>{sample.Cluster>2/2 }</td>	
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.generalAppearance}</td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.generalOdour}</td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.freshness}</td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.dehydration}</td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.discolouration}</td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.deterioration}</td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.blackSpotOrTail}</td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.looseShells}</td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.brokenOrDamagedPcs}</td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.fungalGrowth}</td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.softSpecies}</td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.droopingHead}</td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.foreignMatter}</td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.sulphiteKit}</td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.pests}</td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.muddyOdour}</td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.cookedTexture}</td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.cookedFlavor}</td>
                        <td style={{display: 'block', textAlign: 'center', border: '1px solid black'}}>{sample.parasite}</td>
                      </tr> 
                  )   
                })}            
              </table>
            </td>
          </tr>
          <tr>
            <td colSpan={2} style={{border: '1px solid black', borderRight: '0px'}}><h3><u>Remarks and <br/>Corrective Action</u></h3></td>
            <td colSpan={2} style={{border: '1px solid black', borderLeft: '0px'}}></td>
          </tr>
          <tr >
            <td colSpan = {4} style={{textAlign:'center'}}><h3>*Note:</h3>&ensp; G -Good, S - Satisfy, N - Nil/absent, SF - Soft & Firm, US - UnSatisfy, NA - Not Applicable<br/>NCC - No Colour Change, Pc - Piece</td>
          </tr>
          <tr>
            <td colSpan ={2} style={{textAlign: 'center'}}>
            <br/><br/><br/><br/>
            <h3>Checked by</h3>
            </td> 
            <td colSpan ={2} style={{textAlign: 'center'}}>
            <br/><br/><br/><br/>
            <h3>Verified by</h3>
            </td>            
          </tr>
        </table>
        <script type='text/javascript'>

        </script>
      </body>
    </html>     
   </> 
  </div>
  );
}

export default InspectionGrid;
