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

require_once("definitions.php");
?>

<div class="table-responsive">
    
    <table id="datatable-mailboxes" class="table table-bordered text-nowrap">
        <thead>
            <tr>
                <th>Number</th>
                <th>Type</th>
                <th>Occupants</th>
                <th>Actions</th>
            </tr>
        </thead>
        
        <tbody>
            <?php
            // 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("Connection failed: " . $conn->connect_error);
            }

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

            if ($storeKey === 0) {
                echo '<tr><td colspan="4" class="text-center">Invalid store key</td></tr>';
            } else {
                // Prepare SQL query with join to mailbox_attributes to get the attribute name
                $sql = "SELECT mb.mailbox_key, mb.mailboxnumber, mb.mailbox_name, ma.name AS attribute_name
                    FROM mailboxes mb
                    LEFT JOIN mailbox_attributes ma
                        ON mb.mailbox_attributes_key = ma.mailbox_attributes_key AND ma.store_key = mb.store_key
                    WHERE mb.store_key = ?
                    ORDER BY mb.mailboxnumber ASC;";

                $stmt = $conn->prepare($sql);
                if ($stmt === false) {
                    die("Prepare failed: " . $conn->error);
                }

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

                if ($result === false) {
                    die("Error in query: " . $conn->error);
                }

                if ($result->num_rows > 0) {
                    while ($row = $result->fetch_assoc()) {
                        $mailboxNumber = htmlspecialchars($row['mailboxnumber']);
                        // Use the mailbox attribute name for the Type column, fallback to 'N/A' if null
                        $typeName = htmlspecialchars($row['attribute_name'] ?? 'N/A');
                        $mailboxKey = intval($row['mailbox_key']);

                        // Occupants placeholder (replace with real data as needed)
                        $occupantsHtml = '
                            <div class="avatar-list-stacked">
                                <a class="avatar avatar-sm bg-info text-fixed-white avatar-rounded" href="javascript:void(0);">
                                    +0
                                </a>
                            </div>
                        ';

                        echo '
                            <tr>
                                <td>' . $mailboxNumber . '</td>
                                <td>
                                    <button class="btn btn-sm btn-primary-light">' . $typeName . '</button>
                                </td>
                                <td>' . $occupantsHtml . '</td>
                                <td>
                                    <a href="javascript:void(0);" class="text-info fs-14 lh-1 editMailboxBtn" data-mailbox-key="' . $mailboxKey . '">
                                        <i class="ri-edit-line"></i>
                                    </a>
                                    <a href="javascript:void(0);" class="text-danger fs-14 lh-1 deleteMailboxBtn" data-mailbox-key="' . $mailboxKey . '">
                                        <i class="ri-delete-bin-5-line"></i>
                                    </a>
                                </td>
                            </tr>
                        ';
                    }
                } else {
                    echo '<tr><td colspan="4" class="text-center">No mailboxes found</td></tr>';
                }

                $stmt->close();
            }

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