<?php

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

    $getuser = mysqli_query($conn, "SELECT * FROM `passmain` WHERE `user`='$user' and `spltype`='$spec'") or die(mysqli_error($conn));
    $guser = mysqli_fetch_array($getuser);
    $line = '';

    $none = "style='display:none'";

    $permissionGroups = [
        'masters'      => ['customer','supplier','openbal','chcustdtls','transport','staff','unit','color','rawmatgrp','rawmat','rawchrateop','bank','cash','expense','group','otherac'],
        'rawmaterials' => ['purchmat','debnoteraw','salematmulti','crednoteraw','inwardraw','damagedraw','purchreg','slmatreg','inwregraw','damregraw','rawstock','rawinwout','rawmatwise','rawmatwisesale','rawmgrpsl','partygrpwisesaleraw'],
        'accounts'     => ['bankrecp', 'bankpay', 'cashrecp', 'cashpay', 'contra', 'journal', 'ledger', 'leadparty', 'dailyreport', 'dailycashrp', 'bankbook', 'cashbook', 'jvbook'],
        'containers'   => ['contentry', 'containerreg', 'contalert'],
    ];
    foreach ($permissionGroups as $grp => $fields) {
        $allEmpty = true;
        foreach ($fields as $field) {
            if (empty($guser[$field])) {
                $$field = $none;
            } else {
                $allEmpty = false;
            }
        }
        if ($allEmpty) {
            $$grp = $none;
        }
    }
}