<?php
require_once '../../config/config.php';
require_once '../../includes/functions.php';
startSecureSession();
requireAdmin();

header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $config = $_POST['config'] ?? '';
    
    if (empty($config)) {
        echo json_encode(['success' => false, 'error' => 'No configuration received.']);
        exit;
    }

    // Save to settings table
    $success = Database::execute(
        "INSERT INTO settings (setting_key, setting_value, setting_group) 
         VALUES ('invoice_template_config', ?, 'billing') 
         ON DUPLICATE KEY UPDATE setting_value = ?",
        [$config, $config]
    );

    echo json_encode(['success' => true, 'message' => 'Template layout saved successfully.']);
    exit;
}

echo json_encode(['success' => false, 'error' => 'Invalid request.']);
