<?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  $catagories where active='1'";
$result = $connection->query($sql);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $type = $row['type'];


        $sql = "Select * from " . $product . " where delete_d='0' and type='$type' order by id desc";
        $result = mysqli_query($connection, $sql);
        if ($result) {
            while ($row = mysqli_fetch_assoc($result)) {

      


                echo '<tr>
<td scope="row" class="fixed-column-3" data-column="product_name" style="display:none">' . htmlspecialchars($row['product_name']) . '</td>                                                <td contenteditable="true" class="fixed-column">' . htmlspecialchars($row['id']) . '</td>       
                                                <td><button type="button" class="btn btn-success" data-action="info" name="info" data-toggle="modal" data-target="#infoModal">update</button></td>     
                                                <td  class="fixed-column-2">' . htmlspecialchars($row['barcode']) . '</td>
                                                <td  class="fixed-column-2">' . htmlspecialchars($row['barcode_1']) . '</td>
                                                <th scope="row" class="fixed-column-3">' . htmlspecialchars($row['product_name']) . '</th>



                                                <td class="fixed-column-3">' . htmlspecialchars($row['type']) . '</td>  
                                                                                    
                                                <td contenteditable="true" data-column="trans">' . htmlspecialchars($row['trans']) . '</td>      
                                                <td contenteditable="true" data-column="unit">' . htmlspecialchars($row['unit']) . '</td>
                                                <td contenteditable="true" data-column="pieces">' . htmlspecialchars($row['pieces']) . '</td>      
                                                <td contenteditable="true" data-column="buy">' . number_format($row['buy']) . '</td>
                                                <td contenteditable="true" data-column="buy">' . ($row['currency_buy']) . '</td>
                                                ';

                // echo '<td contenteditable="true">' . htmlspecialchars($row['rate']) . '</td>';

                // echo '<td contenteditable="true">
                //                                     <select id="cars" contenteditable="true">
                //                                         <option value="' . $row['currency_buy'] . '">' . $row['currency_buy'] . '</option>
                //                                         <option value="kyat">kyat</option>
                //                                         <option value="baht">baht</option>
                //                                         <option value="yuan">yuan</option>
                //                                     </select></td>';

                echo '
                                                    <td  style="color:white">' . htmlspecialchars($row['rate']) . '</td>';


                echo '
                                                <td  style="color:#FFA62F">' . number_format($row['round_package_price']) . '</td>
                                                <td  style="color:#FFA62F">' . number_format($row['round_unit_price']) . '</td>
                                                <td  style="color:#FFA62F">' . number_format($row['round_pieces_price']) . '</td>';

                echo '
                                                <td style="color:#42f560">' . number_format($row['package_exist']) . '</td>
                                                <td style="color:#42f560">' . number_format($row['unit_exist']) . '</td>
                                                <td style="color:#42f560">' . number_format($row['pieces_exist']) . '</td>';

                echo '
                                                <td style="color:#00ffbb">' . number_format($row['package_remain']) . '</td>
                                                <td style="color:#00ffbb">' . number_format($row['unit_remain']) . '</td>
                                                <td style="color:#00ffbb">' . number_format($row['pieces_remain']) . '</td>';

                echo '
                                                <td style="color:#fff41c">' . number_format($row['package_trans']) . '</td>
                                                <td style="color:#fff41c">' . number_format($row['unit_trans']) . '</td>
                                                <td style="color:#fff41c">' . number_format($row['pieces_trans']) . '</td>';

                echo '
                                                <td style="color:#0866ff">' . number_format($row['package_sell']) . '</td>
                                                <td style="color:#0866ff">' . number_format($row['unit_sell']) . '</td>
                                                <td style="color:#0866ff">' . number_format($row['pieces_sell']) . '</td>';

                echo '
                                                <td style="color:#ff0d41">' . number_format($row['package_damage']) . '</td>
                                                <td style="color:#ff0d41">' . number_format($row['unit_damage']) . '</td>
                                                <td style="color:#ff0d41">' . number_format($row['pieces_damage']) . '</td>';

                 echo '<td>
                                                <button type="button" class="btn btn-danger" data-action="delete" data-product-name="' . htmlspecialchars($row['product_name']) . '">Delete</button>
                                              </td>
              
                                                </tr>';
            }
        }
    }
}

mysqli_free_result($result);

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

    return $randomString . $timestamp;
}
?>

