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

require_once("definitions.php");
?>

<div class="table-responsive">
    <table id="datatable-plans" class="table table-bordered text-nowrap">
        <thead>
            <tr>
                <th>Mailbox Type</th>
                <th>Plan Name</th>
                <th>Pkg Limit</th>
                <th>1 Mo</th>
                <th>12 Mo</th>
                <th>Business Ok</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="19" class="text-center">Invalid store key</td></tr>';
            } else {
                // Join mailbox_plans with mailbox_attributes to get attribute name
                $sql = "SELECT mp.plan_key,
                               ma.name AS attribute_name,
                               mp.name AS plan_name,
                               mp.pkglimit_mo,
                               mp.one_mo, mp.three_mo, mp.six_mo, mp.twelve_mo,
                               mp.webviewable, mp.webrentable, mp.businesses
                        FROM mailbox_plans mp
                        LEFT JOIN mailbox_attributes ma
                            ON mp.mailbox_attribute_key = ma.mailbox_attributes_key
                            AND mp.store_key = ma.store_key
                        WHERE mp.store_key = ?
                        ORDER BY ma.name ASC, mp.name 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()) {
                        $attributeName = htmlspecialchars($row['attribute_name'] ?? 'N/A');
                        $planName = htmlspecialchars($row['plan_name']);

                        // Package limit logic: show "Unlimited" if > 1000
                        $pkgLimitRaw = $row['pkglimit_mo'];
                        if (is_numeric($pkgLimitRaw) && $pkgLimitRaw > 1000) {
                            $pkgLimit = 'Unlimited';
                        } else {
                            $pkgLimit = is_numeric($pkgLimitRaw) ? $pkgLimitRaw : '';
                        }

                       
                        // Format numeric fields or leave blank if null
                        $oneMo = is_numeric($row['one_mo']) ? '$' . $row['one_mo'] : '';
                        $twelveMo = is_numeric($row['twelve_mo']) ? '$' . $row['twelve_mo'] : '';
                        $noBusinesses = $row['businesses'] ? 'Yes' : 'No';

                        $planKey = intval($row['plan_key']);

                        echo "<tr>
                            <td>{$attributeName}</td>
                            <td>{$planName}</td>
                            <td>{$pkgLimit}</td>
                            <td>{$oneMo}</td>
                            <td>{$twelveMo}</td>
                            <td>{$noBusinesses}</td>
                            <td>
                                <a href='javascript:void(0);' class='text-info fs-14 lh-1 editPlanBtn' data-plan-key='{$planKey}'>
                                    <i class='ri-edit-line'></i>
                                </a>
                                <a href='javascript:void(0);' class='text-danger fs-14 lh-1 deletePlanBtn' data-plan-key='{$planKey}'>
                                    <i class='ri-delete-bin-5-line'></i>
                                </a>
                            </td>
                        </tr>";
                    }
                } else {
                    echo '<tr><td colspan="19" class="text-center">No plans found</td></tr>';
                }

                $stmt->close();
            }

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