require("luamods/command")
require("luamods/layout")
require("luamods/cgi")
require("config/rsm_conf")

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

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

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

siteConfig = command.ex(rsm_conf.rsmc_path..[[ GET: TAG_SITE_CFG]]) or ""
siteID = siteConfig:match([[TAG_SITE_ID=%"([^%"]+)%"]]) or "unknown"
siteLabel = siteConfig:match([[TAG_SITE_LABEL=%"([^%"]+)%"]]) or "unknown"
siteNMS = siteConfig:match([[TAG_SITE_NMS_ADDRESS=%"([^%"]+)%"]]) or "unknown"
siteCurrEvtSeq = siteConfig:match([[TAG_SITE_CURR_EVT_SEQ=(%x+)]]) or "unknown"
siteLastEvtSeq = siteConfig:match([[TAG_SITE_LAST_EVT_SEQ=(%x+)]]) or "unknown"
siteState = siteConfig:match([[TAG_SITE_STATE=(%x+)]]) or "unknown"
siteVersion = siteConfig:match([[TAG_VERSION=(%x+)]]) or "unknown"

option1 = "" -- This is for "unknown"
option2 = "" -- DISCOVERED
option3 = "" -- MAINTENANCE
option4 = "" -- OPERATIONAL

command.ex(rsm_conf.wtr_down)

if (cgi.post.saveButton == "Save and Apply") and (userLevel ~= 1) then
    newLabel = cgi.post.label or ""
    newState = cgi.post.state or ""
    newNMS = cgi.post.nms or ""
    
    result = ""
    if (newLabel ~= siteLabel) then
        result = result .. command.ex(rsm_conf.rsmc_path..[[ SET: TAG_SITE_CFG TAG_SITE_LABEL="]]..newLabel..[["]])
        siteLabel = newLabel
        saved = 1
    end
    if (newState ~= siteState) then
        result = result .. command.ex(rsm_conf.rsmc_path..[[ SET: TAG_SITE_CFG TAG_SITE_STATE=]]..newState..[[]])
        siteState = newState
        saved = 1
    end
    if (newNMS ~= siteNMS) then
        result = result .. command.ex(rsm_conf.rsmc_path..[[ SET: TAG_SITE_CFG TAG_SITE_NMS_ADDRESS="]]..newNMS..[["]])
        siteNMS = newNMS
        saved = 1
    end
end

if siteState == "00" then -- DISCOVERED
    option2 = " selected"
elseif siteState == "01" then
    option3 = " selected" -- MAINTENANCE
elseif siteState == "02" then
    option4 = " selected" -- OPERATIONAL
else
    option1 = " selected" -- "unknown"
end

content = ""

content = content .. [[
                    <div id="info">
                        <center><h1>]]..siteLabel..[[</h1></center>
                    </div>
]]

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

if (siteConfig == "") then
    -- Bail on error
    content = content .. [[
                    <div id="info">
                        <center>
                            <h2><img src="images/warning.png"/> Invalid response</h2>
                            The RSM client ]]..rsm_conf.rsmc_path..[[ is missing, not responding or returned an invalid reply.<br>
                            This may also happen if the site is currently experiencing a lot of traffic.
                        </center>
                    </div>
]]
    print(header..content..footer)
    return
end                    

content = content .. [[

                    <div class="formLayoutfull">
                        <form action="rsm_site.lua" method="post">
                            <label style="display: inline-block; width: 200px;">Site ID:</label> ]] .. siteID .. [[

                            <br><label style="display: inline-block; width: 200px;">Site State:</label>
                            <select name="state"]]..buttonControl..[[>
                                <option value="unknown" disabled]]..option1..[[>UNKNOWN</option>
                                <option value="00" disabled]]..option2..[[>DISCOVERED</option>
                                <option value="01"]]..option3..[[>MAINTENANCE</option>
                                <option value="02"]]..option4..[[>OPERATIONAL</option>
                            </select>

                            <br><br><label style="display: inline-block; width: 200px;">Current Event Sequence:</label> ]] .. siteCurrEvtSeq .. [[

                            <br><label style="display: inline-block; width: 200px;">Last Event Sequence:</label> ]] .. siteLastEvtSeq .. [[

                            <br><label style="display: inline-block; width: 200px;">Site Version:</label> ]] .. siteVersion .. [[

                            <br><br><label style="display: inline-block; width: 200px;">Site Label:</label> <input type="text" name="label" value="]]..siteLabel..[["]]..inputControl..[[/>
                            <br><label style="display: inline-block; width: 200px;">NMS Address:</label> <input type="text" name="nms" value="]]..siteNMS..[["]]..inputControl..[[/>
                            <br><br><hr><br>
]]
if (saved == 1) then
    content = content .. [[
                            <b><img src="images/ok.png"/> Changes saved.</b><br><br>
]]
end
content = content .. [[
                            <input type="submit" value="Save and Apply" name="saveButton"]]..buttonControl..[[/> 
                        </form>
                    </div>
]]

command.ex(rsm_conf.wtr_up)

print(header..content..footer)
