<?php
use System\Response;
use System\Check;
use System\Session;

$users = $dbo->query("SELECT id, username, email, status, bypass_protect FROM users ORDER BY id DESC")->results();

if (Check::type('post')) {
    if (isset($_POST['toggle'])) {
        $user_id  = (int) $_POST['toggle']['id'];
        $newValue = $_POST['toggle']['value'] === 'yes' ? 'yes' : 'no';
        if ($user_id) {
            $dbo->query("UPDATE users SET bypass_protect = ? WHERE id = ?", [$newValue, $user_id]);
            Session::flash('success', 'Protection updated successfully');
        }
    }
    Response::redirect('admin/protect');
}

$view = new System\View('admin/protect');
include(PATH . '/module/common.php');
$view->users = $users;
$view->title = 'Bypass Protection - Linkshub';
$view->meta_desc = '';
$view->canonicalUrl = System\Uri::full('/admin/protect');
$view->noIndex = true;
$view->pageType = 'admin/protect';
$data = $view->render();
echo $data;
