<?php 

$APurl = 'http://localhost/inspection/Admin/DB/getIntReport.php';
$params =(array(
        'stock'=> $_GET['stock'],
          ));

$params_string = http_build_query($params);
$cha = curl_init();
curl_setopt($cha,CURLOPT_URL, $APurl);
curl_setopt($cha,CURLOPT_POST, true);
curl_setopt($cha,CURLOPT_POSTFIELDS, $params_string);

curl_setopt($cha,CURLOPT_RETURNTRANSFER, true);
$profResult = curl_exec($cha);
$jsonProfArr = json_decode($profResult, true);
//print_r($profResult);
$data = $jsonProfArr['data'];



$APurl = 'http://localhost/inspection/Admin/DB/getExtReport.php';
$params =(array(
        'stock'=> $_GET['stock'],
          ));

$params_string = http_build_query($params);
$cha = curl_init();
curl_setopt($cha,CURLOPT_URL, $APurl);
curl_setopt($cha,CURLOPT_POST, true);
curl_setopt($cha,CURLOPT_POSTFIELDS, $params_string);

curl_setopt($cha,CURLOPT_RETURNTRANSFER, true);
$profResult = curl_exec($cha);
$jsonProfArr = json_decode($profResult, true);
//print_r($profResult);
$data1 = $jsonProfArr['data'];



$APurl = 'http://localhost/inspection/Admin/DB/getEngReport.php';
$params =(array(
        'stock'=> $_GET['stock'],
          ));

$params_string = http_build_query($params);
$cha = curl_init();
curl_setopt($cha,CURLOPT_URL, $APurl);
curl_setopt($cha,CURLOPT_POST, true);
curl_setopt($cha,CURLOPT_POSTFIELDS, $params_string);

curl_setopt($cha,CURLOPT_RETURNTRANSFER, true);
$profResult = curl_exec($cha);
$jsonProfArr = json_decode($profResult, true);
//print_r($profResult);
$data2 = $jsonProfArr['data'];

$APurl = 'http://localhost/inspection/Admin/DB/getComment.php';
$params =(array(
        'stock'=> $_GET['stock'],
          ));

$params_string = http_build_query($params);
$cha = curl_init();
curl_setopt($cha,CURLOPT_URL, $APurl);
curl_setopt($cha,CURLOPT_POST, true);
curl_setopt($cha,CURLOPT_POSTFIELDS, $params_string);

curl_setopt($cha,CURLOPT_RETURNTRANSFER, true);
$profResult = curl_exec($cha);
$jsonProfArr = json_decode($profResult, true);
//print_r($profResult);
$data3 = $jsonProfArr['data'];
$data3 = $data3[0];
 
$finalArray[] = array_merge($data[0],$data1[0],$data2[0]);
 
?>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css"  >
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" ></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" ></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/js/bootstrap.min.js" ></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"  ></script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"  ></script>
 

<style>
  .col-sm-2,.col-sm-12,.col-sm-1{border:1px solid black;  word-wrap: break-word;font-size: 12px;}  
  .col-sm-12{font-size: 15px; margin-top: 10px;}
</style>
<div class="row" id="table" style="margin:5px;  ">
   <!-- <div class="col-sm-1">WIND_SCREEN_L_R_HINDGES</div>
 <div class="col-sm-1"><strong>unknown</strong></div>  -->
 <?php  for($i=0;$i<count($finalArray);$i++){
    $keys = array_keys($finalArray[$i]); 
    foreach($keys as $key){if($key!="id" && $key!="Stock"){?>

  <div class="col-sm-1"><?php echo ucwords(str_replace("_", " ",$key)) ?></div>
  <div class="col-sm-1"><strong><?php echo $finalArray[$i][$key]=="unknown" ? "" : $finalArray[$i][$key];?></strong></div> 
        
  <?php }?>
          
  <?php }}?>  
  <div class="col-sm-12"><strong>Comments:</strong> <?php echo $data3 ['comments'];?></div>
  </div>

  <br/>
  
  <br/>
  

<script>
 
  html2canvas(document.querySelector("#table"),{backgroundColor:null}).then(canvas => {
   // console.log(canvas.toDataURL())
     localStorage.setItem("INT",canvas.toDataURL())
     $.post("genFile.php", {stock:'<?php echo $_GET['stock'];?>',img: canvas.toDataURL()}, function(result){
      window.location.replace("pdf_report.php?stock="+'<?php echo $_GET['stock'];?>')
      // console.log(result)
      // $("span").html(result);
  });
   });

  //  html2canvas(document.querySelector("#CMT"),{backgroundColor:null}).then(canvas => {
  //    localStorage.setItem("CMT",canvas.toDataURL()) 
  //  });

  //  html2canvas(document.querySelector("#Ext"),{backgroundColor:null}).then(canvas => {
  //    localStorage.setItem("Ext",canvas.toDataURL()) 
  //  });

  //  html2canvas(document.querySelector("#ENG"),{backgroundColor:null}).then(canvas => {
  //    localStorage.setItem("ENG",canvas.toDataURL()) 
  //  });

   if (localStorage. getItem("INT") === null ) {

    alert("Can not Generate PDF")
   
  }else{
    document.cookie = "INT" + "=" +localStorage.getItem("INT");
    document.cookie = "CMT" + "=" +localStorage.getItem("CMT");
    document.cookie = "Ext" + "=" +localStorage.getItem("Ext");
    document.cookie = "ENG" + "=" +localStorage.getItem("ENG");
//     $.ajax({
//     type: "POST",
//     url:"reporter.php",
//     data: {data: JSON.stringify(localStorage.getItem("INT"))},
//     dataType: 'binary',     //--> using jquery.binarytransport.js
//     success: function (response) {
//         // Default response type is blob
//         console.log(response)
//        // saveAs(response, "test.pdf"); //--> using FileSaver.js
//     }
// });
  
   // window.location.replace("reporter.php?img="+JSON.stringify(localStorage.getItem("INT")))

 }
</script>