<?php

ini_set('display_errors', 1);
error_reporting(E_ERROR);

if (isset($_SESSION["username_" . $dbname])) {
    $user = $_SESSION["username_" . $dbname];

    $getuser = mysqli_query($conn, "SELECT * FROM `passmain` WHERE `user`='$user'") or die(mysqli_error($conn));
    $guser = mysqli_fetch_array($getuser);
    $none = "style='display:none'";

    $permissionGroups = [
        'master'       => ['customer', 'supplier', 'product', 'changeofrates', 'compmast'],
        'transactions' => ['purchase', 'sale', 'debnote', 'crednote', 'inward_ent', 'outward', 'damaged'],
        'reports'      => ['stock', 'inouthist', 'negstock', 'itemwisesl', 'itemwisepl', 'leadparty', 'leadprod'],
        'registers'    => ['purreg', 'salereg', 'inwreg', 'outreg', 'damreg', 'debreg', 'credreg'],
        'dashboards'   => ['inward', 'onlent_demo', 'inward_upload', 'minstkalert'],
        'tools'        => ['tlcompany', 'tlprofile']
    ];

    if ($guser) {
        foreach ($permissionGroups as $group => $fields) {
            $allEmpty = true;
            foreach ($fields as $field) {
                if (empty($guser[$field])) {
                    $$field = $none;
                } else {
                    $allEmpty = false;
                }
            }
            if ($allEmpty) {
                $$group = $none;
            }
        }
    }
}