<?php
require_once 'config.php';

$allowed_categories = ['婚宴酒席', '婚庆布置', '四大金刚', '婚纱礼服', '婚纱照', '婚车车队', '其他杂项'];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $category = $_POST['category'];
    $item_name = trim($_POST['item_name']);
    $estimated_amount = max(0, (float)$_POST['estimated_amount']);
    $actual_amount = max(0, (float)$_POST['actual_amount']);
    $is_essential = isset($_POST['is_essential']) ? 1 : 0;
    
    // 验证分类
    if (!in_array($category, $allowed_categories)) {
        header('Location: budget.php?error=invalid_category');
        exit;
    }
    
    // 验证项目名称
    if (empty($item_name)) {
        header('Location: budget.php?error=empty_name');
        exit;
    }
    
    try {
        $stmt = $pdo->prepare("INSERT INTO budget_items (category, item_name, estimated_amount, actual_amount, is_essential) VALUES (?, ?, ?, ?, ?)");
        $stmt->execute([$category, $item_name, $estimated_amount, $actual_amount, $is_essential]);
        header('Location: budget.php?success=1');
    } catch (PDOException $e) {
        error_log('预算保存失败: ' . $e->getMessage());
        header('Location: budget.php?error=1');
    }
}
?>