<?php
/**
 * Admin Users Management
 */
require_once 'includes/header.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    
    if ($action === 'add') {
        $exists = dbFetch("SELECT id FROM admin_users WHERE username = ? OR email = ?", [$_POST['username'], $_POST['email']]);
        if ($exists) {
            echo '<div class="alert alert-danger">Username or email already exists!</div>';
        } else {
            $data = [
                'username' => trim($_POST['username']),
                'email' => trim($_POST['email']),
                'password' => password_hash($_POST['password'], PASSWORD_BCRYPT, ['cost' => 12]),
                'full_name' => trim($_POST['full_name']),
                'role' => $_POST['role'],
                'status' => 'active'
            ];
            dbInsert('admin_users', $data);
            echo '<div class="alert alert-success"><i class="fas fa-check"></i> Admin created!</div>';
        }
    } elseif ($action === 'delete' && $_POST['admin_id'] != $_SESSION['user_id']) {
        dbDelete('admin_users', 'id = ?', [$_POST['admin_id']]);
        echo '<div class="alert alert-success"><i class="fas fa-check"></i> Admin deleted!</div>';
    } elseif ($action === 'toggle') {
        $admin = dbFetch("SELECT status FROM admin_users WHERE id = ?", [$_POST['admin_id']]);
        $newStatus = $admin['status'] === 'active' ? 'inactive' : 'active';
        dbUpdate('admin_users', ['status' => $newStatus], 'id = ?', [$_POST['admin_id']]);
    }
}

$admins = dbFetchAll("SELECT * FROM admin_users ORDER BY created_at DESC");
?>

<div class="page-header">
    <h1 class="page-title">Admin Users</h1>
    <button class="btn btn-primary" onclick="document.getElementById('addModal').classList.add('active')">
        <i class="fas fa-plus"></i> Add Admin
    </button>
</div>

<div class="card">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table">
                <thead>
                    <tr>
                        <th>Username</th>
                        <th>Email</th>
                        <th>Name</th>
                        <th>Role</th>
                        <th>Last Login</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($admins as $admin): ?>
                    <tr>
                        <td class="font-semibold"><?= htmlspecialchars($admin['username']) ?></td>
                        <td><?= htmlspecialchars($admin['email']) ?></td>
                        <td><?= htmlspecialchars($admin['full_name'] ?? '-') ?></td>
                        <td><span class="badge badge-primary"><?= ucfirst($admin['role']) ?></span></td>
                        <td><?= $admin['last_login'] ? date('d M Y H:i', strtotime($admin['last_login'])) : 'Never' ?></td>
                        <td><span class="badge badge-<?= $admin['status'] === 'active' ? 'success' : 'danger' ?>"><?= ucfirst($admin['status']) ?></span></td>
                        <td>
                            <?php if ($admin['id'] != $_SESSION['user_id']): ?>
                            <form method="POST" style="display:inline">
                                <input type="hidden" name="action" value="toggle">
                                <input type="hidden" name="admin_id" value="<?= $admin['id'] ?>">
                                <button class="btn btn-sm btn-secondary"><i class="fas fa-power-off"></i></button>
                            </form>
                            <form method="POST" style="display:inline" onsubmit="return confirm('Delete this admin?')">
                                <input type="hidden" name="action" value="delete">
                                <input type="hidden" name="admin_id" value="<?= $admin['id'] ?>">
                                <button class="btn btn-sm btn-danger"><i class="fas fa-trash"></i></button>
                            </form>
                            <?php else: ?>
                            <span class="text-muted">Current User</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="modal" id="addModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4>Add Admin User</h4>
                <button class="modal-close" onclick="document.getElementById('addModal').classList.remove('active')">&times;</button>
            </div>
            <form method="POST">
                <input type="hidden" name="action" value="add">
                <div class="modal-body">
                    <div class="form-group">
                        <label class="form-label">Username</label>
                        <input type="text" name="username" class="form-control" required>
                    </div>
                    <div class="form-group">
                        <label class="form-label">Email</label>
                        <input type="email" name="email" class="form-control" required>
                    </div>
                    <div class="form-group">
                        <label class="form-label">Password</label>
                        <input type="password" name="password" class="form-control" required minlength="6">
                    </div>
                    <div class="form-group">
                        <label class="form-label">Full Name</label>
                        <input type="text" name="full_name" class="form-control">
                    </div>
                    <div class="form-group">
                        <label class="form-label">Role</label>
                        <select name="role" class="form-control">
                            <option value="admin">Admin</option>
                            <option value="super_admin">Super Admin</option>
                            <option value="support">Support</option>
                        </select>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Create Admin</button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php require_once 'includes/footer.php'; ?>
