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

content = ""

devID = cgi.get.devID or cgi.post.devID
if (devID == nil) or (devID == "") then devID = "unknown" end

siteConfig = command.ex(rsm_conf.rsmc_path..[[ GET: TAG_SITE_CFG]]) or ""
siteLabel = siteConfig:match([[TAG_SITE_LABEL=%"([^%"]+)%"]]) or "unknown"
devConfig = command.ex(rsm_conf.rsmc_path..[[ GET: TAG_DEV_CFG]]) or ""

oldDev = {}
newDevs = {}

for dcfg in devConfig:gmatch("TAG_DEV_CFG (.-) TAG_VERSION") do
    tmpDev = {}
    tmpDev.devID = dcfg:match("TAG_DEV_ID=(%x+)") or "unknown"
    tmpDev.devType = dcfg:match("TAG_DEV_TYPE=(%x+)") or "07"
    tmpDev.devStatus = dcfg:match("TAG_DEV_STATUS=(%x+)") or "02"
    tmpDev.devState = dcfg:match("TAG_DEV_STATE=(%x+)") or "03"
    tmpDev.devLabel = dcfg:match([[TAG_DEV_LABEL=%"(.-)%"]]) or "unknown"
    -- This is the device that wants to be swopped
    if (devID == tmpDev.devID) then
        oldDev = tmpDev
    else
        if (tmpDev.devStatus == "01") and (tmpDev.devState ~= "02") then
            table.insert(newDevs, tmpDev)
        end
    end
end 

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

if (cgi.post.swopButton == "Swop Device") and (userLevel ~= 1) then
    oldID = devID or ""
    newID = cgi.post.newID or ""
    if (oldID ~= "") and (newID ~= "") then
        command.ex(rsm_conf.rsmc_path..[[ REP: TAG_DEV_ID_OLD=]]..devID..[[ TAG_DEV_ID_NEW=]]..newID)
        content = content .. [[
                    <div id="info">
                        <center>
                            <h2>Device Swopped</h2>
                            <b>Device swop from ]]..oldID..[[ to ]]..newID..[[ has been initiated..<br><br>
                            <a href="rsm_devices.lua">Back to devices</a>
                        </center>
                    </div>
]]
        print(header..content..footer)
        os.exit()        
    end
end

if (userLevel == 1) then
    print("Location: rsm_devices.lua")
else
    if (oldDev.devState ~= "01") or (oldDev.devStatus ~= "00") then 
        -- Bail on error
        content = content .. [[
                    <div id="info">
                        <center>
                            <h2><img src="images/warning.png"/> Device cannot be swopped</h2>
                            This device is not eligible for a device swop.<br>
                            Only offline devices in maintenance mode may be swopped.<br><br>
                            <a href="rsm_devices.lua">Back to devices</a>
                        </center>
                    </div>
]]
        print(header..content..footer)
        os.exit()
    end
end

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

if (siteConfig == "") or (devConfig == "") 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 happen if the site is currently experiencing a lot of traffic.<br><br>
                            <a href="rsm_devices.lua">Back to devices</a>
                        </center>
                    </div>
]]
    print(header..content..footer)
    return
end

if (#(newDevs) > 0) then
    content = content .. [[
                    <h3>Swopping device "]]..oldDev.devLabel..[[" (serial ]]..oldDev.devID..[[) :</h3>
                    <div class="formLayoutfull">
                        <form action="rsm_device_swop.lua" method="post">
                            <center>
                                <b>Select new device to swop with.</b><br><br>
                                <select name="newID"]]..buttonControl..[[>
                                    <option disabled>Select device</option>
]]
    for i,dev in pairs(newDevs) do
        content = content .. [[
                                    <option value="]]..dev.devID..[[">]]..dev.devLabel..[[ - ]]..dev.devID..[[</option>
]]
    end
    content = content .. [[
                                </select>
                                <input type="submit" name="swopButton" value="Swop Device" onclick="return confirm('Are you sure you want to perform the device swop?');"/>
                                <input type="button" name="cancelButton" value="Cancel" onclick="window.location.assign('rsm_device_edit.lua?devID=]]..oldDev.devID..[[');"/>
                                <input type="hidden" name="devID" value="]]..oldDev.devID..[["/>
                            </center>
                        </form>
                    </div>
]]
else
    content = content .. [[
                    <h3>Swopping device "]]..oldDev.devLabel..[[" (serial ]]..oldDev.devID..[[) :</h3>
                    <div id="info">
                        <center>
                            <h2>No eligible devices found</h2>
                            There are no suitable devices to swop with.<br>
                            Candidate devices must be <b>online</b> but not <b>operational.</b><br><br>
                            <a href="rsm_device_edit.lua?devID=]]..oldDev.devID..[[">Back to Device</a>
                        </center>
                    </div>        
]]
end

print(header..content..footer)
