<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once("definitions.php");
?>



<div class="table-responsive">
    <table id="datatable-sizes" class="table table-bordered text-nowrap">
        <thead>
            <tr>
                <th>Name</th>
                <th>Package Price Group</th>
                <th>Dimensions (L x W)</th>
                <th>Pkg Limit</th>
                <th>Virtual Box</th>
                <th>Group Box</th>
                <th>PS1583</th>
                <th>Web Viewable</th>
                <th>Options</th>
            </tr>
        </thead>
        <tbody>
            <?php
            // Start session if not already started
            if (session_status() == PHP_SESSION_NONE) {
                session_start();
            }

            // Create connection
            $conn = new mysqli(PACKAGE_LOG_DB_SERVER, PACKAGE_LOG_DB_USER, PACKAGE_LOG_DB_PASS, PACKAGE_LOG_DB_NAME);

            // Check connection
            if ($conn->connect_error) {
                die("<tr><td colspan='11' class='text-center text-danger'>Connection failed: " . htmlspecialchars($conn->connect_error) . "</td></tr>");
            }

            // Get store key from session (integer)
            $storeKey = isset($_SESSION['storekey']) ? intval($_SESSION['storekey']) : 0;

            if ($storeKey === 0) {
                echo '<tr><td colspan="11" class="text-center">Invalid or missing store key</td></tr>';
            } else {
                // Prepare SQL query
                $sql = "SELECT 
                            ms.mailbox_size_key, 
                            ms.store_key, 
                            ppg.name AS package_price_group_name, 
                            ms.mailboxsize, 
                            ms.name, 
                            ms.length, 
                            ms.width, 
                            ms.pkglimit, 
                            ms.virtualbox, 
                            ms.groupbox, 
                            ms.ps1583, 
                            ms.webviewable, 
                            ms.no_businesses 
                        FROM mailbox_size ms
                        LEFT JOIN mailbox_size_price_group ppg ON ms.package_price_group = ppg.price_group_key
                        WHERE ms.store_key = ?
                        ORDER BY ms.name ASC";

                $stmt = $conn->prepare($sql);
                if ($stmt === false) {
                    die("<tr><td colspan='11' class='text-center text-danger'>Prepare failed: " . htmlspecialchars($conn->error) . "</td></tr>");
                }

                $stmt->bind_param("i", $storeKey);
                $stmt->execute();
                $result = $stmt->get_result();

                if ($result === false) {
                    die("<tr><td colspan='11' class='text-center text-danger'>Query error: " . htmlspecialchars($conn->error) . "</td></tr>");
                }

                // Helper function to render checkbox html
                function renderCheckbox($value){
                    $checked = ($value === 2) ? 'checked' : '';
                    return '<input type="checkbox" disabled ' . $checked . '>';
                }

                if ($result->num_rows > 0) {
                    while ($row = $result->fetch_assoc()) {
                        // Escape outputs
                        $key = htmlspecialchars($row['mailbox_size_key']);
                        $name = htmlspecialchars($row['name']);
                        $packagePriceGroup = htmlspecialchars($row['package_price_group_name']);
                        $length = htmlspecialchars($row['length']);
                        $width = htmlspecialchars($row['width']);
                        $pkgLimitRaw = intval($row['pkglimit']);
                        $virtualBoxVal = intval($row['virtualbox']);
                        $groupBoxVal = intval($row['groupbox']);
                        $ps1583Val = intval($row['ps1583']);
                        $webViewableVal = intval($row['webviewable']);

                        // Format package limit display
                        $pkgLimit = ($pkgLimitRaw > 100000) ? "None" : number_format($pkgLimitRaw);

                        // Combine length and width
                        $dimensions = $length . ' x ' . $width;

                        echo '<tr>
                                <td>' . $name . '</td>
                                <td>' . $packagePriceGroup . '</td>
                                <td>' . $dimensions . '</td>
                                <td class="text-center">' . $pkgLimit . '</td>
                                <td class="text-center">' . renderCheckbox($virtualBoxVal) . '</td>
                                <td class="text-center">' . renderCheckbox($groupBoxVal) . '</td>
                                <td class="text-center">' . renderCheckbox($ps1583Val) . '</td>
                                <td class="text-center">' . renderCheckbox($webViewableVal) . '</td>
                                <td>
                                    <a href="javascript:void(0);" class="text-info fs-14 lh-1 editMailboxSizeBtn" data-mailbox-size-key="' . $key . '" title="Edit">
                                        <i class="ri-edit-line"></i>
                                    </a>
                                    &nbsp;
                                    <a href="javascript:void(0);" class="text-danger fs-14 lh-1 deleteMailboxSizeBtn" data-mailbox-size-key="' . $key . '" title="Delete">
                                        <i class="ri-delete-bin-5-line"></i>
                                    </a>
                                </td>
                            </tr>';
                    }
                } else {
                    echo '<tr><td colspan="11" class="text-center">No mailbox sizes found for this store.</td></tr>';
                }

                $stmt->close();
            }

            $conn->close();
            ?>
        </tbody>
    </table>
</div>