<?php
session_start();
include('db_connection.php');
include('functions.php'); // Ensure this line is at the top
// Check if the student is logged in
if (!isset($_SESSION['student_id'])) {
    header("Location: login.php");
    exit();
}

$student_id = $_SESSION['student_id'];

// Check if form data is present
if (isset($_POST['request_id']) && isset($_POST['action'])) {
    $request_id = $_POST['request_id'];
    $action = $_POST['action'];

    if ($action == 'accept') {
        // Update request status to accepted
        $sql_accept = "UPDATE student_requests SET status = 'Accepted' WHERE request_id = ? AND requested_id = ?";
        $stmt_accept = $conn->prepare($sql_accept);
        $stmt_accept->bind_param("ii", $request_id, $student_id);
        $stmt_accept->execute();
        $stmt_accept->close();
    } elseif ($action == 'reject') {
        // Update request status to rejected
        $sql_reject = "UPDATE student_requests SET status = 'Rejected' WHERE request_id = ? AND requested_id = ?";
        $stmt_reject = $conn->prepare($sql_reject);
        $stmt_reject->bind_param("ii", $request_id, $student_id);
        $stmt_reject->execute();
        $stmt_reject->close();
    }
}

$conn->close();
header("Location: friend_requests.php");
exit();
?>
