<!DOCTYPE html>
<html>
<head>
  <title>လက်ကား</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: -5px;
    }

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

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

    @media print {
      body {
        width: 210mm;
        height: 297mm;
      }
    }

    /* Add styles for the download button */
    #download-btn {
      display: block;
      margin: 20px auto;
      padding: 10px 20px;
      font-size: 16px;
      background-color: #007bff;
      color: white;
      border: none;
      cursor: pointer;
    }
    #download-btn:hover {
      background-color: #0056b3;
    }
  </style>
</head>

<body>

<a href="sell_lk.php"><--Back</a>
<button id="download-btn">Download as Image</button>
  <div class="voucher-container" id="voucher">
    <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>
                      ";
                  }
                }

                $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']; 
                
                        // Replace 'id' with the actual column name to sort by
                        $sql = "SELECT * FROM ".$user_id." where seller='$user_name' 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><br>
                                    <b style='text-align: left; font-size: 15px;'> နောက်ဆုံးငွေချေရက် - ".$row['exp_debt']."</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 ".$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['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'],0) . '</td>                         
                                      </tr>';
                                      
                                    }
                                }

                      
                    $query = "SELECT voucher, SUM(total/rate) as total_sum, MAX(rate) as rate, MAX(banking) as banking,SUM((discount * qty)/rate) as total_discount 
                    FROM `$user_id` where seller='$user_name'";
                    $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'];

                            

                            $query_currency = "SELECT currency FROM `$banking` WHERE type='$banking_data'";
                            $result_currency = $connection->query($query_currency);
                            if ($result_currency === false) {
                                die("Error executing query: " . $connection->error);
                            }
                            if ($result_currency->num_rows > 0) {
                                $currency_row = $result_currency->fetch_assoc();
                                $currency = $currency_row['currency'];
                                     
                                        echo '  
                                        <tr>  
                                            <td colspan="4" style="text-align:center;">Total</td>       
                                            <td><b>' . ($currency) . '</b></td>
                                            <td><b>' . number_format($row['total_discount']) . '</b></td>
                                            <td><b>' . number_format($row['total_sum']) . '</b></td>
                                            ';
                            }
                        }
                    }              
                            }
                        }

      
                  ?>

            </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>



<!-- Include html2canvas library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>

<script>
  document.getElementById('download-btn').addEventListener('click', function () {
    html2canvas(document.getElementById('voucher')).then(function (canvas) {
      var link = document.createElement('a');
      link.href = canvas.toDataURL('image/png');
      link.download = 'voucher.png';
      link.click();
    });
  });
</script>
</body>
</html>
</body>
</html>

