<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://fonts.googleapis.com/css2?family=Pyidaungsu&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha2/dist/css/bootstrap.min.css">
    <title>Print Layout 80 x 3276 mm</title>
    <style>
    body {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        font-family: 'Pyidaungsu', sans-serif;
        color: #000;
    }

    @page {
        size: auto;
        margin: 0;
    }

    @media print {
        body {
            height: 0mm;
            overflow: hidden;
        }
    }

    #print-container {
        width: 80mm; /* Set width to 80mm for the thermal printer */
        padding: 5px;
        overflow: hidden;
    }

    .title {
        text-align: center;
        font-size: 28px; /* Larger font for header */
        font-weight: bold;
    }

    .address,
    .ph_no {
        font-size: 18px; /* Increase font size */
        text-align: center;
        font-weight: bold;
    }

    table {
        font-size: 14px; /* Slightly larger font size for table content */
        width: 100%;
        border-collapse: collapse;
    }

    th, td {
        padding: 2px; /* Reduce padding for compactness */
        text-align: left;
        font-weight: bold; /* Make text bold for better print clarity */
    }

    th {
        font-size: 18px; /* Slightly larger for table headers */
    }

    @media print {
        body * {
            visibility: hidden;
        }

        #print-container, #print-container * {
            visibility: visible;
        }

        #print-container {
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
        }
    }
</style>

</head>

<body>

    <button type="button" id="download-btn" class="btn btn-success" style="margin:20px">Download as Image</button>
    <a href="sell_ll.php"><-- Back</a>
    <a href="print_ll.php">Print 50 MM</a>

    <div id="print-container">

        <?php
            include 'database/database.php';
            include 'database/data.php';

            $sql = "SELECT * FROM ".$shop_name."";
            $result = mysqli_query($connection, $sql);
            if ($result) {
                while ($row = mysqli_fetch_assoc($result)) {
                    echo "
                        <div class='title'><b>".$row['name']."</b></div>
                        <div class='address'>".$row['address']."</div>
                        <div class='ph_no'>".$row['ph_1']."-".$row['ph_2']."</div>
                        
                    ";
                }
            }

            session_start();

            if (isset($_SESSION['browser_id'])) {
                $browserId = $_SESSION['browser_id'];
            } else {
                $browserId = generateUniqueBrowserId();
                $_SESSION['browser_id'] = $browserId;
            }

            $b_id = $browserId;

            echo '<table>';

            $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_LL'];
                    $user_name = $row['user_name'];

                    $query = "SELECT * FROM `$user_id` WHERE seller='$user_name'";
                    $result = $connection->query($query);

                    echo '
                    
                    <tr>
                        <th>Item</th>
       
                        <th>Price</th>
                        <th>Qty</th>
                        <th>Total</th>
                    </tr>';

                    while ($row = $result->fetch_assoc()) {
                        // echo '
                        // <tr>
                        //     <td>' . $row['product_name'] . '</td>
                        //     <td>' . number_format($row['price'] / $row['rate'], 0) . '</td>
                        //     <td>' . $row['qty'] .'-'.$row['type'].'</td>
                        //     <td>' . number_format($row['total'] / $row['rate'], 0).'</td>
                        // </tr>';

                        echo '
                        <tr>
                            <td>' . $row['product_name'] . '</td>
                            <td style="text-align:right">' . number_format($row['price'] / $row['rate'], 0) . '</td>
                            <td style="white-space: nowrap;">' . $row['qty'] .'-ဗူး/ခု</td>
                            <td style="text-align:right">' . number_format($row['total'] / $row['rate'], 0).'</td>
                        </tr>';

                        // echo '
                        // <tr>
                        //     <td colspan=4 style="text-align:center;">' . $row['type'] . '</td>
                        // </tr>';

                    }

                    $query = "SELECT voucher, SUM(total) AS total_sum, MAX(rate) AS rate, MAX(banking) AS banking 
                              FROM `$user_id` 
                              WHERE seller='$user_name' 
                              GROUP BY voucher";
                    $result = $connection->query($query);

                    while ($row = $result->fetch_assoc()) {
                        $currencyQuery = "SELECT currency FROM `$banking` WHERE type='" . $row['banking'] . "'";
                        $currencyResult = $connection->query($currencyQuery);
                        $currency = $currencyResult->fetch_assoc()['currency'];

                        echo '
                        <tr>
                            <td colspan=3 style="text-align:center;"><b>Total (' . $currency . ')</b></td>
                            <td><b>' . number_format($row['total_sum'] / $row['rate'], 0) . '</b></td>
                        </tr>

                        <tr>
                            <td colspan=3 style="margin-left:5px"><b>Casher: '.$user_name.'</b></td>
                        </tr>

                        <tr>
                            <td colspan=3 style="margin-left:5px"><b>Date: '.date("Y-m-d").'</b></td>
                        </tr>
                        
                        ';



                        
                    }
                }
            }

            echo '</table>';


            

            function generateUniqueBrowserId() {
                return bin2hex(random_bytes(16)) . time();
            }
        ?>

 

    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/dom-to-image/2.6.0/dom-to-image.min.js"></script>
    <script>
        document.getElementById('download-btn').addEventListener('click', function () {
            domtoimage.toPng(document.getElementById('print-container'))
                .then(function (dataUrl) {
                    var link = document.createElement('a');
                    link.href = dataUrl;
                    link.download = 'print_layout.png';
                    link.click();
                });
        });
    </script>

</body>

</html>
