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

require_once("definitions.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;
$storeKey = 1;
$mailboxSizes = [];

if ($storeKey) {
    $sql = "SELECT ms.mailbox_attributes_key, ms.name FROM mailbox_attributes ms WHERE store_key = ? ORDER BY ms.name ASC";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $storeKey);
    $stmt->execute();
    $result = $stmt->get_result();

    while ($row = $result->fetch_assoc()) {
        $mailboxSizes[] = $row;
    }
    $stmt->close();
} else {
    echo 'No Store Key';
    // Handle no store_key or error case as needed
}
?>
<form id="add-mailbox-form">
    <h5>Add New Mailbox Entry</h5>
    <div class="mb-3">
        <label for="mailboxNumber" class="form-label">Mailbox Number</label>
        <input type="text" class="form-control" id="mailboxNumber" name="mailboxNumber" required>
    </div>
    <div class="mb-3">
        <label for="mailboxName" class="form-label">Mailbox Name</label>
        <input type="text" class="form-control" id="mailboxName" name="mailboxName">
    </div>
    <div class="mb-3">
        <label for="mailboxSize" class="form-label">Mailbox Type</label>
        <select class="form-select" id="mailboxSize" name="mailboxSize" required>
            <option value="" disabled selected>Select a size</option>
            <?php foreach ($mailboxSizes as $size): ?>
                <option value="<?= htmlspecialchars($size['mailbox_attributes_key']) ?>">
                    <?= htmlspecialchars($size['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <button type="submit" class="btn btn-primary me-2">Submit</button>
    <button type="button" id="cancelAddEntry" class="btn btn-secondary">Cancel</button>
</form>