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

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

        @media print {
            body {
                height: 0mm;
                overflow: hidden;
                page-break-after: always;
            }
        }

        #print-container {
            width: 58mm;
            overflow: hidden;
            background-color: #f9f9f9;
            padding: 1px;
        }

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

        .address {
            font-size: 30px;
            text-align: center;
        }

        .ph_no {
            font-size: 23px;
            text-align: center;
        }

        table {
            margin-left: 15px;
            font-size: 50px;
        }

        th, td {
            text-align: left;
        }

        @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>
    <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();

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

            // Query to fetch the user
            $sql = "SELECT * FROM ".$user_database." WHERE browser_id='$b_id'";
            $result = $connection->query($sql);

            if ($result === false) {
                die("Error executing query: " . $connection->error);
            }

            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);

                    if ($result === false) {
                        die("Error executing query: " . $connection->error);
                    }


                    if ($result->num_rows > 0) {
                        while ($row = $result->fetch_assoc()) {
                            echo '
                            <table class="table">
                            <tr>
                                <th style="text-align: left; padding: 10px;" colspan=3>'.$row['product_name'].'</th>
                                

                                
                            </tr>

                            <tr>
                                <th style="text-align: left; padding: 10px;">' . number_format($row['price']/$row['rate'],0) . ' x </th>
                                <th style="text-align: left; padding: 10px;">'.$row['qty'].' = </th>
                                <th style="text-align: left; padding: 10px;">' . number_format($row['total']/$row['rate'],0) . '</th>
                            </tr>
                             </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);

                    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 '
                                <table>
                                <tr>
                                    <td style="margin-left:5px">Total ('. $currency .')</td>
                                    <td><b>' . number_format($total_sum / $rate, 0) . '</b></td>
                                </tr>
                                
                                <tr>
                                    <td colspan=2 style="margin-left:5px">ဝယ်ယူအားပေးသည့်အတွက် ကျေးဇူးတင်ရှိပါသည်</td>
                                </tr>
                                </table>
                                <br>
                                <br>
                                <br>
                                <br>
                                <br>
                                <br>


                                
                                ';
                            }
                        }
                    }

                }
            } else {
                echo "No records found.";
            }

            function generateUniqueBrowserId()
            {
                $randomString = bin2hex(random_bytes(16));
                $timestamp = time();
                return $randomString . $timestamp;
            }
        ?>
    </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>
