<?php
// app/Controllers/PackageController.php

class PackageController {
    private function requireSuperAdmin() {
        if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'superadmin') {
            header('Location: /restpo/login'); exit;
        }
    }

    public function index() {
        $this->requireSuperAdmin();
        $db = getDB();
        $pageTitle = 'Subscription Packages';
        $packages = $db->query('SELECT * FROM packages ORDER BY id DESC')->fetchAll();

        ob_start(); ?>
<div class="space-y-6">
    <div class="flex justify-between items-center">
        <div>
            <h2 class="text-xl font-bold text-white">Subscription Packages</h2>
            <p class="text-slate-400 text-sm mt-1">Define plans and feature limits for restaurant owners</p>
        </div>
    </div>

    <!-- Create Package Form -->
    <div class="rounded-2xl p-6" style="background:rgba(255,255,255,0.04); border:1px solid rgba(255,255,255,0.08)">
        <h3 class="text-base font-semibold text-white mb-4"><i class="fas fa-cube mr-2 text-purple-400"></i>Create Package</h3>
        <form action="/restpo/superadmin/packages/store" method="POST" class="space-y-5">
            <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
                <div>
                    <label class="block text-sm text-slate-400 mb-1.5">Name</label>
                    <input type="text" name="name" required class="w-full px-4 py-2.5 rounded-xl text-white text-sm outline-none focus:ring-2 focus:ring-purple-500" style="background:rgba(255,255,255,0.07); border:1px solid rgba(255,255,255,0.1)">
                </div>
                <div>
                    <label class="block text-sm text-slate-400 mb-1.5">Price</label>
                    <input type="number" step="0.01" name="price" required class="w-full px-4 py-2.5 rounded-xl text-white text-sm outline-none focus:ring-2 focus:ring-purple-500" style="background:rgba(255,255,255,0.07); border:1px solid rgba(255,255,255,0.1)">
                </div>
                <div>
                    <label class="block text-sm text-slate-400 mb-1.5">Currency</label>
                    <select name="currency" class="w-full px-4 py-2.5 rounded-xl text-white text-sm outline-none focus:ring-2 focus:ring-purple-500" style="background:rgba(30,27,75,1); border:1px solid rgba(255,255,255,0.1)">
                        <option value="$" selected>USD ($)</option>
                        <option value="৳">BDT (৳)</option>
                    </select>
                </div>
                <div>
                    <label class="block text-sm text-slate-400 mb-1.5">Duration (days)</label>
                    <input type="number" name="duration_days" value="30" required class="w-full px-4 py-2.5 rounded-xl text-white text-sm outline-none focus:ring-2 focus:ring-purple-500" style="background:rgba(255,255,255,0.07); border:1px solid rgba(255,255,255,0.1)">
                </div>
            </div>

            <div class="border-t border-white/5 pt-4">
                <h4 class="text-sm font-semibold text-slate-300 uppercase tracking-wider mb-3">Feature Limits</h4>
                <div class="grid grid-cols-1 md:grid-cols-5 gap-4">
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Max Floors</label>
                        <input type="number" name="max_floors" min="0" placeholder="Leave empty for unlimited" class="w-full px-4 py-2.5 rounded-xl text-white text-sm outline-none focus:ring-2 focus:ring-purple-500" style="background:rgba(255,255,255,0.07); border:1px solid rgba(255,255,255,0.1)">
                    </div>
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Max Tables</label>
                        <input type="number" name="max_tables" min="0" placeholder="Leave empty for unlimited" class="w-full px-4 py-2.5 rounded-xl text-white text-sm outline-none focus:ring-2 focus:ring-purple-500" style="background:rgba(255,255,255,0.07); border:1px solid rgba(255,255,255,0.1)">
                    </div>
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Max Cashiers</label>
                        <input type="number" name="max_cashiers" min="0" placeholder="Leave empty for unlimited" class="w-full px-4 py-2.5 rounded-xl text-white text-sm outline-none focus:ring-2 focus:ring-purple-500" style="background:rgba(255,255,255,0.07); border:1px solid rgba(255,255,255,0.1)">
                    </div>
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Max Waiters</label>
                        <input type="number" name="max_waiters" min="0" placeholder="Leave empty for unlimited" class="w-full px-4 py-2.5 rounded-xl text-white text-sm outline-none focus:ring-2 focus:ring-purple-500" style="background:rgba(255,255,255,0.07); border:1px solid rgba(255,255,255,0.1)">
                    </div>
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Max Others</label>
                        <input type="number" name="max_others" min="0" placeholder="Leave empty for unlimited" class="w-full px-4 py-2.5 rounded-xl text-white text-sm outline-none focus:ring-2 focus:ring-purple-500" style="background:rgba(255,255,255,0.07); border:1px solid rgba(255,255,255,0.1)">
                    </div>
                </div>
            </div>

            <div class="border-t border-white/5 pt-4">
                <h4 class="text-sm font-semibold text-slate-300 uppercase tracking-wider mb-3">Extra Features</h4>
                <div class="flex flex-wrap gap-6">
                    <label class="flex items-center gap-3 cursor-pointer group">
                        <input type="checkbox" name="qr_ordering" value="1" class="w-5 h-5 rounded-md text-purple-500 bg-white/5 border-white/10 focus:ring-purple-500 focus:ring-offset-0">
                        <span class="text-sm text-slate-300 group-hover:text-white transition">QR Ordering</span>
                    </label>
                    <label class="flex items-center gap-3 cursor-pointer group">
                        <input type="checkbox" name="kitchen_display" value="1" class="w-5 h-5 rounded-md text-purple-500 bg-white/5 border-white/10 focus:ring-purple-500 focus:ring-offset-0">
                        <span class="text-sm text-slate-300 group-hover:text-white transition">Kitchen Display</span>
                    </label>
                    <label class="flex items-center gap-3 cursor-pointer group">
                        <input type="checkbox" name="is_lifetime" value="1" class="w-5 h-5 rounded-md text-purple-500 bg-white/5 border-white/10 focus:ring-purple-500 focus:ring-offset-0">
                        <span class="text-sm text-slate-300 group-hover:text-white transition">Is Lifetime Plan</span>
                    </label>
                    <label class="flex items-center gap-3 cursor-pointer group">
                        <input type="checkbox" name="inventory" value="1" class="w-5 h-5 rounded-md text-purple-500 bg-white/5 border-white/10 focus:ring-purple-500 focus:ring-offset-0">
                        <span class="text-sm text-slate-300 group-hover:text-white transition">Inventory</span>
                    </label>
                    <label class="flex items-center gap-3 cursor-pointer group">
                        <input type="checkbox" name="staff_management" value="1" class="w-5 h-5 rounded-md text-purple-500 bg-white/5 border-white/10 focus:ring-purple-500 focus:ring-offset-0">
                        <span class="text-sm text-slate-300 group-hover:text-white transition">Staff Management</span>
                    </label>
                </div>
            </div>

            <div class="border-t border-white/5 pt-4">
                <button type="submit" class="px-8 py-3 rounded-xl font-semibold text-white transition shadow-lg" style="background:linear-gradient(135deg,#8b5cf6,#3b82f6)">
                    <i class="fas fa-plus-circle mr-2"></i>Save Package
                </button>
            </div>
        </form>
    </div>

    <!-- Package List - Cards -->
    <div class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-5">
        <?php foreach($packages as $pkg):
            $features = json_decode($pkg['features'] ?? '{}', true) ?: [];
            $nameLower = strtolower($pkg['name']);
            // Determine accent color based on package name
            if (str_contains($nameLower, 'premium')) {
                $accent = '#3b82f6'; $accentBg = 'rgba(59,130,246,0.15)'; $accentText = '#93c5fd';
            } elseif (str_contains($nameLower, 'advanced') || str_contains($nameLower, 'pro')) {
                $accent = '#10b981'; $accentBg = 'rgba(16,185,129,0.15)'; $accentText = '#6ee7b7';
            } elseif (str_contains($nameLower, 'trial') || str_contains($nameLower, 'free')) {
                $accent = '#f59e0b'; $accentBg = 'rgba(245,158,11,0.15)'; $accentText = '#fcd34d';
            } else {
                $accent = '#8b5cf6'; $accentBg = 'rgba(139,92,246,0.15)'; $accentText = '#c4b5fd';
            }
        ?>
        <div class="rounded-2xl overflow-hidden flex flex-col" style="background:rgba(255,255,255,0.04); border:1px solid rgba(255,255,255,0.08)">
            <!-- Header -->
            <div class="px-5 pt-5 pb-3">
                <div class="flex items-center justify-between mb-2">
                    <span class="px-3 py-1 rounded-full text-xs font-bold uppercase tracking-wide" style="background:<?php echo $accentBg; ?>; color:<?php echo $accentText; ?>"><?php echo htmlspecialchars($pkg['name']); ?></span>
                    <span class="px-2 py-0.5 rounded-full text-xs font-semibold" style="background:<?php echo $pkg['status'] === 'active' ? 'rgba(16,185,129,0.15)' : 'rgba(239,68,68,0.15)'; ?>; color:<?php echo $pkg['status'] === 'active' ? '#6ee7b7' : '#fca5a5'; ?>"><?php echo $pkg['status']; ?></span>
                </div>
                <div class="mt-3">
                    <span class="text-2xl font-bold text-white"><?php echo htmlspecialchars($pkg['currency'] ?? '$'); ?><?php echo number_format($pkg['price'],2); ?></span>
                    <span class="text-slate-500 text-sm ml-1">/ <?php echo !empty($features['is_lifetime']) ? 'Lifetime' : ($pkg['duration_days'] . ' days'); ?></span>
                </div>
            </div>

            <!-- Features -->
            <div class="px-5 py-4 flex-1 border-t border-white/5">
                <ul class="space-y-2.5 text-sm">
                    <?php
                    $featureItems = [
                        ['key' => 'max_floors',  'icon' => 'fas fa-building',     'label' => 'Floors',   'suffix' => 'floors'],
                        ['key' => 'max_tables',  'icon' => 'fas fa-chair',        'label' => 'Tables',   'suffix' => 'tables'],
                        ['key' => 'max_cashiers','icon' => 'fas fa-cash-register','label' => 'Cashiers', 'suffix' => ''],
                        ['key' => 'max_waiters', 'icon' => 'fas fa-user-tie',     'label' => 'Waiters',  'suffix' => ''],
                        ['key' => 'max_others',  'icon' => 'fas fa-users-cog',    'label' => 'Others',   'suffix' => ''],
                    ];
                    foreach ($featureItems as $fi):
                        $val = $features[$fi['key']] ?? null;
                        $display = $val !== null ? $val . ($fi['suffix'] ? ' ' . $fi['suffix'] : '') : 'Unlimited';
                    ?>
                    <li class="flex items-center gap-2.5">
                        <i class="<?php echo $fi['icon']; ?> w-4 text-center" style="color:<?php echo $accentText; ?>"></i>
                        <span class="text-slate-400"><?php echo $fi['label']; ?>:</span>
                        <span class="text-white font-medium ml-auto"><?php echo $display; ?></span>
                    </li>
                    <?php endforeach; ?>

                    <?php
                    $toggleItems = [
                        ['key' => 'qr_ordering',     'icon' => 'fas fa-qrcode',   'label' => 'QR Ordering'],
                        ['key' => 'kitchen_display',  'icon' => 'fas fa-tv',       'label' => 'Kitchen Display'],
                        ['key' => 'inventory',        'icon' => 'fas fa-boxes',    'label' => 'Inventory'],
                        ['key' => 'staff_management', 'icon' => 'fas fa-users-cog','label' => 'Staff Management'],
                    ];
                    foreach ($toggleItems as $ti):
                        $enabled = !empty($features[$ti['key']]);
                    ?>
                    <li class="flex items-center gap-2.5">
                        <i class="<?php echo $ti['icon']; ?> w-4 text-center" style="color:<?php echo $accentText; ?>"></i>
                        <span class="text-slate-400"><?php echo $ti['label']; ?></span>
                        <?php if ($enabled): ?>
                            <i class="fas fa-check ml-auto text-emerald-400"></i>
                        <?php else: ?>
                            <i class="fas fa-times ml-auto text-red-400/60"></i>
                        <?php endif; ?>
                    </li>
                    <?php endforeach; ?>
                </ul>
            </div>
            <!-- Action Footer -->
            <div class="px-5 pb-5 pt-2 mt-auto">
                <a href="/restpo/superadmin/packages/edit?id=<?php echo $pkg['id']; ?>" class="w-full flex items-center justify-center gap-2 py-2.5 px-4 rounded-xl text-xs font-semibold text-white transition-all duration-200 hover:brightness-110 shadow-md" style="background:linear-gradient(135deg, <?php echo $accent; ?>, rgba(139,92,246,0.25)); border:1px solid rgba(255,255,255,0.08)">
                    <i class="fas fa-edit text-xs"></i> Edit Plan
                </a>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
</div>
<?php
        $content = ob_get_clean();
        require 'app/Views/layouts/superadmin.php';
    }

    public function store() {
        $this->requireSuperAdmin();
        $db = getDB();

        $nameLower = strtolower($_POST['name'] ?? '');
        $isTrial = str_contains($nameLower, 'trial') || str_contains($nameLower, 'free') || isset($_POST['is_trial']);
        $isLifetime = isset($_POST['is_lifetime']);

        $features = json_encode([
            'max_floors' => $_POST['max_floors'] !== '' ? (int)$_POST['max_floors'] : null,
            'max_tables' => $_POST['max_tables'] !== '' ? (int)$_POST['max_tables'] : null,
            'max_cashiers' => $_POST['max_cashiers'] !== '' ? (int)$_POST['max_cashiers'] : null,
            'max_waiters' => $_POST['max_waiters'] !== '' ? (int)$_POST['max_waiters'] : null,
            'max_others' => $_POST['max_others'] !== '' ? (int)$_POST['max_others'] : null,
            'qr_ordering' => isset($_POST['qr_ordering']),
            'kitchen_display' => isset($_POST['kitchen_display']),
            'is_trial' => $isTrial,
            'is_lifetime' => $isLifetime,
            'inventory' => isset($_POST['inventory']),
            'staff_management' => isset($_POST['staff_management']),
        ]);

        $currency = $_POST['currency'] ?? '$';
        $db->prepare('INSERT INTO packages (name, price, duration_days, features, currency, status) VALUES (?, ?, ?, ?, ?, "active")')
           ->execute([$_POST['name'], $_POST['price'], $_POST['duration_days'], $features, $currency]);
        header('Location: /restpo/superadmin/packages?success=1'); exit;
    }

    public function edit() {
        $this->requireSuperAdmin();
        $db = getDB();
        $id = $_GET['id'] ?? null;
        if (!$id) { header('Location: /restpo/superadmin/packages'); exit; }

        $pkg = $db->prepare('SELECT * FROM packages WHERE id = ?');
        $pkg->execute([$id]);
        $pkg = $pkg->fetch();
        if (!$pkg) { header('Location: /restpo/superadmin/packages?error=Package+not+found'); exit; }

        $features = json_decode($pkg['features'] ?? '{}', true) ?: [];
        $pageTitle = 'Edit Package';

        ob_start(); ?>
<div class="max-w-3xl">
    <div class="mb-6">
        <a href="/restpo/superadmin/packages" class="text-slate-400 hover:text-white text-sm flex items-center gap-2">
            <i class="fas fa-arrow-left text-xs"></i> Back to Packages
        </a>
    </div>

    <div class="rounded-2xl overflow-hidden" style="background:rgba(255,255,255,0.04); border:1px solid rgba(255,255,255,0.08)">
        <div class="px-6 py-4 border-b border-white/5">
            <h2 class="text-lg font-semibold text-white">Edit Package Details</h2>
            <p class="text-slate-400 text-sm mt-0.5">Modify the price, duration, and feature access parameters for this plan.</p>
        </div>
        <form action="/restpo/superadmin/packages/update" method="POST" class="p-6 space-y-6">
            <input type="hidden" name="id" value="<?php echo $pkg['id']; ?>">
            
            <div>
                <h3 class="text-sm font-semibold text-slate-300 uppercase tracking-wider mb-4">Core Information</h3>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Package Name</label>
                        <input type="text" name="name" value="<?php echo htmlspecialchars($pkg['name']); ?>" required class="w-full px-4 py-2.5 rounded-xl text-white text-sm outline-none focus:ring-2 focus:ring-purple-500 transition" style="background:rgba(255,255,255,0.07); border:1px solid rgba(255,255,255,0.1)">
                    </div>
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Price</label>
                        <input type="number" step="0.01" name="price" value="<?php echo htmlspecialchars($pkg['price']); ?>" required class="w-full px-4 py-2.5 rounded-xl text-white text-sm outline-none focus:ring-2 focus:ring-purple-500 transition" style="background:rgba(255,255,255,0.07); border:1px solid rgba(255,255,255,0.1)">
                    </div>
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Currency</label>
                        <select name="currency" class="w-full px-4 py-2.5 rounded-xl text-white text-sm outline-none focus:ring-2 focus:ring-purple-500 transition" style="background:rgba(30,27,75,1); border:1px solid rgba(255,255,255,0.1)">
                            <option value="$" <?php echo ($pkg['currency'] ?? '$') === '$' ? 'selected' : ''; ?>>USD ($)</option>
                            <option value="৳" <?php echo ($pkg['currency'] ?? '$') === '৳' ? 'selected' : ''; ?>>BDT (৳)</option>
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Duration (days)</label>
                        <input type="number" name="duration_days" value="<?php echo htmlspecialchars($pkg['duration_days']); ?>" required class="w-full px-4 py-2.5 rounded-xl text-white text-sm outline-none focus:ring-2 focus:ring-purple-500 transition" style="background:rgba(255,255,255,0.07); border:1px solid rgba(255,255,255,0.1)">
                    </div>
                </div>
            </div>

            <div class="border-t border-white/5 pt-6">
                <h4 class="text-sm font-semibold text-slate-300 uppercase tracking-wider mb-4">Feature Quota Limits</h4>
                <div class="grid grid-cols-1 md:grid-cols-5 gap-4">
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Max Floors</label>
                        <input type="number" name="max_floors" min="0" placeholder="Leave empty for unlimited" value="<?php echo htmlspecialchars($features['max_floors'] ?? ''); ?>" class="w-full px-4 py-2.5 rounded-xl text-white text-sm outline-none focus:ring-2 focus:ring-purple-500 transition" style="background:rgba(255,255,255,0.07); border:1px solid rgba(255,255,255,0.1)">
                    </div>
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Max Tables</label>
                        <input type="number" name="max_tables" min="0" placeholder="Leave empty for unlimited" value="<?php echo htmlspecialchars($features['max_tables'] ?? ''); ?>" class="w-full px-4 py-2.5 rounded-xl text-white text-sm outline-none focus:ring-2 focus:ring-purple-500 transition" style="background:rgba(255,255,255,0.07); border:1px solid rgba(255,255,255,0.1)">
                    </div>
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Max Cashiers</label>
                        <input type="number" name="max_cashiers" min="0" placeholder="Leave empty for unlimited" value="<?php echo htmlspecialchars($features['max_cashiers'] ?? ''); ?>" class="w-full px-4 py-2.5 rounded-xl text-white text-sm outline-none focus:ring-2 focus:ring-purple-500 transition" style="background:rgba(255,255,255,0.07); border:1px solid rgba(255,255,255,0.1)">
                    </div>
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Max Waiters</label>
                        <input type="number" name="max_waiters" min="0" placeholder="Leave empty for unlimited" value="<?php echo htmlspecialchars($features['max_waiters'] ?? ''); ?>" class="w-full px-4 py-2.5 rounded-xl text-white text-sm outline-none focus:ring-2 focus:ring-purple-500 transition" style="background:rgba(255,255,255,0.07); border:1px solid rgba(255,255,255,0.1)">
                    </div>
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Max Others</label>
                        <input type="number" name="max_others" min="0" placeholder="Leave empty for unlimited" value="<?php echo htmlspecialchars($features['max_others'] ?? ''); ?>" class="w-full px-4 py-2.5 rounded-xl text-white text-sm outline-none focus:ring-2 focus:ring-purple-500 transition" style="background:rgba(255,255,255,0.07); border:1px solid rgba(255,255,255,0.1)">
                    </div>
                </div>
            </div>

            <div class="border-t border-white/5 pt-6">
                <h4 class="text-sm font-semibold text-slate-300 uppercase tracking-wider mb-4">Module Access</h4>
                <div class="flex flex-wrap gap-6">
                    <label class="flex items-center gap-3 cursor-pointer group">
                        <input type="checkbox" name="qr_ordering" value="1" <?php echo !empty($features['qr_ordering']) ? 'checked' : ''; ?> class="w-5 h-5 rounded-md text-purple-500 bg-white/5 border-white/10 focus:ring-purple-500 focus:ring-offset-0">
                        <span class="text-sm text-slate-300 group-hover:text-white transition">QR Ordering</span>
                    </label>
                    <label class="flex items-center gap-3 cursor-pointer group">
                        <input type="checkbox" name="kitchen_display" value="1" <?php echo !empty($features['kitchen_display']) ? 'checked' : ''; ?> class="w-5 h-5 rounded-md text-purple-500 bg-white/5 border-white/10 focus:ring-purple-500 focus:ring-offset-0">
                        <span class="text-sm text-slate-300 group-hover:text-white transition">Kitchen Display</span>
                    </label>
                    <label class="flex items-center gap-3 cursor-pointer group">
                        <input type="checkbox" name="is_trial" value="1" <?php echo !empty($features['is_trial']) ? 'checked' : ''; ?> class="w-5 h-5 rounded-md text-amber-500 bg-white/5 border-white/10 focus:ring-amber-500 focus:ring-offset-0">
                        <span class="text-sm text-slate-300 group-hover:text-white transition">Is Trial Plan</span>
                    </label>
                    <label class="flex items-center gap-3 cursor-pointer group">
                        <input type="checkbox" name="is_lifetime" value="1" <?php echo !empty($features['is_lifetime']) ? 'checked' : ''; ?> class="w-5 h-5 rounded-md text-purple-500 bg-white/5 border-white/10 focus:ring-purple-500 focus:ring-offset-0">
                        <span class="text-sm text-slate-300 group-hover:text-white transition">Is Lifetime Plan</span>
                    </label>
                    <label class="flex items-center gap-3 cursor-pointer group">
                        <input type="checkbox" name="inventory" value="1" <?php echo !empty($features['inventory']) ? 'checked' : ''; ?> class="w-5 h-5 rounded-md text-purple-500 bg-white/5 border-white/10 focus:ring-purple-500 focus:ring-offset-0">
                        <span class="text-sm text-slate-300 group-hover:text-white transition">Inventory</span>
                    </label>
                    <label class="flex items-center gap-3 cursor-pointer group">
                        <input type="checkbox" name="staff_management" value="1" <?php echo !empty($features['staff_management']) ? 'checked' : ''; ?> class="w-5 h-5 rounded-md text-purple-500 bg-white/5 border-white/10 focus:ring-purple-500 focus:ring-offset-0">
                        <span class="text-sm text-slate-300 group-hover:text-white transition">Staff Management</span>
                    </label>
                </div>
            </div>

            <div class="border-t border-white/5 pt-6 flex gap-3">
                <button type="submit" class="px-8 py-3 rounded-xl font-semibold text-white transition shadow-lg" style="background:linear-gradient(135deg,#8b5cf6,#3b82f6)">
                    <i class="fas fa-save mr-2"></i> Save Changes
                </button>
                <a href="/restpo/superadmin/packages" class="px-6 py-3 rounded-xl font-semibold text-slate-400 hover:text-white hover:bg-white/5 border border-white/10 text-center transition flex items-center justify-center">
                    Cancel
                </a>
            </div>
        </form>
    </div>
</div>
<?php
        $content = ob_get_clean();
        require 'app/Views/layouts/superadmin.php';
    }

    public function update() {
        $this->requireSuperAdmin();
        $db = getDB();
        $id = $_POST['id'] ?? null;
        if (!$id) { header('Location: /restpo/superadmin/packages'); exit; }

        $nameLower = strtolower($_POST['name'] ?? '');
        $isTrial = str_contains($nameLower, 'trial') || str_contains($nameLower, 'free') || isset($_POST['is_trial']);
        $isLifetime = isset($_POST['is_lifetime']);

        $features = json_encode([
            'max_floors' => $_POST['max_floors'] !== '' ? (int)$_POST['max_floors'] : null,
            'max_tables' => $_POST['max_tables'] !== '' ? (int)$_POST['max_tables'] : null,
            'max_cashiers' => $_POST['max_cashiers'] !== '' ? (int)$_POST['max_cashiers'] : null,
            'max_waiters' => $_POST['max_waiters'] !== '' ? (int)$_POST['max_waiters'] : null,
            'max_others' => $_POST['max_others'] !== '' ? (int)$_POST['max_others'] : null,
            'qr_ordering' => isset($_POST['qr_ordering']),
            'kitchen_display' => isset($_POST['kitchen_display']),
            'is_trial' => $isTrial,
            'is_lifetime' => $isLifetime,
            'inventory' => isset($_POST['inventory']),
            'staff_management' => isset($_POST['staff_management']),
        ]);

        $currency = $_POST['currency'] ?? '$';
        $db->prepare('UPDATE packages SET name=?, price=?, duration_days=?, features=?, currency=? WHERE id=?')
           ->execute([$_POST['name'], $_POST['price'], $_POST['duration_days'], $features, $currency, $id]);

        header('Location: /restpo/superadmin/packages?msg=Package+updated+successfully'); exit;
    }
}
