<!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 58mm</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Pyidaungsu', sans-serif;
        }

        #print-container {
            width: 58mm;
            padding: 5px;
            overflow: hidden;
        }

        .title {
            text-align: center;
            font-size: 17px;
        }

        .address, .ph_no {
            font-size: 13px;
            text-align: center;
        }

        table {
            font-size: 13px;
            text-align: center;
            width: 100%;
        }

        /* Print-specific styling for 58mm receipt */
        @media print {
            body * {
                visibility: hidden;
            }

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

            #print-container {
                position: absolute;
                left: 0;
                top: 0;
                width: 58mm;
            }

            .btn {
                display: none;
            }
        }
    </style>
</head>

<body>

    <button type="button" id="print-btn" class="btn btn-primary" style="margin: 20px;">Print</button>
    <a href="sell_ll.php" class="btn btn-secondary" style="margin: 20px;">&larr; Back</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>
                        <div class='ph_no'>".date("Y-m-d")."</div><br>
                    ";
                }
            }

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

            $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 '<table class="table">';
                    while ($row = $result->fetch_assoc()) {
                        echo "
                            <tr>
                                <th colspan=3>".$row['product_name']." - (".$row['type'].")</th>
                            </tr>
                            <tr>
                                <td style='text-align: right;'>".number_format($row['price'] / $row['rate'], 0)." x </td>
                                <td style='text-align: center;'>".$row['qty']." = </td>
                                <td style='text-align: left;'>".number_format($row['total'] / $row['rate'], 0)."</td>
                            </tr>
                        ";
                    }
                    echo '</table>';

                    $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()) {
                        $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);
                        $currency_row = $result_currency->fetch_assoc();
                        $currency = $currency_row['currency'];

                        echo "
                            <table>
                                <tr>
                                    <td>Total ($currency)</td>
                                    <td><b>".number_format($total_sum / $rate, 0)."</b></td>
                                </tr>
                                <tr>
                                    <td colspan=2><b>Casher: $user_name</b></td>
                                </tr>
                                <tr>
                                    <td colspan=2>ဝယ်ယူအားပေးသည့်အတွက် ကျေးဇူးတင်ရှိပါသည်</td>
                                </tr>
                            </table><br><br>
                        ";
                    }
                }
            } else {
                echo "No records found.";
            }

            function generateUniqueBrowserId() {
                return bin2hex(random_bytes(8)).time();
            }
        ?>
    </div>

    <script>
        document.getElementById('print-btn').addEventListener('click', function () {
            window.print();
        });
    </script>
</body>

</html>
