<%
	local util = require "luci.util"
	local fs = require "nixio.fs"
	local sys = require "luci.sys"
	local http = require "luci.http"
	local dispatcher = require "luci.dispatcher"
	local uci = require "luci.model.uci".cursor()

	local devv = tostring(uci:get("sms_tool", "general", "readport"))
	local smsmem = tostring(uci:get("sms_tool", "general", "storage"))
	local sms = tostring(luci.sys.exec("sms_tool -s" .. smsmem .. " -d " .. devv .. " -f '%Y-%m-%d %H:%M' -j recv 2>/dev/null"))
	local smsmer = tostring(uci:get("sms_tool", "general", "mergesms"))
	local smscuta = string.sub (sms, 8)
	local smscut = smscuta:sub(1, #smscuta - 2)
	local statusb = luci.util.exec("sms_tool -s" .. smsmem .. " -d ".. devv .. " status")
	local all = statusb:match('[^: ]+$')
	local location = ""
	local l = string.sub (statusb, 15, 16)
	if l == "SM" then
      		location = translate("SIM card")
 	end
	if l == "ME" then
      		location = translate("Modem memory")
 	end
-%>
	
<%+header%>

<!--
-- Copyright 2020-2022 Rafał Wabik (IceG) - From eko.one.pl forum
-- Licensed to the GNU General Public License v3.0.
-->

<style>

table {
  border-collapse: collapse;
  width: 100%;
}

th, td {
  border-bottom: 1px solid var(--border-color-medium);
  font-size: 12px;
  padding: 10px;
  text-align: justify;
  display: table-cell;
  vertical-align: top;
}

td input[type="checkbox"] {
    float: left;
    margin: 0 auto;
    width: 40%;
}

tr:nth-child(odd) {background-color: var(--background-color-medium)}

</style>

<script type="text/javascript">//<![CDATA[

	window.onload = function readSMS() {

			XHR.get('<%=luci.dispatcher.build_url("admin", "modem", "sms", "readsim")%>', null,
			function(x, sim)
			{
			var vn = parseInt(sim.use) || 0;
			var mn = parseInt(sim.all) || 100;
			var pc = Math.floor((100 / mn) * vn);
			document.getElementById('usse').innerHTML=sim.use  + " / " + sim.all + " " + '('+ pc + '%)';
			}
			);

			var msgm = <%=smsmer%>;
			if ( msgm == "0" ) {

			var array = <%=smscut%>;
			var sortedData = array .sort((function (a, b) { return new Date(b.timestamp) - new Date(a.timestamp) }));
			var table = document.getElementById("smsTable");
			var data = JSON.stringify(sortedData);
			var json = JSON.parse(data);

			var x = <%=all%>;
        		for (var d = 0; d < json.length; d++) {

            					row = table.insertRow(-1);
  						var cell1 = row.insertCell(0);
  						var cell2 = row.insertCell(0);
  						var cell3 = row.insertCell(0);
  						var cell4 = row.insertCell(0);
						cell4.innerHTML = "<input type='checkbox' name='smsn' id="+"btn"+json[d].index+" /><img src='<%=resource%>/icons/delsms.png'>"
 				 		cell3.innerHTML = json[d].sender;
  						cell2.innerHTML = json[d].timestamp;
    						cell1.innerHTML = json[d].content;	
				}
			}

			if ( msgm == "1" ) {

			var array = <%=smscut%>;
			var sortedData = array .sort((function (a, b) { return new Date(b.timestamp) - new Date(a.timestamp) }));
			var table = document.getElementById("smsTable");

			var MergeMySMS = sortedData;
    			result = [];

			MergeMySMS.forEach(function (o) {
    			if (!this[o.sender]) {
    			if(o.part > 0){
        		this[o.sender] = { index: o.index, sender: o.sender, timestamp: o.timestamp, part: o.part, total: o.total, content: o.content, contentparts: [] };
        		this[o.sender].contentparts[o.part] = o.content;
        		}else{
        		this[o.sender] = { index: o.index, sender: o.sender, timestamp: o.timestamp, part: o.part, total: o.total, content: o.content};
        		}
        		result.push(this[o.sender]);
        		return;
    			}
			if (this[o.sender].total == o.total && this[o.sender].timestamp == o.timestamp && this[o.sender].sender == o.sender && this[o.sender].part > 0) {
    			this[o.sender].index += '-' + o.index;
            		this[o.sender].contentparts[o.part] = o.content;}
			else {
			this[o.sender] = { index: o.index, sender: o.sender, timestamp: o.timestamp, part: o.part, total: o.total, content: o.content };
        		result.push(this[o.sender]);
        		return;
			}
			}, Object.create(null));
			result.forEach(function(o) {
                		if(o.contentparts){
                    		o.contentparts.shift();
                    		o.content = o.contentparts.join('');
                		}
			});

			var data = JSON.stringify(result);
			var json = JSON.parse(data);

			var x = <%=all%>;
        		for (var d = 0; d < json.length; d++) {

            					row = table.insertRow(-1);
  						var cell1 = row.insertCell(0);
  						var cell2 = row.insertCell(0);
  						var cell3 = row.insertCell(0);
  						var cell4 = row.insertCell(0);
						cell4.innerHTML = "<input type='checkbox' name='smsn' id="+"btn"+json[d].index+" /><img src='<%=resource%>/icons/delsms.png'>"
 				 		cell3.innerHTML = json[d].sender;
  						cell2.innerHTML = json[d].timestamp;
    						cell1.innerHTML = json[d].content;
				}
			}
	}


function refreshSMS() {
window.location.reload();
}

function toggle(source) {
    var checkboxes = document.querySelectorAll('input[type="checkbox"]');
    for (var i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i] != source)
            checkboxes[i].checked = source.checked;
    }
}


function delete_new()
{
if (document.querySelectorAll('input[name="smsn"]:checked').length === document.querySelectorAll('input[name="smsn"]').length) {

            	if (confirm("<%:Delete all the messages?%>")) {
		var rowCount = smsTable.rows.length;
		document.getElementById('usse').innerHTML = "0"  + " / " + <%=all%>;
		for (var i = rowCount - 1; i > 0; i--) {
            		smsTable.deleteRow(i);}

		XHR.get('<%=luci.dispatcher.build_url("admin", "modem", "sms", "delete_all")%>/' , null,

			function()
			{
			document.getElementById('usse').innerHTML = "0"  + " / " + <%=all%> + " " + '(0%)';
			}
		);

		XHR.get('<%=luci.dispatcher.build_url("admin", "modem", "sms", "countsms")%>/' , null,

			function()
			{
			}
		);
		return false;
		}


        } else {

		if (document.querySelectorAll('input[name="smsn"]:checked').length == 0){
    		alert('<%:Please select the message(s) to be deleted%>');   
		} else {

                var items = document.getElementsByName("smsn");
                var selectedItems = "";
                for (var i = 0; i < items.length; i++) {
                    if (items[i].type == "checkbox" && items[i].checked == true) selectedItems += items[i].id + "\n";
                }
		  smsindex = selectedItems.replace(/\s/g, '');

	if (confirm("<%:Delete selected message(s)?%>")) {

		XHR.get('<%=luci.dispatcher.build_url("admin", "modem", "sms", "delete_one")%>/' + smsindex, null,

			function()
			{
       			var table = document.getElementById("smsTable");
  			var index = 1;
  			while (index < table.rows.length) {

    			var input = table.rows[index].cells[0].children[0];
      			if (input && input.checked) {
      			table.deleteRow(index);
    			}
    			else {
      			index++;
  			}
        		}

		XHR.get('<%=luci.dispatcher.build_url("admin", "modem", "sms", "readsim")%>', null,
			function(x, sim)
			{
			var vn = parseInt(sim.use) || 0;
			var mn = parseInt(sim.all) || 100;
			var pc = Math.floor((100 / mn) * vn);
			document.getElementById('usse').innerHTML=sim.use  + " / " + sim.all + " " + '('+ pc + '%)';

			}
			);
			}

		);

		XHR.get('<%=luci.dispatcher.build_url("admin", "modem", "sms", "countsms")%>/' , null,

			function()
			{
			}
		);
		return false;
		}
        }
	}
}

//]]></script>

<h2><%:SMS Messages%></h2>
	<div class="cbi-map-descr"><%:Web UI for receiveling messages via sms_tool. More information about the sms-tool on the%> <a href="https://eko.one.pl/?p=openwrt-sms_tool" target="_blank"><%:eko.one.pl forum%></a>.</div>
	<p></p>
	<h4><%:Received Messages%></h4>
	<div class="table" width="100%">
	<div class="tr"><div class="td left" width="33%"><%:Messages store in%>:</div><div class="td left"><%=location%></div></div>
	<div class="tr"><div class="td left" width="33%"><%:Messages (Inbox / Maximum)%>:</div><div class="td left" id="usse"></div></div>
	</div>

	<table id="smsTable" width="100%" class="table">
  	<tr class="tr cbi-rowstyle-2">

    	<td class="td left" width="7%"><input type='checkbox' id='allcheck' onclick='toggle(this);'/></td>
    	<td class="td left" width="11%"><strong><%:From%></strong></td>
    	<td class="td left" width="15%"><strong><%:Received%></strong></td>
    	<td class="td center" width="66%" ><strong><%:Message%></strong></td>

 	</tr>
	</table>

	<div class="table" width="100%" >
    	<div class="tr cbi-rowstyle-2">
	
	<div class="td right" style="width:100%;">
	<div style="float: left;"></div>
	<input type="button" id="delbtn" class="btn cbi-button cbi-button-neutral" value="<%:Refresh SMS%>" onclick="refreshSMS()" />&nbsp;&nbsp;&nbsp;<input type="button" id="delabtn" class="btn cbi-button cbi-button-neutral" value="<%:Delete message(s)%>" onclick="delete_new()" />

	</div>
	</div>

<%+footer%>
