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

-- Get all the config from the xml as well as test various aspects
-- Test what kind of user accessed the bord (admin or not)
userLevel = db.getPriv(layout.username) or 1

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

-- Set layout of the header and footer
header = layout.getHeader()
footer = layout.getFooter()

-- Get message config from xml file
smsProfileConfig = command.ex([[cat ]]..rsm_conf.gsd_xml_path) or ""

-- Get all the message config blobs
messageConfs = {}
for match in smsProfileConfig:gmatch("<message%s*(id=.->)%s-</message>") do
    local messageId = match:match("id=\"(.-)\"")
    local messageLabel = match:match("label=\"(.-)\"")
    table.insert(messageConfs, [[MessageId: ]]..messageId..[[\n]]..[[MessageLabel: ]]..messageLabel..[[\n]]..match)
end

-- Populate table of messages with message config blobs.
messages = {}
for i,configBlob in pairs(messageConfs) do
    blob = {}
    blob.msgId = configBlob:match([[MessageId:%s+(.-)%s-\n]]) or ""
    blob.msgLabel = configBlob:match([[MessageLabel:%s+(.-)%s-\n]]) or ""
    blob.msgText = configBlob:match([[<text_message>%s-(.-)%s-</text_message>]]) or ""
    blob.msgShowSite = configBlob:match([[<include_site>%s-(.-)%s-</include_site>]]) or ""
    blob.msgRecepients = configBlob:match([[<cell_numbers>%s-(.-)%s-</cell_numbers>]]) or ""

    local recepientCount = 0
    for match in blob.msgRecepients:gmatch("<recepient%s*(.-)%s-</recepient>") do
        recepientCount = recepientCount + 1
    end
    blob.msgRecepientCount = recepientCount

    table.insert(messages,blob)
end

-- Page content (the view)
content = ""

-- Set the content of the header
content = [[
                    <div id="info">
                        <center><h1>SMS Notification Profile</h1></center>
                    </div>
                    <script>
                        function toggleVis(target)
                        {
                            if (target.style.display == "none")
                            {
                                target.style.display = "inline-block";
                            }
                            else
                            {
                                target.style.display = "none";
                            }
                            return false;
                        }
                    </script>
]]

-- If admin give a note that the page can't be edited
if (userLevel == 1) then
    content = content .. [[
                    <center><b>Note:</b> Editing this section requires administrator privileges.</center><br>
    ]]
end

-- If no config was detected notify user
if (smsProfileConfig == "") then
    -- Don't bail, just warn
    content = content .. [[
                    <div id="info">
                        <center>
                            <h2><img src="images/warning.png"/> Config file(s) missing</h2>
                            Cannot read the following file(s) required for sms configuration:<br><br>
                            <span style="display: inline-block; text-align: left; width: 400px;">
                                <ul>
    ]]


    if (smsProfileConfig == "") then
        messageBroken = 1
        content = content .. [[
                                    <li>Message Config - ]]..rsm_conf.gsd_xml_path..[[</li>
        ]]
    end
    
    content = content .. [[
                                </ul>
                            </span>
                        </center>
                    </div>
    ]]
end

-- Main form div
content = content .. [[
                    <div class="formLayoutfull">
]]

-- Test if xml file is there, then setup main form
if (messageBroken == 1) then
    content = content .. [[
                        <h3><img src="images/warning.png" alt="message config missing" title="message config missing"> Messages:</h3>
                        Cannot determine path to messages.
]]
else
    -- Message table with all the configured messages
    content = content .. [[
                        <h3>Messages:</h3>
    ]]

    if (#(messages) > 0) then
        content = content .. [[
                        <table class="onetwo" style="text-align: center;">
                            <tr>
                                <th>Message Label:</th>
                                <th>No of Recepients:</th>
                                <th>Send site label:</th>
                            </tr>
        ]]
        for i, msg in pairs(messages) do
            content = content .. [[
                            <tr>
                                <td><a href="gsd_edit.lua?file=]]..msg.msgId..[["> ]]..msg.msgLabel..[[ <img src="images/spanner.png"/></a></td>
                                <td>]]..msg.msgRecepientCount..[[</td>
                                <td>]]..msg.msgShowSite..[[</td>
                            </tr>
            ]]
        end
        content = content .. [[
                        </table>
                        <br>
                        <b>Note:</b> Click on message labels to edit messages<br>
        ]]
    else
        content = content .. [[ 
                        There are no configured messages.<br>
        ]]
    end
    
    -- Add new message to the xml file
    content = content .. [[ 
                        <form name="addMessage" action="gsd_edit.lua" method="post">
                            <br><input type="submit" name="submitNewMessage" value="Add new message to SMS profile"]]..buttonControl..[[/><br>
                        </form>
                        <br><b>Note:</b> Changes / additions to SMS Notification profiles will restart the daemon.<br><br>
    ]]
end

-- End of main form div
content = content .. [[
                    </div>
]]

-- Show hyperlink to xml file
if (messageBroken ~= 1) then
    smsProfileConfig = smsProfileConfig:gsub("<","&lt;")
    smsProfileConfig = smsProfileConfig:gsub(">","&gt;")
    content = content .. [[
                    <br><img src="images/magnify.png"/> <a href="javascript:void(0)" onclick="toggleVis(document.getElementById('smsProfileConfig'));">View ]]..                      rsm_conf.gsd_xml_path..[[</a></a><br>

                    <div id="smsProfileConfig" style="display: none;">
                        <pre class="formLayoutfull">]]..smsProfileConfig..[[</pre>
                    </div>
    ]]
end

--Post webpage
html = header
html = html..content
html = html..footer

print(html)


