<?php
session_start();
include "../config/db.php";

if (!isset($_SESSION['admin_id'])) {
    header("Location: login.php");
    exit();
}

if (isset($_GET['id'])) {
    $user_id = mysqli_real_escape_string($conn, $_GET['id']);
    
    // 1. Reset all outstanding late penalties for this user to zero (Slate Cleaning)
    mysqli_query($conn, "UPDATE orders SET penalty_amount = 0.00 WHERE user_id = '$user_id' AND status IN ('Overdue', 'Return Requested')");
    
    // 2. Unblock the user by setting status to NULL and resetting the manual_unblock flag to 0
    mysqli_query($conn, "UPDATE users SET status = NULL, manual_unblock = 0 WHERE id = '$user_id'");
    
    header("Location: manage-users.php?msg=User account has been unblocked successfully&msg_type=success");
    exit();
}

header("Location: manage-users.php");
exit();
?>
