<?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 class="row" id="add-plan-form"> <h5>Add New Mailbox Plan</h5>
<div class="row mb-3">
    <div class="col-sm-6">
        <label for="plan_name" class="form-label">Plan Name</label>
        <input type="text" id="plan_name" name="plan_name" class="form-control" required>
    </div>
    <div class="col-sm-6">
        <label for="web_viewable" class="form-label">Web Viewable</label>
        <div class="form-check form-check-lg form-switch">
            <input class="form-check-input" type="checkbox" role="switch" id="web_viewable" name="web_viewable">
            <label class="form-check-label" for="web_viewable">Yes</label>
        </div>
    </div>
</div>
<div class="row mb-3">
    <label for="mailbox_type" class="form-label">Mailbox Type</label>
    <div class="col-sm-6">
        <select class="form-select" id="mailbox_type" name="mailbox_type" 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>
    <div class="col-sm-3">
        <button type="button" id="addMailboxType" class="btn btn-secondary">Add New</button>
    </div>
</div>
<div class="row mb-3">
    <div class="col-sm-6">
        <label for="monthly_package_limit" class="form-label">Monthly Package Limit</label>
        <input type="number" id="monthly_package_limit" name="monthly_package_limit" class="form-control" min="0" placeholder="1000+ for unlimited">
    </div>
    <div class="col-sm-6">
        <label for="instant_package_limit" class="form-label">Instant Package Limit</label>
        <input type="number" id="instant_package_limit" name="instant_package_limit" class="form-control" min="0" placeholder="1000+ for unlimited">
    </div>
</div>
<div class="row mb-3">
    <div class="col-sm-3">
        <label for="occupant_limit" class="form-label">Occupant Limit</label>
        <input type="number" step="0.01" min="0" id="occupant_limit" name="occupant_limit" class="form-control" required>
    </div>
    <div class="col-sm-3">
        <label for="additional_occupants_fee" class="form-label">Additional Occupants ea ($)</label>
        <input type="number" step="0.01" min="0" id="additional_occupants_fee" name="additional_occupants_fee" class="form-control" required>        
    </div>
    <div class="col-sm-3">
        <label for="hard_limit" class="form-label">Hard Limit</label>
        <input type="number" step="0.01" min="0" id="hard_limit" name="hard_limit" class="form-control" required>        
    </div>
</div>
<div class="row mb-3">
    <div class="col-sm-3">
        <label for="price_1mo" class="form-label">1mo Price ($)</label>
        <input type="number" step="0.01" min="0" id="price_1mo" name="price_1mo" class="form-control" required>
    </div>
    <div class="col-sm-3">
        <label for="price_3mo" class="form-label">3mo Price ($)</label>
        <input type="number" step="0.01" min="0" id="price_3mo" name="price_3mo" class="form-control" required>        
    </div>
    <div class="col-sm-3">
        <label for="price_6mo" class="form-label">6mo Price ($)</label>
        <input type="number" step="0.01" min="0" id="price_6mo" name="price_6mo" class="form-control" required>        
    </div>
    <div class="col-sm-3">
        <label for="price_12mo" class="form-label">1yr Price ($)</label>
        <input type="number" step="0.01" min="0" id="price_12mo" name="price_12mo" class="form-control" required>
    </div>
</div>
<div class="row mb-3">
    <label class="form-label">Free Months</label>
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="free_months_1mo" name="free_months_1mo" class="form-control" required>
    </div>
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="free_months_3mo" name="free_months_3mo" class="form-control" required>        
    </div>
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="free_months_6mo" name="free_months_6mo" class="form-control" required>        
    </div>
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="free_months_12mo" name="free_months_12mo" class="form-control" required>
    </div>
</div>

<div class="row mb-3">
    <div class="col-sm-3">
        <label class="form-label">Actions</label>
    </div>
    <div class="col-sm-3">
        <label class="form-label">Allowance</label>
    </div>
    <div class="col-sm-3">
        <label class="form-label">Extra ea ($)</label>
    </div>
</div>

<div class="row mb-3">
    <div class="col-sm-3">
        <label for="incoming_mail_items_label" class="form-label">Incoming Mail Items</label>
    </div>
    
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="incoming_mail_items_allowance" name="incoming_mail_items_allowance" class="form-control" required>        
    </div>
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="incoming_mail_items_extra_fee" name="incoming_mail_items_extra_fee" class="form-control" required>        
    </div>
</div>

<div class="row mb-3">
    <div class="col-sm-3">
        <label for="open_scan_requests_label" class="form-label">Open & Scan Requests</label>
    </div>
    
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="open_scan_requests_allowance" name="open_scan_requests_allowance" class="form-control" required>        
    </div>
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="open_scan_requests_extra_fee" name="open_scan_requests_extra_fee" class="form-control" required>        
    </div>
</div>

<div class="row mb-3">
    <div class="col-sm-3">
        <label for="scanned_pages_per_month_label" class="form-label">Scanned Pages / Month</label>
    </div>
    
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="scanned_pages_per_month_allowance" name="scanned_pages_per_month_allowance" class="form-control" required>        
    </div>
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="scanned_pages_per_month_extra_fee" name="scanned_pages_per_month_extra_fee" class="form-control" required>        
    </div>
</div>

<div class="row mb-3">
    <div class="col-sm-3">
        <label for="scanned_pages_per_request_label" class="form-label">Scanned Pages / Request</label>
    </div>
    
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="scanned_pages_per_request_allowance" name="scanned_pages_per_request_allowance" class="form-control" required>        
    </div>
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="scanned_pages_per_request_extra_fee" name="scanned_pages_per_request_extra_fee" class="form-control" required>        
    </div>
</div>

<div class="row mb-3">
    <div class="col-sm-3">
        <label for="forward_requests_label" class="form-label">Forward Requests</label>
    </div>
    
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="forward_requests_allowance" name="forward_requests_allowance" class="form-control" required>        
    </div>
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="forward_requests_extra_fee" name="forward_requests_extra_fee" class="form-control" required>        
    </div>
</div>

<div class="row mb-3">
    <div class="col-sm-3">
        <label for="check_deposit_requests_label" class="form-label">Check Deposit Requests</label>
    </div>
    
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="check_deposit_requests_allowance" name="check_deposit_requests_allowance" class="form-control" required>        
    </div>
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="check_deposit_requests_extra_fee" name="check_deposit_requests_extra_fee" class="form-control" required>        
    </div>
</div>

<div class="row mb-3">
    <div class="col-sm-3">
        <label for="recycle_requests_label" class="form-label">Recycle Requests</label>
    </div>
    
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="recycle_requests_allowance" name="recycle_requests_allowance" class="form-control" required>        
    </div>
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="recycle_requests_extra_fee" name="recycle_requests_extra_fee" class="form-control" required>        
    </div>
</div>

<div class="row mb-3">
    <div class="col-sm-3">
        <label for="shred_requests_label" class="form-label">Shred Requests</label>
    </div>
    
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="shred_requests_allowance" name="shred_requests_allowance" class="form-control" required>        
    </div>
    <div class="col-sm-3">
        <input type="number" step="1" min="0" id="shred_requests_extra_fee" name="shred_requests_extra_fee" class="form-control" required>        
    </div>
</div>

<div class="row mb-3">
    <div class="col-sm-4">
        <label for="visit_limit_per_month" class="form-label">Visit Limit / Mo</label>
        <input type="number" id="visit_limit_per_month" name="visit_limit_per_month" class="form-control" min="0" required>
    </div>
    <div class="col-sm-4">
        <label for="cost_per_visit" class="form-label">Cost / Visit ($)</label>
        <input type="number" step="0.01" min="0" id="cost_per_visit" name="cost_per_visit" class="form-control" required>
    </div>
    <div class="col-sm-4">
        <label for="local_pickup" class="form-label">Local Pickup</label>
        <div class="form-check form-check-lg form-switch">
            <input class="form-check-input" type="checkbox" role="switch" id="local_pickup" name="local_pickup">
            <label class="form-check-label" for="local_pickup">Yes</label>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-xs-6">
        <button type="submit" class="btn btn-primary me-2">Save</button>
        <button type="button" id="cancelAddPlan" class="btn btn-secondary">Cancel</button>
    </div>
</div>
</form>