<?php
// Initialize the session
session_start(['read_and_close' => true]);
require 'conn.php';

$coid  = $_SESSION['co_id'];
$userid  = $_SESSION['user_id'];

session_write_close();

$accs_access = $mysqli->query("
    SELECT DISTINCT p.pm_acc_lvl
    FROM accounts a
    JOIN permissions p 
        ON p.co_id = $coid
        AND p.user_id = $userid AND ( (a.acc_categ ='bank_wallet' OR a.acc_categ ='cash_wallet') OR (p.menu_access='accounts' ) )
    WHERE (
        p.pm_acc_id = a.id 
        OR (
            p.pm_acc_categ = a.acc_categ
            AND (
                p.pm_acc_id IS NULL 
                OR p.pm_acc_id = ''
            )
        )
    )
");

$acc_count = [];

while ($row = mysqli_fetch_assoc($accs_access)) {
    $acc_count[] = $row['pm_acc_lvl'];
}
 
 
 
 $inventory_access = $mysqli->query("
    SELECT DISTINCT p.pm_acc_lvl
    FROM accounts a
    JOIN permissions p 
        ON p.co_id = $coid
        AND p.user_id = $userid AND a.acc_categ ='warehouse'
    WHERE (
        p.pm_acc_id = a.id 
        OR (
            p.pm_acc_categ = a.acc_categ
            AND (
                p.pm_acc_id IS NULL 
                OR p.pm_acc_id = ''
            )
        )
    )
");

$inv_count = [];

while ($row = mysqli_fetch_assoc($inventory_access)) {
    $inv_count[] = $row['pm_acc_lvl'];
}
 
$customer_access = $mysqli->query("
    SELECT DISTINCT p.pm_acc_lvl
    FROM accounts a
    JOIN permissions p 
        ON p.co_id = $coid
        AND p.user_id = $userid AND a.acc_categ ='customer' 
    WHERE (
        p.pm_acc_id = a.id 
        OR (
            p.pm_acc_categ = a.acc_categ
            AND (
                p.pm_acc_id IS NULL 
                OR p.pm_acc_id = ''
            )
        )
    )
    OR p.menu_access='customers'
");

$cust_count = [];

while ($row = mysqli_fetch_assoc($customer_access)) {
    $cust_count[] = $row['pm_acc_lvl'];
} 
 
 
$supplier_access = $mysqli->query("
    SELECT DISTINCT p.pm_acc_lvl
    FROM accounts a
    JOIN permissions p 
        ON p.co_id = $coid
        AND p.user_id = $userid AND a.acc_categ ='supplier' 
    WHERE (
        p.pm_acc_id = a.id 
        OR (
            p.pm_acc_categ = a.acc_categ
            AND (
                p.pm_acc_id IS NULL 
                OR p.pm_acc_id = ''
            )
        )
    )
    OR p.menu_access='suppliers'
");

$supp_count = [];

while ($row = mysqli_fetch_assoc($supplier_access)) {
    $supp_count[] = $row['pm_acc_lvl'];
} 

$ledger_access = $mysqli->query("
    SELECT DISTINCT p.pm_acc_lvl
    FROM accounts a
    JOIN permissions p 
        ON p.co_id = $coid
        AND p.user_id = $userid 
    WHERE 
     p.menu_access='accounts_lists'
");

$ledg_count = [];

while ($row = mysqli_fetch_assoc($ledger_access)) {
    $ledg_count[] = $row['pm_acc_lvl'];
} 

$product_access = $mysqli->query("
    SELECT DISTINCT p.pm_acc_lvl
    FROM accounts a
    JOIN permissions p 
        ON p.co_id = $coid
        AND p.user_id = $userid 
    WHERE 
     p.menu_access='products'
");

$pdt_count = [];

while ($row = mysqli_fetch_assoc($product_access)) {
    $pdt_count[] = $row['pm_acc_lvl'];
} 


$permission_access = $mysqli->query("
    SELECT DISTINCT p.pm_acc_lvl
    FROM accounts a
    JOIN permissions p 
        ON p.co_id = $coid
        AND p.user_id = $userid 
    WHERE 
     p.menu_access='permissions'
");

$perm_count = [];

while ($row = mysqli_fetch_assoc($permission_access)) {
    $perm_count[] = $row['pm_acc_lvl'];
} 

$cash_adj_access = $mysqli->query("
    SELECT DISTINCT p.pm_acc_lvl
    FROM accounts a
    JOIN permissions p 
        ON p.co_id = $coid
        AND p.user_id = $userid 
    WHERE 
     p.menu_access='cash_adj'
");

$cshAdj_count = [];

while ($row = mysqli_fetch_assoc($cash_adj_access)) {
    $cshAdj_count[] = $row['pm_acc_lvl'];
} 

$testing_access = $mysqli->query("
    SELECT DISTINCT p.pm_acc_lvl
    FROM accounts a
    JOIN permissions p 
        ON p.co_id = $coid
        AND p.user_id = $userid 
    WHERE 
     p.menu_access='testing'
");

$test_count = [];

while ($row = mysqli_fetch_assoc($testing_access)) {
    $test_count[] = $row['pm_acc_lvl'];
} 

$admin_access = $mysqli->query("
    SELECT DISTINCT p.pm_acc_lvl
    FROM accounts a
    JOIN permissions p 
        ON p.co_id = $coid
        AND p.user_id = $userid 
    WHERE 
     p.menu_access='admin'
");

$admin_count = [];

while ($row = mysqli_fetch_assoc($admin_access)) {
    $admin_count[] = $row['pm_acc_lvl'];
} 
 
  ?>