<?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)) {
                echo '<tr>
    <td contenteditable="true" style="display: none;" data-column="tra_id">' . 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" data-column="voucher">' . htmlspecialchars($row['voucher']) . '</td>
    <td contenteditable="true" data-column="barcode">' . htmlspecialchars($row['barcode']) . '</td>
    <td contenteditable="true" data-column="product_name">' . htmlspecialchars($row['product_name']) . '</td>';

    echo '<td>
        <select data-column="type" class="editable-select" contenteditable="true">
            <option value="ဗူး/ခု" ' . ($row['type'] == 'ဗူး/ခု' ? 'selected' : '') . '>ဗူး/ခု</option>
            <option value="ကဒ်" ' . ($row['type'] == 'ကဒ်' ? 'selected' : '') . '>ကဒ်</option>
            <option value="ဖာ" ' . ($row['type'] == 'ဖာ' ? 'selected' : '') . '>ဖာ</option>
        </select>
      </td>';

    echo'
    <td contenteditable="true" data-column="price">' . number_format(htmlspecialchars($row['price']), 0) . '</td>
    <td contenteditable="true" data-column="discount">' . number_format(htmlspecialchars($row['discount']), 0) . '</td>
    <td contenteditable="true" data-column="qty">' . htmlspecialchars($row['qty']) . '</td>
    <td contenteditable="true" data-column="total">' . number_format(htmlspecialchars($row['total'])) . '</td>
    <td>'. htmlspecialchars($row['customer']).'</td>
    <td contenteditable="true" data-column="buy_type">' . htmlspecialchars($row['buy_type']) . '</td>
    <td contenteditable="true" data-column="banking">' . htmlspecialchars($row['banking']) . '</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="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" data-column="rate">' . htmlspecialchars($row['rate']) . '</td>
</tr>';

            }
        }
    }

    $query = "SELECT SUM(total) 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="7" 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;
}
?>
