<!DOCTYPE html>
<html>
<head>
  <title>လက်ကား</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
    }


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

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

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

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

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

    @media print {
      body {
        width: 210mm;
        height: 297mm;
      }
    }
  </style>
</head>

<body>
<a href="sell_lk.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>
                      ";
                  }
                }


              ?>
          
          </td>
      </tr>
      <tr>
        <td colspan="2" class="voucher-content">
            <table><br>
                <tr>
                  <th>ပစ္စည်းအမည်</th>
                  <th>အမျိုးစား</th>
                  <th>အရေအတွက်</th>
                  <th>ဈေးနှုန်း</th>
                  <th>ကျသင့်ငွေ</th>
                </tr>
                    <?php

                    $sql = "SELECT * FROM ".$user_database." WHERE browser_id='$b_id'";
                    $result = $connection->query($sql);

                    if ($result->num_rows > 0) {
                        while ($row = $result->fetch_assoc()) {
                            $user_id = $row['user_name_lk'];
                            $user_name = $row['user_name'];

                                $sql = "Select * from ".$user_id." where seller='$user_name'";
                                $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['total'] / $row['rate'] : $row['total'],0) . '</td>                         
                                      </tr>';
                                      
                                    }
                                }
                                  
                            }
                        }

              $query = "SELECT voucher, SUM(total) as total_sum, MAX(rate) as rate, MAX(banking) as banking, SUM(discount * qty) as total_discount 
              FROM `$user_id`";
              $result = $connection->query($query);
              
              if ($result === false) {
                  die("Error executing query: " . $connection->error);
              }
              
              if ($result->num_rows > 0) {
                  while ($row = $result->fetch_assoc()) {
                      $voucher = $row['voucher'];
                      $total_sum = $row['total_sum'];
                      $rate = $row['rate'];
                      $banking_data = $row['banking'];
              
                      // Fetch currency based on banking data
                      $query_currency = "SELECT currency FROM `$banking` WHERE type='$banking_data'";
                      $result_currency = $connection->query($query_currency);
                      
                      if ($result_currency->num_rows > 0) {
                          $currency_row = $result_currency->fetch_assoc();
                          $currency = $currency_row['currency'];
              
                          // Properly close each row to ensure no line breaks
                          echo '<tr>';  
                          echo '<td colspan="3" style="text-align:center;">Total</td>';       
                          echo '<td><b>' . htmlspecialchars($currency) . '</b></td>';
                          echo '<td><b>' . number_format($total_sum - $row['total_discount'], 0) . '</b></td>';
                          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>

