<!-- Edit Restaurant Content -->
<div class="max-w-3xl">
    <div class="mb-6">
        <a href="/restpo/superadmin/restaurants" 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 Restaurants
        </a>
    </div>

    <?php if(isset($_GET['error'])): ?>
    <div class="rounded-xl p-4 text-sm font-medium mb-6 animate-pulse" style="background:rgba(239,68,68,0.15); border:1px solid rgba(239,68,68,0.3); color:#fca5a5">
        <i class="fas fa-exclamation-circle mr-2"></i> <?php echo htmlspecialchars($_GET['error']); ?>
    </div>
    <?php endif; ?>

    <div class="rounded-2xl overflow-hidden shadow-2xl" 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 flex justify-between items-center bg-gradient-to-r from-purple-900/20 to-blue-900/20">
            <div>
                <h2 class="text-lg font-semibold text-white">Edit Restaurant Profile</h2>
                <p class="text-slate-400 text-sm mt-0.5">Manage operational settings and active subscription plans for this restaurant.</p>
            </div>
            <span class="px-3 py-1 rounded-full text-xs font-semibold" style="background:<?php echo $restaurant['status']==='active'?'rgba(16,185,129,0.15)':'rgba(239,68,68,0.15)'; ?>; color:<?php echo $restaurant['status']==='active'?'#6ee7b7':'#fca5a5'; ?>">
                Status: <?php echo ucfirst($restaurant['status']); ?>
            </span>
        </div>
        <form action="/restpo/superadmin/restaurants/update" method="POST" class="p-6 space-y-6">
            <input type="hidden" name="id" value="<?php echo $restaurant['id']; ?>">

            <!-- Restaurant Information section -->
            <div>
                <h3 class="text-sm font-semibold text-slate-300 uppercase tracking-wider mb-4"><i class="fas fa-store mr-2 text-purple-400"></i>Restaurant Info</h3>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Restaurant Name *</label>
                        <input type="text" name="name" value="<?php echo htmlspecialchars($restaurant['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">Email Address</label>
                        <input type="email" name="email" value="<?php echo htmlspecialchars($restaurant['email']); ?>" 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">Phone Number</label>
                        <input type="text" name="phone" value="<?php echo htmlspecialchars($restaurant['phone']); ?>" 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">Restaurant Currency</label>
                        <select name="currency" id="restaurant-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)">
                            <?php 
                            $currencies = [
                                '$' => '$ (USD)',
                                '৳' => '৳ (BDT)',
                            ];
                            foreach ($currencies as $symbol => $label):
                                $selected = ($restaurant['currency'] === $symbol) ? 'selected' : '';
                            ?>
                                <option value="<?php echo htmlspecialchars($symbol); ?>" <?php echo $selected; ?>><?php echo htmlspecialchars($label); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
            </div>

            <!-- Restaurant Admin Credentials section -->
            <div class="border-t border-white/5 pt-6">
                <h3 class="text-sm font-semibold text-slate-300 uppercase tracking-wider mb-4"><i class="fas fa-user-shield mr-2 text-indigo-400"></i>Restaurant Admin Account</h3>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Admin Full Name *</label>
                        <input type="text" name="admin_name" value="<?php echo htmlspecialchars($adminUser['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">Admin Email Address *</label>
                        <input type="email" name="admin_email" value="<?php echo htmlspecialchars($adminUser['email'] ?? ''); ?>" 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 class="md:col-span-2">
                        <label class="block text-sm text-slate-400 mb-1 font-medium">Admin New Password</label>
                        <p class="text-xs text-slate-500 mb-2">Leave blank to keep the current password.</p>
                        <input type="password" name="admin_password" placeholder="••••••••" 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)" autocomplete="new-password">
                    </div>
                </div>
            </div>

            <!-- Subscription and Status section -->
            <div class="border-t border-white/5 pt-6">
                <h3 class="text-sm font-semibold text-slate-300 uppercase tracking-wider mb-4"><i class="fas fa-receipt mr-2 text-blue-400"></i>Subscription & Plan</h3>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Assigned Subscription Package</label>
                        <select name="package_id" id="restaurant-package" 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="">No Active Package</option>
                            <?php 
                            $activePackageId = $activeSub ? $activeSub['package_id'] : '';
                            foreach($packages as $pkg): 
                                $selected = ($activePackageId == $pkg['id']) ? 'selected' : '';
                            ?>
                            <option value="<?php echo $pkg['id']; ?>" <?php echo $selected; ?> data-currency="<?php echo htmlspecialchars($pkg['currency'] ?? '$'); ?>">
                                <?php echo htmlspecialchars($pkg['name']); ?> — <?php echo htmlspecialchars($pkg['currency'] ?? '$'); ?><?php echo number_format($pkg['price'], 2); ?> (<?php echo $pkg['duration_days']; ?> days)
                            </option>
                            <?php endforeach; ?>
                        </select>
                        <p class="text-xs text-slate-400 mt-2">
                            <i class="fas fa-info-circle mr-1 text-purple-400"></i> Changing the package will deactivate the current active subscription and start a new active one immediately.
                        </p>
                    </div>
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Account Status</label>
                        <select name="status" 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="active" <?php echo ($restaurant['status'] === 'active') ? 'selected' : ''; ?>>Active</option>
                            <option value="inactive" <?php echo ($restaurant['status'] === 'inactive') ? 'selected' : ''; ?>>Inactive</option>
                        </select>
                        <p class="text-xs text-slate-400 mt-2">
                            <i class="fas fa-exclamation-triangle mr-1 text-amber-500"></i> Inactive accounts block restaurant employees from logging into their dashboards.
                        </p>
                    </div>
                </div>
            </div>

            <!-- Subscription Meta Information (Read-Only Status Box) -->
            <?php if ($activeSub): ?>
            <div class="rounded-xl p-4 border border-white/5 space-y-2.5" style="background:rgba(255,255,255,0.02)">
                <h4 class="text-xs font-semibold text-slate-400 uppercase tracking-wider">Active Subscription Meta</h4>
                <div class="grid grid-cols-2 md:grid-cols-4 gap-4 text-sm">
                    <div>
                        <span class="block text-xs text-slate-500">Started On</span>
                        <span class="font-medium text-slate-300"><?php echo date('M d, Y', strtotime($activeSub['start_date'])); ?></span>
                    </div>
                    <div>
                        <span class="block text-xs text-slate-500">Expires On</span>
                        <span class="font-medium text-slate-300"><?php echo date('M d, Y', strtotime($activeSub['end_date'])); ?></span>
                    </div>
                    <div>
                        <span class="block text-xs text-slate-500">Amount Paid</span>
                        <span class="font-medium text-slate-300"><?php echo $restaurant['currency']; ?><?php echo number_format($activeSub['amount'], 2); ?></span>
                    </div>
                    <div>
                        <span class="block text-xs text-slate-500">Payment Status</span>
                        <span class="px-2 py-0.5 rounded-full text-xs font-semibold inline-block mt-0.5" style="background:rgba(16,185,129,0.15); color:#6ee7b7">
                            <?php echo ucfirst($activeSub['payment_status']); ?>
                        </span>
                    </div>
                </div>
            </div>
            <?php endif; ?>

            <!-- Custom Limits Override Section -->
            <?php $extraFeatures = json_decode($restaurant['extra_features'] ?? '{}', true) ?: []; ?>
            <div class="border-t border-white/5 pt-6">
                <h3 class="text-sm font-semibold text-slate-300 uppercase tracking-wider mb-4"><i class="fas fa-sliders mr-2 text-amber-400"></i>Custom Limits & Features Override (Optional)</h3>
                <p class="text-xs text-slate-500 mb-4">Set specific values here to override the limits of the selected subscription package for this restaurant.</p>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-4">
                    <div>
                        <label class="block text-sm text-slate-400 mb-1.5">Max Floors</label>
                        <input type="number" name="override_max_floors" min="0" placeholder="Default package limit" value="<?php echo isset($extraFeatures['max_floors']) ? htmlspecialchars($extraFeatures['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="override_max_tables" min="0" placeholder="Default package limit" value="<?php echo isset($extraFeatures['max_tables']) ? htmlspecialchars($extraFeatures['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="override_max_cashiers" min="0" placeholder="Default package limit" value="<?php echo isset($extraFeatures['max_cashiers']) ? htmlspecialchars($extraFeatures['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="override_max_waiters" min="0" placeholder="Default package limit" value="<?php echo isset($extraFeatures['max_waiters']) ? htmlspecialchars($extraFeatures['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>
                <div class="flex flex-wrap gap-6 mt-4">
                    <label class="flex items-center gap-3 cursor-pointer group">
                        <input type="checkbox" name="override_qr_ordering" value="1" <?php echo !empty($extraFeatures['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">Force Enable QR Ordering</span>
                    </label>
                    <label class="flex items-center gap-3 cursor-pointer group">
                        <input type="checkbox" name="override_kitchen_display" value="1" <?php echo !empty($extraFeatures['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">Force Enable Kitchen Display</span>
                    </label>
                    <label class="flex items-center gap-3 cursor-pointer group">
                        <input type="checkbox" name="override_is_lifetime" value="1" <?php echo !empty($extraFeatures['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">Force Lifetime Expiry</span>
                    </label>
                </div>
            </div>

            <!-- Form Actions -->
            <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 hover:brightness-110 flex items-center gap-2" style="background:linear-gradient(135deg,#8b5cf6,#3b82f6)">
                    <i class="fas fa-save"></i> Save Changes
                </button>
                <a href="/restpo/superadmin/restaurants" 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>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const currencySelect = document.getElementById('restaurant-currency');
    const packageSelect = document.getElementById('restaurant-package');
    if (currencySelect && packageSelect) {
        const originalOptions = Array.from(packageSelect.options);
        
        function filterPackages() {
            const selectedCurrency = currencySelect.value;
            const currentSelectedValue = packageSelect.value;
            
            // clear options
            packageSelect.innerHTML = '';
            
            originalOptions.forEach(opt => {
                if (opt.value === '' || opt.getAttribute('data-currency') === selectedCurrency) {
                    packageSelect.appendChild(opt.cloneNode(true));
                }
            });
            
            // restore selected value if still valid
            if (Array.from(packageSelect.options).some(o => o.value === currentSelectedValue)) {
                packageSelect.value = currentSelectedValue;
            } else {
                packageSelect.value = '';
            }
        }
        
        currencySelect.addEventListener('change', filterPackages);
        filterPackages();
    }
});
</script>
