<?php
session_start();
if (!isset($_SESSION['admin'])) {
    header("Location: index.php");
    exit();
}

$settings_file = 'data/button_settings.json';

// Get the submitted settings
$phonepe_visible = isset($_POST['phonepe_visible']) ? true : false;
$paytm_visible = isset($_POST['paytm_visible']) ? true : false;

// Create the settings array
$settings = [
    'phonepe_visible' => $phonepe_visible,
    'paytm_visible' => $paytm_visible
];

// Ensure the data directory exists
if (!file_exists('data')) {
    mkdir('data', 0755, true);
}

// Save the settings to the JSON file
if (file_put_contents($settings_file, json_encode($settings))) {
    $_SESSION['success'] = "Button visibility settings updated successfully!";
} else {
    $_SESSION['error'] = "Failed to update button visibility settings.";
}

// Redirect back to the dashboard
header("Location: dashboard.php");
exit();
?>