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

function h($str) {
    return htmlspecialchars($str, ENT_QUOTES);
}

$plan_key = isset($_GET['plan_key']) ? intval($_GET['plan_key']) : 0;
$storeKey = isset($_SESSION['storekey']) ? intval($_SESSION['storekey']) : 0;

if ($storeKey === 0) {
    echo "<p>Invalid store key.</p>";
    exit;
}

$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>Database connection failed.</p>";
    exit;
}

// Load mailbox attributes (mailbox types) for dropdown
$mailboxAttributes = [];
$sql = "SELECT mailbox_attributes_key, name FROM mailbox_attributes WHERE store_key = ? ORDER BY name ASC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $storeKey);
$stmt->execute();
$res = $stmt->get_result();
while ($row = $res->fetch_assoc()) {
    $mailboxAttributes[] = $row;
}
$stmt->close();

$plan = [
    'plan_key' => 0,
    'store_key' => $storeKey,
    'mailbox_attribute_key' => '',
    'name' => '',
    'order' => 0,
    'businesses' => 0,
    'occupant_limit' => 0,
    'additional_occupants_fee' => 0.00,
    'package_limit' => 0,
    'pkglimit_mo' => 0,
    'pkglimit_instant' => 0,
    'price_1mo' => 0.00,
    'price_3mo' => 0.00,
    'price_6mo' => 0.00,
    'price_12mo' => 0.00,
    'free_months_1mo' => 0,
    'free_months_3mo' => 0,
    'free_months_6mo' => 0,
    'free_months_12mo' => 0,
    'web_viewable' => 0,
    'incoming_mail_items_allowance' => 0,
    'incoming_mail_items_extra_fee' => 0.00,
    'open_scan_requests_allowance' => 0,
    'open_scan_requests_extra_fee' => 0.00,
    'scanned_pages_per_month_allowance' => 0,
    'scanned_pages_per_month_extra_fee' => 0.00,
    'scanned_pages_per_request_allowance' => 0,
    'scanned_pages_per_request_extra_fee' => 0.00,
    'forward_requests_allowance' => 0,
    'forward_requests_extra_fee' => 0.00,
    'check_deposit_requests_allowance' => 0,
    'check_deposit_requests_extra_fee' => 0.00,
    'recycle_requests_allowance' => 0,
    'recycle_requests_extra_fee' => 0.00,
    'shred_requests_allowance' => 0,
    'shred_requests_extra_fee' => 0.00,
    'visit_limit_per_month' => 0,
    'cost_per_extra_visit' => 0.00,
    'local_pickup' => 0,
];

if ($plan_key > 0) {
    $sql = "SELECT * FROM mailbox_plans WHERE plan_key = ? AND store_key = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ii", $plan_key, $storeKey);
    $stmt->execute();
    $result = $stmt->get_result();
    $data = $result->fetch_assoc();
    if ($data) {
        $plan = $data;
    } else {
        echo "<p>Mailbox plan not found.</p>";
        $stmt->close();
        $conn->close();
        exit;
    }
    $stmt->close();
}

$conn->close();
?>

<form id="edit-mailbox-plan-form" class="row g-3">
    <h5><?= $plan_key > 0 ? 'Edit Mailbox Plan' : 'New Mailbox Plan' ?></h5>
    <input type="hidden" name="plan_key" value="<?= h($plan['plan_key']) ?>">
    <input type="hidden" name="store_key" value="<?= h($storeKey) ?>">

    <div class="col-md-6">
        <label for="name" class="form-label">Plan Name</label>
        <input type="text" maxlength="128" id="name" name="name" class="form-control" required value="<?= h($plan['name']) ?>">
    </div>

    <div class="col-md-6">
        <label for="mailbox_attribute_key" class="form-label">Mailbox Type</label>
        <select id="mailbox_attribute_key" name="mailbox_attribute_key" class="form-select" required>
            <option value="" disabled <?= $plan['mailbox_attribute_key'] === '' ? 'selected' : '' ?>>Select a type</option>
            <?php foreach ($mailboxAttributes as $attr): ?>
                <option value="<?= h($attr['mailbox_attributes_key']) ?>" <?= $attr['mailbox_attributes_key'] == $plan['mailbox_attribute_key'] ? 'selected' : '' ?>>
                    <?= h($attr['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>

    <div class="col-md-6">
        <label for="web_viewable" class="form-label">Web Viewable</label>
        <div class="form-check form-switch">
            <input class="form-check-input" type="checkbox" id="web_viewable" name="web_viewable" value="1" <?= $plan['web_viewable'] ? 'checked' : '' ?>>
            <label class="form-check-label" for="web_viewable">Yes</label>
        </div>
    </div>

    <div class="col-md-6">
        <label for="pkglimit_mo" class="form-label">Monthly Package Limit</label>
        <input type="number" min="0" id="pkglimit_mo" name="pkglimit_mo" class="form-control" placeholder="1000+ for unlimited" value="<?= h($plan['pkglimit_mo']) ?>">
    </div>

    <div class="col-md-6">
        <label for="pkglimit_instant" class="form-label">Instant Package Limit</label>
        <input type="number" min="0" id="pkglimit_instant" name="pkglimit_instant" class="form-control" placeholder="1000+ for unlimited" value="<?= h($plan['pkglimit_instant']) ?>">
    </div>

    <div class="col-md-4">
        <label for="occupant_limit" class="form-label">Occupant Limit</label>
        <input type="number" step="1" min="0" id="occupant_limit" name="occupant_limit" class="form-control" required value="<?= h($plan['occupant_limit']) ?>">
    </div>

    <div class="col-md-4">
        <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 value="<?= h($plan['additional_occupants_fee']) ?>">
    </div>

    <div class="col-md-4">
        <label for="hard_limit" class="form-label">Hard Limit</label>
        <input type="number" step="0.01" min="0" id="package_limit" name="package_limit" class="form-control" required value="<?= h($plan['package_limit']) ?>">
    </div>

    <div class="col-md-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 value="<?= h($plan['price_1mo']) ?>">
    </div>
    <div class="col-md-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 value="<?= h($plan['price_3mo']) ?>">
    </div>
    <div class="col-md-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 value="<?= h($plan['price_6mo']) ?>">
    </div>
    <div class="col-md-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 value="<?= h($plan['price_12mo']) ?>">
    </div>

    <!-- Free Months -->
    <div class="col-md-3">
        <label for="free_months_1mo" class="form-label">Free Months 1mo</label>
        <input type="number" min="0" step="1" id="free_months_1mo" name="free_months_1mo" class="form-control" required value="<?= h($plan['free_months_1mo']) ?>">
    </div>
    <div class="col-md-3">
        <label for="free_months_3mo" class="form-label">Free Months 3mo</label>
        <input type="number" min="0" step="1" id="free_months_3mo" name="free_months_3mo" class="form-control" required value="<?= h($plan['free_months_3mo']) ?>">
    </div>
    <div class="col-md-3">
        <label for="free_months_6mo" class="form-label">Free Months 6mo</label>
        <input type="number" min="0" step="1" id="free_months_6mo" name="free_months_6mo" class="form-control" required value="<?= h($plan['free_months_6mo']) ?>">
    </div>
    <div class="col-md-3">
        <label for="free_months_12mo" class="form-label">Free Months 12mo</label>
        <input type="number" min="0" step="1" id="free_months_12mo" name="free_months_12mo" class="form-control" required value="<?= h($plan['free_months_12mo']) ?>">
    </div>

    <!-- Allowance and Extra fees for various categories -->
    <?php
    // Define array of categories for allowances and fees
    $categories = [
        'incoming_mail_items' => 'Incoming Mail Items',
        'open_scan_requests' => 'Open & Scan Requests',
        'scanned_pages_per_month' => 'Scanned Pages / Month',
        'scanned_pages_per_request' => 'Scanned Pages / Request',
        'forward_requests' => 'Forward Requests',
        'check_deposit_requests' => 'Check Deposit Requests',
        'recycle_requests' => 'Recycle Requests',
        'shred_requests' => 'Shred Requests',
    ];

    foreach ($categories as $key => $label) {
        ?>
        <div class="col-md-4">
            <label for="<?= $key ?>_allowance" class="form-label"><?= $label ?> Allowance</label>
            <input type="number" min="0" step="1" id="<?= $key ?>_allowance" name="<?= $key ?>_allowance" class="form-control" required value="<?= h($plan[$key . '_allowance'] ?? 0) ?>">
        </div>
        <div class="col-md-4">
            <label for="<?= $key ?>_extra_fee" class="form-label"><?= $label ?> Extra Fee ($)</label>
            <input type="number" min="0" step="0.01" id="<?= $key ?>_extra_fee" name="<?= $key ?>_extra_fee" class="form-control" required value="<?= h($plan[$key . '_extra_fee'] ?? 0.00) ?>">
        </div>
        <div class="col-md-4"></div>
        <?php
    }
    ?>

    <div class="col-md-4">
        <label for="visit_limit_per_month" class="form-label">Visit Limit / Month</label>
        <input type="number" min="0" step="1" id="visit_limit_per_month" name="visit_limit_per_month" class="form-control" required value="<?= h($plan['visit_limit_per_month']) ?>">
    </div>
    <div class="col-md-4">
        <label for="cost_per_visit" class="form-label">Cost / Visit ($)</label>
        <input type="number" min="0" step="0.01" id="cost_per_visit" name="cost_per_visit" class="form-control" required value="<?= h($plan['cost_per_visit']) ?>">
    </div>
    <div class="col-md-4">
        <label for="local_pickup" class="form-label">Local Pickup</label>
        <div class="form-check form-switch">
            <input class="form-check-input" type="checkbox" id="local_pickup" name="local_pickup" value="1" <?= $plan['local_pickup'] ? 'checked' : '' ?>>
            <label class="form-check-label" for="local_pickup">Yes</label>
        </div>
    </div>

    <div class="col-12 mt-3">
        <button type="submit" class="btn btn-primary me-2"><?= $plan_key > 0 ? 'Save Changes' : 'Add Plan' ?></button>
        <button type="button" id="cancelEditPlan" class="btn btn-secondary">Cancel</button>
    </div>
</form>