<?php
include "database/database.php";
include "database/data.php";
session_start();

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 " . $user_database . " WHERE browser_id='$b_id'";
$result = $connection->query($sql);
if ($result && $result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $seller = $row['user_name_lk'];
        $user_name = $row['user_name'];

        $sql = "SELECT * FROM " . $seller . " WHERE seller='" . $user_name . "' ORDER BY id DESC";
        $result = mysqli_query($connection, $sql);
        if ($result) {
            while ($row = mysqli_fetch_assoc($result)) {
                // Start row
                echo '<tr>
                    <td contenteditable="true" style="display: none;">' . htmlspecialchars($row['tra_id']) . '</td>
                    <td>
                    <form action="sell_lk.php" method="POST">
                            <input type="hidden" name="tra_id" value="' . htmlspecialchars($row['tra_id']) . '">
                            <input type="hidden" name="action" value="info">
                            <button type="submit" class="btn btn-success" name="info_button">Info</button>
                        </form>
                    </td>
                    <td contenteditable="true">' . htmlspecialchars($row['barcode']) . '</td>
                    <th contenteditable="true" scope="row">' . htmlspecialchars($row['product_name']) . '</td>
                    <td contenteditable="true">
                        <select id="cars" contenteditable="true">
                            <option value="' . $row['type'] . '">' . $row['type'] . '</option>
                            <option value="ဗူး/ခု">ဗူး/ခု</option>
                            <option value="ကဒ်">ကဒ်</option>
                            <option value="ဖာ">ဖာ</option>
                        </select>
                    </td>
                    <td contenteditable="true">' . number_format(htmlspecialchars($row['price'] / $row['rate']), 0) . '</td>
                    <td contenteditable="true">' . number_format(htmlspecialchars($row['discount'] / $row['rate']), 0) . '</td>
                    <td contenteditable="true">' . htmlspecialchars($row['qty']) . '</td>
                    <td contenteditable="true">' . number_format(htmlspecialchars($row['total'] / $row['rate'])) . '</td>
                    <td contenteditable="true">' . (htmlspecialchars($row['banking'])) . '</td>
                    <td contenteditable="true" style="display: none;">' . htmlspecialchars($row['customer']) . '</td>
                    <td>
                        <!-- Delete Form -->
                        <form action="sell_lk.php" method="POST">
                            <input type="hidden" name="tra_id" value="' . htmlspecialchars($row['tra_id']) . '">
                            <input type="hidden" name="action" value="delete">
                            <button type="submit" class="btn btn-danger" name="delete_button">Delete</button>
                        </form>
                    </td>
                    <td><button type="submit" class="btn btn-warning" data-action="upload">အားလုံး</button></td>
                    <td contenteditable="true">' . htmlspecialchars($row['voucher']) . '</td>
                </tr>';
            }
        }
    }

    $query = "SELECT SUM(total/rate) as total_sum, banking FROM `$seller` WHERE seller='" . $user_name . "'";
    $result = $connection->query($query);
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $total = $row['total_sum'];
        $payment_data = $row['banking'];

        $sql = "SELECT currency FROM " . $banking . " WHERE type='$payment_data'";
        $result = $connection->query($sql);
        if ($result && $result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $currency = $row['currency'];

                echo '
                    <tr>
                        <td colspan="6" style="text-align:center;">Total</td>
                        <td colspan="2"><b>' . number_format($total) . ' ' . $currency . '</b></td>
                    </tr>';
            }
        }
    }
} else {
    echo "<tr><td colspan='4'>No records found</td></tr>";
}

function generateUniqueBrowserId()
{
    $randomString = bin2hex(random_bytes(16));
    $timestamp = time();

    return $randomString . $timestamp;
}
?>
