require("luamods/db")
require("luamods/cgi")
require("luamods/layout")

userLevel = db.getPriv(layout.username) or 1

if (userLevel == 1) then
    buttonControl = " disabled"
    inputControl = " readonly"
else
    buttonControl = ""
    inputControl = ""
end

header = layout.getHeader()
footer = layout.getFooter()

userstb = db.getusers()

content = [[
                    <div id="info">
                        <div style="background:url(images/users.png); background-size:32px; background-repeat:no-repeat; background-position: 20px center;">
                            <center><h1>Users - Manage Accounts</h1></center>
                        </div>
                    </div>
]]                    
if (userLevel == 1) then
    content = content .. [[
                    <center><b>Note:</B> Editing this section requires administrator privileges.</center><br>
]]
end
content = content .. [[
                    <div>
                        <br>
                        <center>
                            <b>This is a list of all the accounts in the database.<br><br>
                            Click on the usernames to manage individual users.</b>
                        </center><br>
]]
if (#(userstb) > 0) then
    content = content .. [[
                    <table class="onetwo" border="1">
                        <tr>
                            <th style="width: 75%">Username</th>
                            <th>User Type</th>
                        </tr>
]]

    usertypes = {"Administrator", "Guest", "Developer"}

    for idx, usr in pairs(userstb) do
        if usr.name == layout.username then mark = "*" else mark = "" end
        content = content .. [[
                        <tr>
                            <td><a href="users_edit.lua?username=]]..usr.name..[[">]]..usr.name..[[ ]]..mark..[[ <img src="images/spanner.png"/></a></td>
                            <td>]]..usertypes[usr.priv+1]..[[</td>
                        </tr>
]]
    end

    content = content .. [[
                    </table><br>
]]
end

content = content..[[
                    <form action="users_new.lua" method="post">
                        <input type="Submit" name="submitButton" value="Create New User Account"]]..buttonControl..[[/>
                    </form>
                </div>
]]    

print (header..content..footer)
