<?php
// 引入数据库连接
require_once 'db_connect.php';

// 初始化变量
$id = $_POST['id'] ?? 0;
$status = $_POST['status'] ?? 0;
// 管理员首页地址，确保与实际管理员首页文件名一致
$adminHome = 'admin_recommendations.php';

// 验证参数
if (empty($id) || !in_array($status, [1, 2])) {
    header("Location: $adminHome?delete_status=error&msg=参数错误");
    exit;
}

// 处理数据库更新
if ($pdo) {
    try {
        // 准备更新语句
        $sql = "UPDATE recommendations 
                SET status = :status, reviewed_at = CURRENT_TIMESTAMP 
                WHERE id = :id";
        
        $stmt = $pdo->prepare($sql);
        $stmt->bindParam(':status', $status, PDO::PARAM_INT);
        $stmt->bindParam(':id', $id, PDO::PARAM_INT);
        
        // 执行更新
        $result = $stmt->execute();
        
        if ($result) {
            $message = $status == 1 ? "推荐已同意" : "推荐已驳回";
            header("Location: $adminHome?delete_status=success&msg=$message");
        } else {
            header("Location: $adminHome?delete_status=error&msg=更新失败，请重试");
        }
    } catch(PDOException $e) {
        // 记录错误日志
        $logMsg = date('[Y-m-d H:i:s] ') . "状态更新错误: " . $e->getMessage() . "\n";
        file_put_contents('db_error.log', $logMsg, FILE_APPEND);
        
        header("Location: $adminHome?delete_status=error&msg=数据库错误，请稍后再试");
    }
} else {
    header("Location: $adminHome?delete_status=error&msg=数据库连接失败");
}

exit;
?>