require("luamods/cgi")
require("luamods/layout")
require("luamods/command")
require("luamods/db")
require("config/ccd_conf")

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()

content = [[
                    <div id="info">
                        <center><h1>Climate Control Profiles</h1></center>
                    </div>
]]

if (userLevel == 1) then
    content = content .. [[
                    <center><b>Note:</b> Editing this section requires administrator privileges.</center><br>
]]
end

if (cgi.post.Restart == "Restart Daemon") and (userLevel ~= 1) then
    result = command.ex(ccd_conf.restart_cmd)
    content = content .. [[
                    <div id="info">
                        <center><BR><b>Restarting daemon...</b><BR><BR>]]..result..[[</center>
                    </div>]]
end

f = command.getFilesInTable(ccd_conf.profiles_path)
profiles = {}
for k,v in pairs(f) do
    xml = command.ex("cat "..ccd_conf.profiles_path..v)

    name, tmp = xml:match("<profile%s+name=\"(.-)\">%s*(.-)%s*</profile>")

    if name ~= nil and tmp ~= nil then
        profiles[k] = {}
        profiles[k].filename = v
        profiles[k].name = name
    end
end

content = content .. [[
                    <div class="formLayoutfull">
                        <TABLE BORDER="1" class="onetwo" style="text-align: center;">
                            <TR bgcolor="#D8D8D8">
                                <TH>Filename</TH>
                                <TH>Profile name</TH>
                                <TH>Manage</TH>
                            </TR>
]]
for k,v in pairs(profiles) do
    content = content .. [[
                            <TR>
                                <TD style="vertical-align: middle;">]]..v.filename..[[</TD>
                                <TD style="vertical-align: middle;">]]..v.name..[[</TD>
                                <TD style="vertical-align: middle;">
                                    <FORM action="ccd_edit.lua" method="post">
                                        <INPUT type="hidden" name="filename" value="]]..v.filename..[["/>
                                        <INPUT type="submit" name="Edit" value="Edit"/>
                                    </FORM>
                                </TD>
                            </TR>
]]
end
content = content .. [[
                        </TABLE><BR>
                        <FORM action="ccd_edit.lua" method="post">
                            <INPUT type="submit" name="Submit" value="Add new climate control profile" ]]..buttonControl..[[/>
                        </FORM><BR>
                        <B>Note:</B> Changes / additions to climate control profiles<BR>
                        only take effect when the daemon is restarted.<BR><BR>
                        <FORM action="ccd_profiles.lua" method="post">
                            <INPUT type="submit" name="Restart" value="Restart Daemon" ]]..buttonControl..[[/>
                        </FORM>
                    </div>
]]
html = header
html = html..content
html = html..footer

print(html)

