<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

<div class="row row-flex">
    <div class="col-xs-12 text-center">
        <h1>Control</h1>
    </div>
    <div class='col-xs-4 text-center box-me'>
        <!-- Use data-confirm-message to set the exact message that pops up in the alert window.  -->

        <a href="#"
        data-confirm-message="Are you sure you want to remotely start the well?"
        data-refreshpause="1"
        data-command=""
        data-staticsend="{'tag': 'cmd_Start', 'val': 1}"
        data-channelId="<%= channels["transferlite.writeplctag"].channelId %>"
        data-techname="<%=channels["transferlite.writeplctag"].techName %>"
        data-name="<%= channels["transferlite.writeplctag"].name%>"
        data-nodechannelcurrentId="<%= channels["transferlite.writeplctag"].nodechannelcurrentId %>"
        id="<%= channels["transferlite.writeplctag"].channelId %>"
        class="btn btn-large btn-success confirmstatic pad15">
        Start</a>
    </div>

    <div class='col-xs-4 text-center box-me'>
        <a href="#"
        data-confirm-message="Are you sure you want to remotely stop the well?"
        data-refreshpause="1"
        data-command=""
        data-staticsend="{'tag': 'cmd_Stop', 'val': 1}"
        data-channelId="<%= channels["transferlite.writeplctag"].channelId %>"
        data-techname="<%=channels["transferlite.writeplctag"].techName %>"
        data-name="<%= channels["transferlite.writeplctag"].name%>"
        data-nodechannelcurrentId="<%= channels["transferlite.writeplctag"].nodechannelcurrentId %>"
        id="<%= channels["transferlite.writeplctag"].channelId %>"
        class="btn btn-large btn-danger confirmstatic pad15">
        Stop</a>

    </div>

    <div class='col-xs-4 text-center box-me'>
        <a href="#"
        data-confirm-message="Are you sure you want to remotely reset alarms?"
        data-refreshpause="1"
        data-command=""
        data-staticsend="{'tag': 'cmd_ResetAlarms', 'val': 1}"
        data-channelId="<%= channels["transferlite.writeplctag"].channelId %>"
        data-techname="<%=channels["transferlite.writeplctag"].techName %>"
        data-name="<%= channels["transferlite.writeplctag"].name%>"
        data-nodechannelcurrentId="<%= channels["transferlite.writeplctag"].nodechannelcurrentId %>"
        id="<%= channels["transferlite.writeplctag"].channelId %>"
        class="btn btn-large btn-warning confirmstatic pad15">
        Reset Alarms</a>

    </div>
</div>
<div class="row row-flex">
    <div class="col-xs-12 text-center">
        <h1>Auto Mode
            <% if(channels['transferlite.auto_manual'].value === "Auto") { %>
                <span class="label label-success">Active</span>
            <% } %>
        </h1>
    </div>
    <div class='col-xs-4 text-center box-me'>
        <a href="#"
        data-confirm-message="Are you sure you want to remotely activate auto mode?"
        data-refreshpause="1"
        data-command=""
        data-staticsend="{'tag': 'cfg_Mode', 'val': 0}"
        data-channelId="<%= channels["transferlite.writeplctag"].channelId %>"
        data-techname="<%=channels["transferlite.writeplctag"].techName %>"
        data-name="<%= channels["transferlite.writeplctag"].name%>"
        data-nodechannelcurrentId="<%= channels["transferlite.writeplctag"].nodechannelcurrentId %>"
        id="<%= channels["transferlite.writeplctag"].channelId %>"
        class="btn btn-large btn-theme animated confirmstatic pad15">
        Set Auto Mode</a>

    </div>
    <div class="col-md-4 box-me entry-top-level" id="flow_rate_setpoint">
        <div class="pad15">
            <h2>Flow Rate Setpoint</h2>
            <form class="form-inline">
                <div class="form-group">
                    <input class="form-control val_box"
                    type="number"
                    step="any"
                    value="<%=channels['transferlite.flow_rate_setpoint'].value %>">
                </div>
                <a href="#"
                data-confirm-message="Are you sure you want to do this?"
                data-refreshpause="1"
                data-command=""
                data-staticsend="{'tag': 'set_FlowRateSetpoint', 'val': <%=channels['transferlite.flow_rate_setpoint'].value %>}"
                data-channelId="<%= channels["transferlite.writeplctag"].channelId %>"
                data-techname="<%=channels["transferlite.writeplctag"].techName %>"
                data-name="<%= channels["transferlite.writeplctag"].name%>"
                data-nodechannelcurrentId="<%= channels["transferlite.writeplctag"].nodechannelcurrentId %>"
                id="<%= channels["transferlite.writeplctag"].channelId %>"
                class="btn btn-large btn-theme animated setstatic material-icons">send</a>
            </form>
        </div>
    </div>
</div>

<div class="row row-flex">
    <div class="col-xs-12 text-center">
        <h1>Manual Mode
            <% if(channels['transferlite.auto_manual'].value === "Manual") { %>
                <span class="label label-success">Active</span>
            <% } %>
        </h1>
    </div>
    <div class='col-xs-4 text-center box-me'>
        <a href="#"
        data-confirm-message="Are you sure you want to remotely activate manual mode?"
        data-refreshpause="1"
        data-command=""
        data-staticsend="{'tag': 'cfg_Mode', 'val': 1}"
        data-channelId="<%= channels["transferlite.writeplctag"].channelId %>"
        data-techname="<%=channels["transferlite.writeplctag"].techName %>"
        data-name="<%= channels["transferlite.writeplctag"].name%>"
        data-nodechannelcurrentId="<%= channels["transferlite.writeplctag"].nodechannelcurrentId %>"
        id="<%= channels["transferlite.writeplctag"].channelId %>"
        class="btn btn-large btn-theme animated confirmstatic pad15">
        Set Manual Mode</a>

    </div>

    <div class="col-md-4 box-me entry-top-level" id="system1_frequency_setpoint">
        <div class="pad15">
            <h2>System 1 Frequency</h2>
            <form class="form-inline">
                <div class="form-group">
                    <input class="form-control val_box"
                    type="number"
                    step="any"
                    value="<%=channels['transferlite.system1_frequency_setpoint'].value %>">
                </div>
                <a href="#"
                data-confirm-message="Are you sure you want to do this?"
                data-refreshpause="1"
                data-command=""
                data-staticsend="{'tag': 'set_ManualFrequencySP_System1', 'val': <%=channels['transferlite.system1_frequency_setpoint'].value %>}"
                data-channelId="<%= channels["transferlite.writeplctag"].channelId %>"
                data-techname="<%=channels["transferlite.writeplctag"].techName %>"
                data-name="<%= channels["transferlite.writeplctag"].name%>"
                data-nodechannelcurrentId="<%= channels["transferlite.writeplctag"].nodechannelcurrentId %>"
                id="<%= channels["transferlite.writeplctag"].channelId %>"
                class="btn btn-large btn-theme animated setstatic material-icons">send</a>
            </form>
        </div>
    </div>
    <div class="col-md-4 box-me entry-top-level" id="system2_frequency_setpoint">
        <div class="pad15">
            <h2>System 2 Frequency</h2>
            <form class="form-inline">
                <div class="form-group">
                    <input class="form-control val_box"
                    type="number"
                    step="any"
                    value="<%=channels['transferlite.system2_frequency_setpoint'].value %>">
                </div>
                <a href="#"
                data-confirm-message="Are you sure you want to do this?"
                data-refreshpause="1"
                data-command=""
                data-staticsend="{'tag': 'set_ManualFrequencySP_System2', 'val': <%=channels['transferlite.system2_frequency_setpoint'].value %>}"
                data-channelId="<%= channels["transferlite.writeplctag"].channelId %>"
                data-techname="<%=channels["transferlite.writeplctag"].techName %>"
                data-name="<%= channels["transferlite.writeplctag"].name%>"
                data-nodechannelcurrentId="<%= channels["transferlite.writeplctag"].nodechannelcurrentId %>"
                id="<%= channels["transferlite.writeplctag"].channelId %>"
                class="btn btn-large btn-theme animated setstatic material-icons">send</a>
            </form>
        </div>
    </div>
</div>


<script>

        $('.val_box').each(function(topLevel){
            $(this).change(function(){
                var id = "#" + $(this).closest(".entry-top-level").attr('id');
                if (id !== "#undefined"){
                    // console.log("before parse", $(id).find('.setstatic').attr('data-staticsend').replace(/'/g, '"'));
                    var jsobj = JSON.parse($(id).find('.setstatic').attr('data-staticsend').replace(/'/g, '"'));
                    // console.log("after parse", jsobj)
                    // var tagStart = $(id).find('.setstatic').attr('data-staticsend').indexOf("tag") + 3;
                    // var tagStop = $(id).find('.setstatic').attr('data-staticsend').indexOf("] ]");
                    // var tag = $(id).find('.setstatic').attr('data-staticsend').substring(tagStart, tagStop).split(',')[0].replace(/'/g,"");
                    var val = $(id).find('.val_box').val();
                    var newData = "{'tag': '" + jsobj.tag +"', 'val': " + val.toString() + "}"
                    var tag = $(id).find('.setstatic').attr('data-staticsend', newData);
                    console.log($(id).find('.setstatic').attr('data-staticsend'));
                }
            });
        });
        
    </script>
    
    <style>
        .box-me {
            position: relative;
            padding: 0.5em;
            padding-bottom: 1.5em;
            border: 1px solid #eee;
            /*margin: 1em 0;*/
        }
        .box-me .gauge-box {
            margin-top: -0.25em;
        }
        
        .pad15 {
            margin: 15px 15px;
        }
        
        .box-me h2 {
            text-transform: uppercase;
            font-size: 14px;
            color: #666;
            font-weight: 400;
            letter-spacing: 1px;
            z-index: 100;
        }
        .dynamic-chart-form {
            background-color: whiteSmoke;
            padding: 1em 0.5em;
            margin-top: 1em;
        }
        
        .row-flex {
            display: -webkit-box;
            display: -webkit-flex;
            display: -ms-flexbox;
            display:         flex;
            flex-wrap: wrap;
        }
        .row-flex > [class*='col-'] {
            display: flex;
            flex-direction: column;
        }
        
        </style>