<?php
include 'DB.php';
$permissions = array(
    'Add-Customer' => array(),
    'KYC' => array(),
    'Pending' => array('Scan', 'Documents','Path','Update-Customer'),
    'Allotment' => array('Documents','Path','Update-Customer','Update-Registration-No'),
    'Proceed'=> array('Documents','Path','Update-Customer','Update-Registration-No'),
    'Upload'=> array('Scan', 'Documents','Path','Update-Customer','Done'),
    'Insp'=> array('Scan','Documents','Path','Update-Customer','Update-Registration-No','Next','Remark'),
    'Payment'=> array('Scan', 'Documents','Path','Update-Customer'),
    'Semi-Successfull'=> array('Documents','Path','Update-Customer','Update-Registration-No','Update-TaxReceipt'),
    'HSRP'=> array('Scan','Documents','Path','Update-Customer','Update-Registration-No'),
    'Fitment'=> array('Call','Documents','Path','Update-Customer','Update-Registration-No'),
    'Ready-to-go'=> array('Call','Scan','Documents','Path','Update-Customer','Update-Registration-No'),
	'RC'=> array('Call','Documents','Path','Update-Customer','Update-Registration-No','Docs'),
    'Register'=> array('Call','Documents','Path','Update-Customer','Update-Registration-No','Docs'),
    'IssueDocs'=> array('Call','Documents','Scan','Remark'),
    'Full-Successfull'=> array('Documents','Path','Update-Customer','Update-Registration-No'),
    'Update-Registration-No'=> array(),
	'RTO-Data'=> array()
);

$user_id = $_GET['id'];
$userPermissions = array();
if (!empty($user_id)) {
    $bdd = new DB();
    $userPermissionsQuery = "SELECT Tabs, Submenu FROM Permissions WHERE UId = $user_id";
    $userPermissionsResult = $bdd->getAll($userPermissionsQuery);
    foreach ($userPermissionsResult as $row) {
        $userPermissions[$row['Tabs']] = explode(',', $row['Submenu']);
    }
}
$htmlContent = ''; 

foreach ($permissions as $permission => $submenus) {
    $htmlContent .= '<label>';
    $htmlContent .= '<input type="checkbox" name="main_permissions[]" value="' . $permission . '" onclick="toggleSubmenu(\'' . str_replace(' ', '-', $permission) . '_submenu\')" ';

    // Check if the permission is in the user's permissions and mark it as checked
    if (isset($userPermissions[$permission])) {
        $htmlContent .= 'checked';
    }

    $htmlContent .= '> ' . $permission . '</label><br>';

    if (isset($userPermissions[$permission])) {
        $htmlContent .= '<div style="display:block;" class="submenu" id="' . str_replace(' ', '-', $permission) . '_submenu">';
    } else {
        $htmlContent .= '<div style="display:none;" class="submenu" id="' . str_replace(' ', '-', $permission) . '_submenu">';
    }
    if (!empty($submenus)) {
        $htmlContent .= '<h5>Sub-menu for ' . $permission . ':</h5>';
    }

    // Generate sub-menu checkboxes here and mark them as checked if they exist in the user's permissions
    foreach ($submenus as $submenu) {
        $htmlContent .= '<label>';
        $htmlContent .= '<input type="checkbox" name="submenu_permissions[' . $permission . '][]" value="' . $submenu . '" ';

        if (isset($userPermissions[$permission]) && in_array($submenu, $userPermissions[$permission])) {
            $htmlContent .= 'checked';
        }

        $htmlContent .= '> ' . $submenu . '</label><br>';
    }

    if (!empty($submenus)) {
        $htmlContent .= '<br>';
        $htmlContent .= '<br>';
    }
    $htmlContent .= '</div>';
}

// Return the generated HTML content as an AJAX response
echo $htmlContent;
?>
