<?php
// mflowzoo/api/get_active_gifts.php - 行動宇宙：取得上架商品清單
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
require_once 'db.php';

// 防呆裝甲：確保手機版資料庫也有 shipping_fee 欄位
try {
    $pdo->exec("ALTER TABLE gifts ADD COLUMN shipping_fee INT NOT NULL DEFAULT 0");
} catch (Exception $e) {
    // 忽略已存在的錯誤
}

try {
    // 撈取 is_active = 1 (上架中) 的商品
    $stmt = $pdo->query("SELECT id, name, type, cost, shipping_fee, icon FROM gifts WHERE is_active = 1 ORDER BY id DESC");
    $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo json_encode(["status" => "success", "data" => $data]);
} catch (Exception $e) {
    echo json_encode(["status" => "error", "message" => "讀取商品失敗: " . $e->getMessage()]);
}
?>