<?php
session_start();
require_once("definitions.php");

// Validate store key
$store_key = $_SESSION['storekey'] ?? 0;
if ($store_key <= 0) {
    echo "<p>Error: Invalid store session.</p>";
    exit;
}

// Connect to database
$conn = new mysqli(PACKAGE_LOG_DB_SERVER, PACKAGE_LOG_DB_USER, PACKAGE_LOG_DB_PASS, PACKAGE_LOG_DB_NAME);
if ($conn->connect_error) {
    echo "<p>Error: Database connection failed.</p>";
    exit;
}
$conn->set_charset("utf8mb4");

// Get mailbox_attributes_key from GET, default 0
$attrKey = isset($_GET['mailbox_attributes_key']) ? intval($_GET['mailbox_attributes_key']) : 0;

$name = "";
$length = "";
$width = "";
$virtualbox = 0;
$groupbox = 0;
$ps1583 = 0;
$webviewable = 0;
$businesses = 0;

if ($attrKey > 0) {
    // Fetch existing attribute data
    $stmt = $conn->prepare("SELECT name, length, width, virtualbox, groupbox, ps1583, webviewable, businesses FROM mailbox_attributes WHERE mailbox_attributes_key = ? AND store_key = ?");
    $stmt->bind_param("ii", $attrKey, $store_key);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($row = $result->fetch_assoc()) {
        $name = htmlspecialchars($row['name']);
        $length = htmlspecialchars($row['length']);
        $width = htmlspecialchars($row['width']);
        $virtualbox = (int)$row['virtualbox'];
        $groupbox = (int)$row['groupbox'];
        $ps1583 = (int)$row['ps1583'];
        $webviewable = (int)$row['webviewable'];
        $businesses = (int)$row['businesses'];
    } else {
        echo "<p>Error: Mailbox attribute not found.</p>";
        exit;
    }
    $stmt->close();
}

$conn->close();
?>

<form id="mailbox-attribute-form">
    <input type="hidden" name="mailbox_attributes_key" value="<?php echo $attrKey; ?>">
    <input type="hidden" name="original_name" value="<?php echo $name; ?>">
    <div class="mb-3">
        <label for="name" class="form-label">Name *</label>
        <input type="text" id="name" name="name" class="form-control" required value="<?php echo $name; ?>">
    </div>

    <div class="mb-3">
        <label for="length" class="form-label">Length</label>
        <input type="text" id="length" name="length" class="form-control" value="<?php echo $length; ?>">
    </div>

    <div class="mb-3">
        <label for="width" class="form-label">Width</label>
        <input type="text" id="width" name="width" class="form-control" value="<?php echo $width; ?>">
    </div>

    <div class="form-check">
        <input type="checkbox" id="virtualbox" name="virtualbox" class="form-check-input" <?php echo $virtualbox ? 'checked' : ''; ?>>
        <label for="virtualbox" class="form-check-label">Virtual Box</label>
    </div>

    <div class="form-check">
        <input type="checkbox" id="groupbox" name="groupbox" class="form-check-input" <?php echo $groupbox ? 'checked' : ''; ?>>
        <label for="groupbox" class="form-check-label">Group Box</label>
    </div>

    <div class="form-check">
        <input type="checkbox" id="ps1583" name="ps1583" class="form-check-input" <?php echo $ps1583 ? 'checked' : ''; ?>>
        <label for="ps1583" class="form-check-label">PS1583</label>
    </div>

    <div class="form-check">
        <input type="checkbox" id="webviewable" name="webviewable" class="form-check-input" <?php echo $webviewable ? 'checked' : ''; ?>>
        <label for="webviewable" class="form-check-label">Web Viewable</label>
    </div>

    <div class="form-check">
        <input type="checkbox" id="businesses" name="businesses" class="form-check-input" <?php echo $businesses ? 'checked' : '' ?>>
        <label for="businesses" class="form-check-label">Businesses</label>
    </div>

    <button type="submit" class="btn btn-primary">Save</button>
    <button type="button" id="cancelMailboxAttribute" class="btn btn-secondary">Cancel</button>
</form>