<?php
require 'config.php';

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php"); exit;
}

$total = max(1, min(50, (int)($_POST['total_tables'] ?? 10)));

try {
    $pdo->prepare("INSERT INTO payment_settings (key_name, value) VALUES ('total_tables',?) ON DUPLICATE KEY UPDATE value=?")
        ->execute([$total, $total]);
    $_SESSION['success'] = "Jumlah meja berhasil diperbarui menjadi $total meja.";
} catch (Exception $e) {
    $_SESSION['error'] = "Gagal menyimpan: " . $e->getMessage();
}

header("Location: generate_table_qr.php");