<?php
    session_start();
	if ( empty($_SESSION['login']) ) {
        header( 'Location: login.php' );
        die;
	}

    include 'include/database.php';

    $connection = connect($database);

    $canAdmin = canAdministrate($connection, $_SESSION['login']);

    if ($canAdmin) {
        $users = getUsers($connection);
    } else {
        $users = array();
    }

    if ($canAdmin) {
        $userTable = '
            <table id="userTable" class="center">
                <tr>
                    <th>User</th>
                    <th>View</th>
                    <th>Reset</th>
                    <th>Edit</th>
                    <th>Delete</th>
                    <th>Links</th>
                    <th>Control</th>
                    <th>Admin</th>
                    <th></th>
                </tr>
        ';

        foreach ($users as $user) {
            $canViewTables = canViewTables($connection, $user['name']);
            $canReset = canResetAlerts($connection, $user['name']);
            $canEdit = canEditValues($connection, $user['name']);
            $canDelete = canDeleteValues($connection, $user['name']);
            $canViewLinks = canViewLinks($connection, $user['name']);
            $canControl = canControlSystems($connection, $user['name']);
            $canAdmin = canAdministrate($connection, $user['name']);

            $userTable .= "
                <tr>
                    <td>{$user['name']}</td>
                    <td><input onchange=\"sendPermissions({$user['id']}, '{$user['name']}')\" id=\"{$user['name']} view\" type=\"checkbox\"";

            if ($canViewTables) {
                $userTable .= ' checked';
            }

            $userTable .= " /></td>
                    <td><input onchange=\"sendPermissions({$user['id']}, '{$user['name']}')\" id=\"{$user['name']} reset\" type=\"checkbox\"";

            if ($canReset) {
                $userTable .= ' checked';
            }

            $userTable .= " /></td>
                    <td><input onchange=\"sendPermissions({$user['id']}, '{$user['name']}')\" id=\"{$user['name']} edit\" type=\"checkbox\"";

            if ($canEdit) {
                $userTable .= ' checked';
            }

            $userTable .= " /></td>
                    <td><input onchange=\"sendPermissions({$user['id']}, '{$user['name']}')\" id=\"{$user['name']} delete\" type=\"checkbox\"";

            if ($canDelete) {
                $userTable .= ' checked';
            }

            $userTable .= " /></td>
                    <td><input onchange=\"sendPermissions({$user['id']}, '{$user['name']}')\" id=\"{$user['name']} links\" type=\"checkbox\"";

            if ($canViewLinks) {
                $userTable .= ' checked';
            }

            $userTable .= " /></td>
                    <td><input onchange=\"sendPermissions({$user['id']}, '{$user['name']}')\" id=\"{$user['name']} control\" type=\"checkbox\"";

            if ($canControl) {
                $userTable .= ' checked';
            }

            $userTable .= " /></td>
                    <td><input onchange=\"sendPermissions({$user['id']}, '{$user['name']}')\" id=\"{$user['name']} admin\" type=\"checkbox\"";

            if ($canAdmin) {
                $userTable .= ' checked';
            }

            $userTable .= " /></td>
                    <td><button onclick=\"deleteUser('{$user['name']}')\">Delete</button></td>
                </tr>";
        }

        $userTable .= '
            </table>
        ';

        echo $userTable;
    } else {
        echo '<strong class="warning">You do not have permission to see this yet! Please email the administrator of this system for assistance...</strong>';
    }

    $connection = null;
?>