<?php
session_start();
require_once('../../include/dbconfig.php');
require_once('../../include/class.user.php');

// Check if admin is logged in (you should add proper admin authentication)
// if (!isset($_SESSION['admin'])) {
//     header("Location: ../admin_login.php");
//     exit();
// }

$reg_user = new USER();
$message = '';

// Handle unlock request
if (isset($_POST['unlock_account'])) {
    $username = mysqli_real_escape_string($connection, $_POST['username']);
    
    $unlock_stmt = $reg_user->runQuery("UPDATE account SET login_attempts = 0, account_locked = 0, locked_at = NULL WHERE username = :username");
    $unlock_stmt->bindParam(':username', $username);
    
    if ($unlock_stmt->execute()) {
        $message = "<div class='alert alert-success'>Account '$username' has been successfully unlocked!</div>";
    } else {
        $message = "<div class='alert alert-danger'>Failed to unlock account. Please try again.</div>";
    }
}

// Get all locked accounts
$locked_accounts_stmt = $reg_user->runQuery("SELECT id, username, fname, lname, email, login_attempts, locked_at FROM account WHERE account_locked = 1 ORDER BY locked_at DESC");
$locked_accounts_stmt->execute();
$locked_accounts = $locked_accounts_stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Unlock User Accounts - Admin Panel</title>
    <link href="../../assets/bootstrap.css" rel="stylesheet">
    <style>
        body {
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1000px;
            margin: 0 auto;
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        h2 {
            color: #114a43;
            margin-bottom: 30px;
        }
        .table {
            margin-top: 20px;
        }
        .btn-unlock {
            background-color: #28a745;
            color: white;
        }
        .btn-unlock:hover {
            background-color: #218838;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>Unlock User Accounts</h2>
        
        <?php echo $message; ?>
        
        <?php if (count($locked_accounts) > 0): ?>
            <table class="table table-bordered table-striped">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Username</th>
                        <th>Full Name</th>
                        <th>Email</th>
                        <th>Failed Attempts</th>
                        <th>Locked At</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($locked_accounts as $account): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($account['id']); ?></td>
                            <td><?php echo htmlspecialchars($account['username']); ?></td>
                            <td><?php echo htmlspecialchars($account['fname'] . ' ' . $account['lname']); ?></td>
                            <td><?php echo htmlspecialchars($account['email']); ?></td>
                            <td><?php echo htmlspecialchars($account['login_attempts']); ?></td>
                            <td><?php echo htmlspecialchars($account['locked_at']); ?></td>
                            <td>
                                <form method="POST" style="display: inline;">
                                    <input type="hidden" name="username" value="<?php echo htmlspecialchars($account['username']); ?>">
                                    <button type="submit" name="unlock_account" class="btn btn-sm btn-unlock" 
                                            onclick="return confirm('Are you sure you want to unlock this account?');">
                                        Unlock Account
                                    </button>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php else: ?>
            <div class="alert alert-info">
                <strong>No locked accounts found.</strong> All user accounts are currently active.
            </div>
        <?php endif; ?>
        
        <hr>
        
        <h3>Manual Unlock</h3>
        <p>Enter a username to unlock a specific account:</p>
        <form method="POST" class="form-inline">
            <div class="form-group">
                <input type="text" name="username" class="form-control" placeholder="Enter username" required>
            </div>
            <button type="submit" name="unlock_account" class="btn btn-unlock">Unlock Account</button>
        </form>
        
        <br><br>
        <a href="index.php" class="btn btn-secondary">Back to Admin Dashboard</a>
    </div>
</body>
</html>
