<!DOCTYPE html>
<html>
<head>
  <title>A4 Voucher</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
    }

    .voucher-container {
      width: 210mm; /* A4 paper width */
      height: 297mm; /* A4 paper height */
      background-color: #f2f2f2;
      padding: 5mm;
      box-sizing: border-box;
    }

    table {
      width: 100%;
      border-collapse: collapse;
    }

    th, td {
      padding: 10px;
      border: 1px solid #ccc;
    }

    .voucher-header {
      text-align: center;
      font-size: 24px;
      margin-bottom: 10px;
    }

    .voucher-content {
      font-size: 13px;
    }

    .voucher-footer {
      margin-top: 10px;
      text-align: right;
    }

    @media print {
      body {
        width: 210mm;
        height: 297mm;
      }
    }
  </style>
</head>
<body>
<a href="customer_voucher.php"><--Back</a>
  <div class="voucher-container">
    <table>
      <tr>
        <td colspan="2" class="voucher-header">
            <img src="image/logo.png" style="width: 80px; margin-bottom: -50px;">
            <br><br>
            </img>
            
            <?php
              include 'database/database.php';
              include 'database/data.php';

              session_start();
  
              // Check if the browser ID is already stored in the session
              if (isset($_SESSION['browser_id'])) {
                  $browserId = $_SESSION['browser_id'];
              } else {
                  // Generate a new browser ID
                  $browserId = generateUniqueBrowserId();
                  // Store the browser ID in the session for future use
                  $_SESSION['browser_id'] = $browserId;
              }
              $b_id = $browserId;
  
              $sql = "Select * from ".$shop_name."";
              $result = mysqli_query($connection, $sql);
              if ($result) {
                  while ($row = mysqli_fetch_assoc($result)) {
  
                      echo "
                          <div class='title' style='margin-top:30px'><b>".$row['name']."</b></div>
                          <address style='text-align: left;font-size: 15px;'> ".$row['address']."<br> ".$row['ph_1']."-".$row['ph_2']."</address>
                      ";
                  }
                }

                $voucher = $_POST['voucher_no'];


                
                // Replace 'id' with the actual column name to sort by
                $sql = "SELECT * FROM ".$sell_data." where voucher = ".$voucher." ORDER BY id LIMIT 1"; 
                $result = mysqli_query($connection, $sql);
                
                if ($result) {
                    while ($row = mysqli_fetch_assoc($result)) {
                        echo "<div style='text-align:left; line-height:20px;'>
                            <b style='text-align: left; font-size: 15px;'> ဝယ်ယူသူအမည် - ".$row['customer']."</b><br>
                            <b style='text-align: left; font-size: 15px;'> voucher no - ".$row['voucher']."</b>
                            </div>
                        ";
                    }
                } else {
                    // Handle the error if the query fails
                    // echo "Error: " . $connection->error;
                }
              
              ?>
          
          </td>
      </tr>
      <tr>
        <td colspan="2" class="voucher-content">
            <table><br>
                <tr>
                <th>ပစ္စည်းအမည်</th>
                  <th>အမျိုးစား</th>
                  <th>အရေအတွက်</th>
                  <th>ဈေးနှုန်း</th>
                  <th>လျော့ဈေး</th>
                  <th>စုစုပေါင်းလျော့ဈေး</th>
                  <th>ကျသင့်ငွေ</th>
                </tr>
                    <?php

                    $sql = "SELECT * FROM ".$sell_data." where voucher = ".$voucher.""; 
                    $result = mysqli_query($connection, $sql);
                    if ($result) {
                        while ($row = mysqli_fetch_assoc($result)) {
                          echo '  
                          <tr>  
                              <td contenteditable="true">' . $row['product_name'] . '</td>   
                              <td contenteditable="true">' . $row['type'] . '</td>   
                              <td contenteditable="true">' . $row['qty'] . '</td>   
                              <td contenteditable="true">' . number_format($row['rate'] != 0 ? $row['price'] / $row['rate'] : $row['price'],0) . '</td>
                              <td contenteditable="true">' . number_format(($row['rate'] != 0 ? ($row['discount'] / $row['rate']) : $row['discount']), 0) . '</td>     
                              <td contenteditable="true">' . number_format(($row['rate'] != 0 ? ($row['discount'] / $row['rate']) : $row['discount']) * $row['qty'], 0) . '</td>     
                              <td contenteditable="true">' . number_format($row['rate'] != 0 ? $row['total'] / $row['rate'] : $row['total']) . '</td>                         
                          </tr>';
                        }
                    }

                    $sql = "Select sum(total) as total_amount,SUM(discount * qty) as total_discount,rate FROM ".$sell_data." where voucher = ".$voucher."";
                    $result = mysqli_query($connection, $sql);
                    if ($result) {
                        while ($row = mysqli_fetch_assoc($result)) {

                          echo '  
                          <tr>  
                              <td colspan="5" style="text-align:center;">Total</td>       
                              <td><b>' . number_format($row['total_discount']) . '</b></td>
                              ';

                          // Check if rate is not zero or null to prevent division by zero
                          if ($row['rate'] != 0 && !is_null($row['rate'])) {
                              $total_converted = round($row['total_amount'] / $row['rate'], 2);
                              echo '<td><b>' . number_format($total_converted) . '</b></td>';
                          } else {
                              echo '<td><b>No data</b></td>'; // Show "N/A" or some message if rate is zero
                          }

                          echo '</tr>';
                        }
                    }
                                  
                       

      
                  ?>

            </table>
        </td>
      </tr>

      <tr>
        <?php
        $today = date("Y-m-d");
          echo'
          <td class="voucher-footer" colspan="2">Date: '.$today.'<br>
            <label>ဝယ်မှုအတွက် အထူးကျေးဇူးတင်ရှိပါသည်</label><br>
            <label>Manager Sign</label><br><br><br>
            <label>------------------</label>
        </td>
          ';
        ?>
        
        
      </tr>
    </table>
  </div>
</body>
</html>

