<?php
/**
 * Find Hidden Classes
 * This script finds where hidden and d-none classes are being applied
 */

// Set up environment
$_GET['_route'] = 'banks/list';
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['HTTP_HOST'] = 'newdesign1.netreachsolutions.co.ke';
$_SERVER['REQUEST_URI'] = '/index.php?_route=banks/list';
$_SERVER['HTTPS'] = 'on';
$_SERVER['SERVER_PORT'] = '443';

try {
    // Include necessary files
    require_once 'system/vendor/autoload.php';
    require_once 'init.php';
    
    // Set up global variables
    $routes = explode('/', 'banks/list');
    global $ui;
    
    // Initialize UI
    $ui = new Smarty();
    $ui->setTemplateDir([
        'custom' => File::pathFixer($UI_PATH . '/ui_custom/'),
        'default' => File::pathFixer($UI_PATH . '/ui/')
    ]);
    $ui->assign('_url', APP_URL . '/index.php?_route=');
    $ui->setCompileDir(File::pathFixer($UI_PATH . '/compiled/'));
    $ui->setConfigDir(File::pathFixer($UI_PATH . '/conf/'));
    $ui->setCacheDir(File::pathFixer($UI_PATH . '/cache/'));
    $ui->assign('app_url', APP_URL);
    $ui->assign('_domain', str_replace('www.', '', parse_url(APP_URL, PHP_URL_HOST)));
    $ui->assign('_path', __DIR__);
    $ui->assign('_c', $config);
    $ui->assign('_system_menu', 'dashboard');
    
    // Mock admin authentication
    $_SESSION['aid'] = 1;
    $_SESSION['username'] = 'admin';
    $_SESSION['user_type'] = 'SuperAdmin';
    
    // Mock admin data
    $admin = [
        'id' => 1,
        'username' => 'admin',
        'user_type' => 'SuperAdmin'
    ];
    
    $ui->assign('_title', 'Bank Management');
    $ui->assign('_system_menu', 'services');
    $ui->assign('_admin', $admin);
    
    // Get banks data
    $query = ORM::for_table('tbl_banks');
    $query->order_by_desc('is_default')->order_by_asc('name');
    $banks = Paginator::findMany($query, []);
    
    $ui->assign('banks', $banks);
    $ui->assign('search', '');
    $ui->assign('filter_stk', '');
    $ui->assign('filter_active', '');
    
    // Render template
    ob_start();
    $ui->display('banks.tpl');
    $output = ob_get_contents();
    ob_end_clean();
    
    echo "=== Finding Hidden Classes ===\n";
    
    // Search for hidden classes
    $lines = explode("\n", $output);
    $hidden_lines = [];
    
    for ($i = 0; $i < count($lines); $i++) {
        $line = trim($lines[$i]);
        if (strpos($line, 'hidden') !== false || strpos($line, 'd-none') !== false) {
            $hidden_lines[] = [
                'line_num' => $i + 1,
                'content' => $line
            ];
        }
    }
    
    echo "Found " . count($hidden_lines) . " lines with hidden classes:\n\n";
    
    foreach ($hidden_lines as $hidden) {
        echo "Line " . $hidden['line_num'] . ": " . $hidden['content'] . "\n";
        
        // Show context
        $start = max(0, $hidden['line_num'] - 4);
        $end = min(count($lines) - 1, $hidden['line_num'] + 2);
        
        echo "Context:\n";
        for ($j = $start; $j <= $end; $j++) {
            $marker = ($j == $hidden['line_num'] - 1) ? ">>> " : "    ";
            echo $marker . ($j + 1) . ": " . trim($lines[$j]) . "\n";
        }
        echo "\n";
    }
    
    // Check if the table itself has hidden classes
    if (preg_match('/<table[^>]*class="[^"]*"[^>]*>/', $output, $table_matches)) {
        echo "Table opening tag: " . $table_matches[0] . "\n";
        if (strpos($table_matches[0], 'hidden') !== false || strpos($table_matches[0], 'd-none') !== false) {
            echo "⚠ Table has hidden classes!\n";
        } else {
            echo "✓ Table does not have hidden classes\n";
        }
    }
    
    // Check if tbody has hidden classes
    if (preg_match('/<tbody[^>]*class="[^"]*"[^>]*>/', $output, $tbody_matches)) {
        echo "Tbody opening tag: " . $tbody_matches[0] . "\n";
        if (strpos($tbody_matches[0], 'hidden') !== false || strpos($tbody_matches[0], 'd-none') !== false) {
            echo "⚠ Tbody has hidden classes!\n";
        } else {
            echo "✓ Tbody does not have hidden classes\n";
        }
    }
    
} catch (Exception $e) {
    echo "Exception: " . $e->getMessage() . "\n";
} catch (Error $e) {
    echo "Fatal Error: " . $e->getMessage() . "\n";
}
?>
