<?php
session_start();

if(!isset($_SESSION["sess_user"])) {
    header("Location: index.php");
    exit();
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
       <?php  
  if(isset($_GET['search'])) { 
 include('includes/config.php');
           $user=$_GET['user'];
          $fromdate = $_GET['from_date'];
          $todate =$_GET['to_date'];
          $results = mysqli_query($con, "SELECT * FROM `shipment` as s  WHERE s.user_id ='".$user."' AND s.s_date BETWEEN '".$fromdate."' AND '".$todate."' AND s.s_status='1' LIMIT 1");

      $frdate = $_GET['from_date']; $frmdates =date("j-M-Y",strtotime($frdate)); 
      $todate =$_GET['to_date']; $tomdates = date("j-M-Y",strtotime($todate));
      
      
       $billNo = isset($_GET['bill_no']) ? $_GET['bill_no'] : '';
         $billDate = isset($_GET['bill_date']) ? $_GET['bill_date'] : '';

          $preBalance = isset($_GET['previous_bal']) ? $_GET['previous_bal'] : '';
                  $details = isset($_GET['details']) ? $_GET['details'] : '';
      
      
       while($rows = mysqli_fetch_assoc($results)) {
       
       ?> <title>Shipment By <?php $username =$rows['user_id']; echo ("$username.\n From $frmdates To $tomdates."); ?>
    </title>
   
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 <!-- Favicon icon -->
    <link rel="shortcut icon" href="images/logos/soft-lg-1.png" type="image/x-icon" >
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css">
	<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css">
	<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.5.2/css/buttons.bootstrap4.min.css">
	<link rel="stylesheet" href="https://cdn.datatables.net/responsive/2.2.3/css/responsive.bootstrap4.min.css">
</head>


<!--<body onload="window.print()">-->
<body>
<div class="container">

<div class="page-header">
   
     
    <?php
if(!$_GET['hide_logo']){
    $img_file = 'images/logos/madinaconst-lgnew.png';

    // Get image MIME type and base64 encode the image
    $imageInfo = getimagesize($img_file);
    $src = 'data:' . $imageInfo['mime'] . ';base64,' . base64_encode(file_get_contents($img_file));

    // Display the image
    echo '<center><div class="logo"><img height="250" width="350" src="' . $src . '"></div></center>';
}
?>
    
    
     
    <br>
     
    <h3 align="center">Shipment By <?php echo $username; ?>.</small></h3>
    <?php  $frdate = $_GET['from_date']; $frmdates =date("j-M-Y",strtotime($frdate)); 
      $todate =$_GET['to_date']; $tomdates = date("j-M-Y",strtotime($todate));
      echo  ("<center><h3>From $frmdates To $tomdates.<h3></center>");
       ?>
  <?php } ?>

</div>



 <div class="row">
    <div class="col-md-4">
    <?php if($billNo) { ?>
    <b>Bill No <?php echo $billNo; ?></b><br>
   
    
    <?php }  ?>   
    <?php if($details) { ?>
     <b>Details: <?php echo $details;  ?></b>
     <?php }?>
    </div>
    <div class="col-md-4">
        
    </div>
    
    <div class="col-md-4">
        <?php if($billDate) { ?>
    <b>Date <?php echo $billDate; ?></b>
    
    <?php }  ?>   
    </div>
         
</div> 

 
<table id="example" class="table table-striped table-bordered dt-responsive nowrap" style="width:100%">                  
                        
                  <thead>
                  <tr>
                     <th>S No</th> 
                    <th class="text-center">SM No</th>
                    <th class="text-center">Date</th>
                    <th class="text-center">Slip No</th>
                    <th class="text-center">Vendor</th>
                    <th class="text-center">Vehicle</th>
                    <th class="text-center">Item</th>
                    <th class="text-center">Qty</th>
                    <th class="text-center">Measurement</th>
                    <th class="text-center">Rate</th>
                    <th class="text-center">Amount</th>
                 
                  </tr>
                  </thead>
                  <tbody>

 <?php   include('includes/config.php');
       
          $user=$_GET['user'];
          $fromdate = $_GET['from_date'];
          $todate =$_GET['to_date'];
          
      $no=0;
     

 $result = mysqli_query($con, "SELECT * FROM `shipment` as s  WHERE s.user_id ='".$user."' AND s.s_date BETWEEN '".$fromdate."' AND '".$todate."' AND s.s_status='1' ORDER BY s.s_date ASC");

      
      
      
       while($row = mysqli_fetch_assoc($result)) {
       $no++;    
    
      $v = $row['measurement_total'];
      $amount = $row['ship_total'];
      $totalship +=$amount;
      
    
       ?>
                  <tr>
                        <td><?php echo $no; ?></td>
                      <td><?php echo $row['order_num']; ?></td>
                     
                        <td>
                     <?php $ordate = $row['s_date']; echo date("d/m/Y", strtotime($ordate));
                     //$ordates =date("M-j-Y",strtotime($ordate)); echo $ordates; ?></td>
                        <td><?php echo $row['slip_no']; ?></td>
                    
                      <td><?php echo $row['vendor_id']; ?></td>
                      <td><?php echo $row['measure_id']; ?></td>
                     
                      <td><?php echo  $row['material_id']; ?></td>
                      <td><?php echo  $row['qty']; ?></td>
                      <td><?php echo $v; ?></td>
                      <td><?php echo  $row['s_rate']; ?></td>
                      <td><?php echo  $row['ship_total']; ?></td>
                     
                       
                      </tr>
                     
 <?php } ?>
                  </tbody>
              <tfoot>
                  <tr>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                  
                    <th><?php $totalships = (int)$totalship; echo $totalships; ?>
                     <?php if ($preBalance) {
    $totalbillAmount = $preBalance + $totalships;
    //echo $totalbillAmount; // Display the calculated total bill amount
}  ?>
                    </th>
                    
                    </tr>
                  </tfoot>
                </table>
                 </tbody>
    </table>
    
      
  <div class="row">
        <div class="col-md-4">
            
        </div>
        
        <div class="col-md-4">
            
        </div>
        
        <div class="col-md-4">
            <?php
    

    // Use logical OR instead of bitwise OR
    if ($preBalance) { ?>
        <b>Previous Balance: <?php echo $preBalance ?: '0'; ?></b><br>
        <b>Total Bill Amount: <?php echo $totalbillAmount ?: $totalships; ?></b>
    <?php } ?>
            
        </div>
    </div>
   
</div>
<?php } ?>
<!-- you need to include the shieldui css and js assets in order for the components to work -->
  <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.2/js/dataTables.buttons.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.bootstrap4.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.html5.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.print.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.colVis.min.js"></script>
    <script src="https://cdn.datatables.net/responsive/2.2.3/js/dataTables.responsive.min.js"></script>
    <script src="https://cdn.datatables.net/responsive/2.2.3/js/responsive.bootstrap4.min.js"></script>
     <script>
$(document).ready(function() {
    
  
    $('#example').DataTable( {
        dom: 'Bfrtip',
        //"paging": false,
          
        buttons: [
            { extend: 'copyHtml5', footer: true },
            { extend: 'excelHtml5', footer: true },
           // { extend: 'csvHtml5', footer: true },
            { extend: 'pdfHtml5',header:true, footer: true, orientation: 'landscape',
        pageSize: 'A4', customize:  function (doc) { 
                     
                     
                    
                    //doc.layout = 'lightHorizotalLines;'
                    //doc.pageMargins = [45, 30, 50, 90];
                   // doc.pageMargins = [30, 20, 40, 0];
                      
                   doc.defaultStyle.fontSize = 6.4;
                    doc.styles.tableHeader.fontSize = 7;
                    doc.styles.title.fontSize = 10;
                  
                     var table = doc.content[1].table;
                  
                  
               // Set the widths of the columns here
table.widths = ['5%', '9%', '8%', '11%', '11%', '11%', '9%', '7%', '9%', '9%', '9%'];

                    

  // Fetch PHP values for Bill No and Bill Date
                    var billNo = '<?php echo $billNo; ?>';
                    var billDate = '<?php echo $billDate; ?>';
                    var preBalance = '<?php echo $preBalance ?>';
                     var totalshipment = '<?php echo $totalships ?>';
                    var totalBillAmount = '<?php echo $totalbillAmount ?>';
                    
                      
                                        // Set custom header
                    var headerContent = [
                        [
                            { text: billNo ? 'Bill No: ' + billNo : '', alignment: 'left', fontSize: 10,bold:true }, 
                            { text: billDate ? '<?php echo date("j-M-Y", strtotime($billDate)); ?>' : '', alignment: 'right', fontSize: 10,bold:true }
                        ]
                    ];

                    // Add the header to the PDF
                    doc.content.unshift({
                        margin: [0, 0, 0, 10], // Adjust margin for the header
                        table: {
                            widths: ['50%', '50%'], // Split into two columns (left and right)
                            body: headerContent
                        },
                        layout: 'noBorders' // Ensure no borders around the header
                    });

              
// Set text alignment for td cells to center
for (var rowIndex = 0; rowIndex < table.body.length; rowIndex++) {
    var row = table.body[rowIndex];
    row.forEach(function(cell) {
        cell.alignment = 'center';
    });
}


// Assuming columnIndex is the index of the column that should not break across pages
var columnIndex = 0; // Change this to the actual index of the column

// Set the pageBreak property for the entire column
table.body.forEach(function(row) {
    row[columnIndex].pageBreak = 'avoid';
});
                    
                    // Customize the tfoot style
                    if (table.footer && table.footer.length > 0) {
    var footerRow = table.footer[table.footer.length - 1];
    footerRow.forEach(function(cell) {
        cell.alignment = 'center';
        cell.fontSize = 5; // Adjust the font size as needed
    });
}
                 
                  
                    
                    
                  doc.content.splice( 0,0, {
                        
                        alignment: 'center',
                        margin: [ 0, 0, 0, 0 ],
                        image: '<?php echo $src; ?>',
                        width: 200,
                        height: 150,
                       
                          //  bottom:90
                    } );
                    
                    // Add the "Previous Balance" and "Total Bill Amount" at the bottom right
                    if (preBalance) {
                        doc.content.push({
                            margin: [0, 10, 0, 0], // Add margin for the bottom
                            table: {
                                widths: ['50%', '50%'], // Split into two columns: left for labels, right for values
                                body: [
                                    [
                                        { text: 'Previous Balance:', alignment: 'left', fontSize: 10,bold: true },
                                        { text: preBalance ? preBalance : '0', alignment: 'right', fontSize: 10,bold:true }
                                    ],
                                    [
                                        { text: 'Total Bill Amount:', alignment: 'left', fontSize: 10,bold: true },
                                        { text: totalBillAmount ? totalBillAmount : totalshipment, alignment: 'right', fontSize: 10,bold:true }
                                    ]
                                ]
                            },
                            layout: {
                                // Adding thicker borders to the rows
                                hLineWidth: function(i, node) { 
                                    if (i === 0 || i === node.table.body.length) {
                                        return 0; // No border for the top and bottom of the table
                                    }
                                    // Make the borders of the specific rows thicker
                                    if (i === 0 || i === 1) { // First and second row for "Previous Balance" and "Total Bill Amount"
                                        return 2; // Thicker borders for the rows with labels and values
                                    } 
                                    return 0.5; // Default border thickness
                                },
                                vLineWidth: function(i) { 
                                    return 0.5; // Vertical border thickness
                                },
                                hLineColor: function(i, node) { 
                                    // Change color for thicker borders
                                    if (i === 0 || i === 1) {
                                        return '#000000'; // Darker color for thicker borders
                                    }
                                    return '#d3d3d3'; // Lighter color for normal rows
                                },
                                vLineColor: function(i) { 
                                    return '#d3d3d3'; // Vertical border color
                                },
                                paddingLeft: function(i) { return 10; }, // Left padding
                                paddingRight: function(i) { return 10; }, // Right padding
                                paddingTop: function(i) { return 5; }, // Top padding
                                paddingBottom: function(i) { return 5; } // Bottom padding
                            }
                        });
                    }
                    
                    
                           
         } }
        
            
        ]
    } );
} );


     </script>

<!-- Export a Table to PDF - END -->
</div>

</body>
</html>