<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Print to XP-80</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
        }
        .print-area {
            width: 80mm; /* Ensures it fits the thermal paper */
            padding: 10px;
            font-size: 14px; /* Increase for better clarity */
   
        }
        .print-button {
            margin-top: 20px;
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
        }

        .print-area .title h2,
.print-area .title p {
  line-height: 1.4; /* or try 1.5 or 20px depending on your font size */
  margin: 4px 0; /* optional: reduce spacing between lines */
}


        /* @media print {
        body {
            font-size: 14px !important;
            font-weight: bold;
            text-align: center !important; 
        }
        .print-area {
            text-align: center !important; 
        }
    } */

    
    @media print {
        #print-btn {
                display: none;  /* Hide the print button */
            }
  body {
    font-family: Arial, sans-serif;
    margin-bottom: 100px;

}

}
    </style>
</head>
<button type="button" id="print-btn" class="btn btn-warning" style="text-align:left;">Print</button>
<script> 
    document.getElementById('print-btn').addEventListener('click', function () {
    window.print();
});

</script>
<body>
            <?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)) {
                    $shop_name = $row['name'];
                    $shop_type = $row['type'];
                    $shop_address = $row['address'];
                    $shop_ph_no = $row['ph_1'].",".$row['ph_2'];

                    // <div style='font-size:10px;text-align:left;margin-left:5px'>Date: ".date("Y-m-d h:i A")."</div>
                }
                                }

            ?>



    <div class="print-area" id="printContent">
        <div style="text-align:center;" class="title">
        <h2><?php echo $shop_name; ?></h2>
        <p><?php echo $shop_type; ?></p>
        <p><?php echo $shop_address; ?></p>
        <p><?php echo $shop_ph_no; ?></p>
        </div>

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

        $query = "SELECT * FROM `$rate` order by id desc limit 1";
        $result = $connection->query($query);
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $yuan_rate = $row['yuan'];
            }
        }
    }
}

$query = "SELECT seller,voucher,customer, SUM(total) as total_sum, MAX(rate) as rate, MAX(banking) as banking,SUM(qty) as total_qty,SUM(discount) as total_discount
FROM `$user_id` 
WHERE seller='$user_name' 
GROUP BY seller";
$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'];
        $total_qty = $row['total_qty'];
        $total_discount = $row['total_discount'];
        $rate = $row['rate'];
        $banking_data = $row['banking'];
        $customer = $row['customer'];
        $seller = $row['seller'];

    
        $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 "
            <div style='line-height:1.2'>
            <div class='title' style='font-size:25px'><b>".$currency.' '. number_format($total_sum, 0) ."</b></div>
            <div class='title' style='font-size:15px'>Total</div>
            <hr>
        </div>
            ";

            echo "
<div style='line-height:1.5; text-align: left;margin-left:10px;margin-bottom:20px'>
<div  style='font-size:15px; margin-left: 0;'>Voucher: ".$voucher."</div> 
<div  style='font-size:15px; margin-left: 0;'>Casher: ".$seller."</div> 
<div style='font-size:15px; margin-left: 0;'>Date: ".date("Y-m-d h:i A")."</div> 
";
        }
    }
}
?>
<hr>
<table style="border-collapse: collapse;">
<?php 
    $query = "SELECT * FROM `$user_id` WHERE seller='$user_name'";
    $result = $connection->query($query);
    
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $qty = $row['qty'];
            $price = $row['price'];
            $banking_type = $row['banking'];
            $product_name = $row['product_name'];
            $total = $row['total'];
    
            // Use a different variable for banking query result
            $sql = "SELECT * FROM ".$banking." WHERE type='$banking_type'";
            $banking_result = $connection->query($sql); 
    
            if ($banking_result && $banking_result->num_rows > 0) {
                while ($banking_row = $banking_result->fetch_assoc()) {
                    $currency = $banking_row['currency'];
    
                    if ($currency == 'kyat'){
                        $currency = 'Ks';
                    }else if($currency == 'baht'){ 
                        $currency = '฿';
                    }else if($currency == 'yuan'){ 
                        $currency = '¥';
                    }
                }
            }

            echo '
                <tr> 
                    <td rowspan="2" style="max-width: 180px; word-wrap: break-word; padding: 5px;text-align:left;">
                        <p style="margin: 0; line-height: 1.2; font-size: 15px;">' . $product_name . '</p>
                    </td>
                    <td style="width: 20%; white-space: nowrap; padding: 5px; text-align:right;">
                        <p style="margin: 0; line-height: 1.2;"><b>' . $currency . ' ' . number_format($total, 0) . '</b></p>
                    </td>
                </tr>
                <tr> 
                    <td style="width: 20%; white-space: nowrap; padding: 5px; text-align:right;">
                        <p style="margin: 0; line-height: 1.2;">' . $qty . ' x ' . $currency . ' ' . $price . '</p>
                    </td>
                </tr>';
                
            // Insert <hr> after each product row
            echo '<tr><td colspan="2" style="border-top: 1px solid #000;"></td></tr>'; 
        }
    }
?>


<?php 
$query = "SELECT voucher, SUM(total) as total_sum, MAX(rate) as rate, MAX(banking) as banking,SUM(qty) as total_qty,SUM(discount*qty) as total_discount
FROM `$user_id` 
WHERE seller='$user_name' 
GROUP BY seller";
$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'];
        $total_qty = $row['total_qty'];
        $total_discount = $row['total_discount'];
        $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'];
            if ($currency == 'kyat'){
                $currency = 'Ks';
            }else if($currency == 'baht'){ 
                $currency = '฿';
            }else if($currency == 'yuan'){ 
                $currency = '¥';
            }
        }
    }
}

?>

<tr> 
    <td style="max-width: 180px; word-wrap: break-word; padding: 5px;text-align:left;">
        <p style="margin: 0; line-height: 1.2; font-size: 15px;"><b>Discount</b></p>
    </td>

    <td style="width: 20%; white-space: nowrap; padding: 5px; text-align:right;">
        <p style="margin: 0; line-height: 1.2;"><?php echo $currency.' '.number_format($total_discount, 0) ?></p>
    </td>
</tr>

<tr> 
    <td style="max-width: 180px; word-wrap: break-word; padding: 5px;text-align:left;">
        <p style="margin: 0; line-height: 1.2; font-size: 15px;"><b>Total</b></p>
    </td>

    <td style="width: 20%; white-space: nowrap; padding: 5px; text-align:right;">
        <p style="margin: 0; line-height: 1.2;"><?php echo $currency.' '.number_format($total_sum, 0) ?></p>
    </td>
</tr>

<tr> 
    <td style="max-width: 180px; word-wrap: break-word; padding: 5px;text-align:left;">
        <p style="margin: 0; line-height: 1.2; font-size: 15px;"><b>Total Qty</b></p>
    </td>

    <td style="width: 20%; white-space: nowrap; padding: 5px; text-align:right;">
        <p style="margin: 0; line-height: 1.2;"><?php echo ' '.number_format($total_qty, 0) ?></p>
    </td>
</tr>

</table>


<hr>
<div>
<p style="text-align: center;font-size:12px">ဝယ်ယူအားပေးမှုအတွက် <br>ကျေးဇူးတင်ရှိပါသည်။</p> 
<p style="text-align: center;font-size:10px">Power by IME App (09 668 330 870)</p>
</div><br>
<p style="text-align: center;">*****************</p>
<p style="text-align: center;">*****************</p>
<p style="text-align: center;">*****************</p>

<style>
    @media print {
        /* Add a blank line or space below the content */
        body {
            margin-bottom: 30px; /* Adjust this value to your preference */
        }
    }
</style>
    </div>

    <!-- <button class="print-button" onclick="printReceipt()">Print Receipt</button>

    <script>
        document.getElementById("date").innerText = new Date().toLocaleString();

        function printReceipt() {
    let printContent = document.getElementById("printContent").innerHTML;
    
    let newWindow = window.open('', '', `width=${screen.width},height=${screen.height}`);
    newWindow.document.write('<html><head><title>Print</title>');
    newWindow.document.write('<style>@media print { body { font-size: 14px; font-weight: bold; text-align: center; } }</style>');
    newWindow.document.write('</head><body>');
    newWindow.document.write(printContent);
    newWindow.document.write('</body></html>');
    newWindow.document.close();
    newWindow.print();
    newWindow.close();
}
    </script> -->

</body>
</html>
