<?php
require_once 'db.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = json_decode(file_get_contents("php://input"), true);
    $type = $_GET['type'] ?? '';

    if ($type === 'top') {
        $pdo->exec("DELETE FROM top_banners");
        $stmt = $pdo->prepare("INSERT INTO top_banners (id, title, image) VALUES (?, ?, ?)");
        foreach ($data as $banner) {
            $stmt->execute([$banner['id'], $banner['title'], $banner['image']]);
        }
        echo json_encode(['success' => true]);
    } else if ($type === 'hero') {
        $pdo->exec("DELETE FROM hero_banners");
        $stmt = $pdo->prepare("INSERT INTO hero_banners (id, image, link) VALUES (?, ?, ?)");
        foreach ($data as $banner) {
            $stmt->execute([$banner['id'], $banner['image'], $banner['link'] ?? '']);
        }
        echo json_encode(['success' => true]);
    }
}
?>
