<style type="text/css">
    tr.homematic-servicemessage {
        height: 34px;
    }
    tr.homematic-servicemessage td {
        padding-right: 12px;
    }
</style>
<script type="text/javascript">
"use strict";

servConn.getStringtable(function (res) {
    // TODO - bind relies on this is already done. Reliable?
    localData.homematicStringtable = res;
});

vis.binds.homematic = {
    serviceMessages: function (el) {

        function appendRow(el, id, name, msg, ts, oper) {

            if (oper == 7) {
                // Meldung kann bestätigt werden
                var button = '<button stlye="display:none;" data-hm-alarm="'+id+'" id="'+el.attr('id')+'_'+id+'">bestätigen</button>';
            } else {
                var button = "";
            }

            el.append('<tr class="homematic-servicemessage">' +
                    '<td class="homematic-servicemessage-device">'+name+'</td>' +
                    '<td class="homematic-servicemessage-text">'+msg+'</td>' +
                    '<td class="homematic-servicemessage-ts">'+ts+'</td>' +
                    '<td class="homematic-servicemessage-button">'+button+'</td></tr>')
            .find('#'+el.attr('id')+'_'+id)
            .button({
                icons: {
                    primary: "ui-icon-check"
                },
                text: false
            }).click(function () {
                //alert("receipt "+jQuery(this).attr('data-hm-alarm'));
                servConn.alarmReceipt(parseInt(jQuery(this).attr('data-hm-alarm'), 10));
                jQuery(this).parent().parent().remove();
            }).show();

        }

        function buildServiceMessageTable() {
            var $this = jQuery(el).prev().find("table.homematic-servicemessages");
            $this.html("<tbody></tbody>");

            var ALDPs = localData.metaIndex.ALDP;
            for (var i = 0; i < ALDPs.length; i++) {

                if (localData.uiState['_' + ALDPs[i] + '.Value'] == 1) {
                    var metaAlarm = localData.metaObjects[ALDPs[i]];
                    var metaTrigger = localData.metaObjects[metaAlarm.AlTriggerDP];
                    var metaDevice = localData.metaObjects[localData.metaObjects[metaTrigger.Parent].Parent];
                    var msg = metaAlarm.Name.split(".");
                    msg = msg[1];

                    if (localData.homematicStringtable.MAINTENANCE && localData.homematicStringtable.MAINTENANCE[msg] && localData.homematicStringtable.MAINTENANCE[msg].text) {
                        msg = localData.homematicStringtable.MAINTENANCE[msg].text;
                    }

                    appendRow($this, ALDPs[i], metaDevice.Name, msg, localData.uiState['_' + ALDPs[i] + '.Timestamp'], metaAlarm.Operations);
                }
            }
        }

        buildServiceMessageTable();

        localData.uiState.bind('_41.Value', function (e, newVal, oldVal) {
            if (newVal != oldVal) setTimeout(buildServiceMessageTable, 5000);
        });

    }
};

</script>

<script type="text/ejs" id="tplHmServiceMsgs" class="dashui-tpl" data-dashui-set="homematic" data-dashui-name="homematic - service messages" data-dashui-attrs="">
    <div class="dashui-widget <%== this.data.attr('class') %>" style="top:0px; left: 0px; width: 360px; height: 200px;font-family: Verdana,Arial,sans-serif;font-size: 13px;" id="<%= this.data.attr('wid') %>">
        <div class="dashui-widget-body">
            <table class="homematic-servicemessages" id="msgs_<%= this.data.attr('wid') %>"></table>
        </div>
    </div>
    <div <%= (el) -> vis.binds.homematic.serviceMessages(el) %> />
</script>
