<?php namespace AloVoice; ?>
<style>
	.alv_table_lines .nowrp {
		white-space: nowrap;
	}
	.loadingline{
		display:none;
	}
</style>
<center><h3 class="h3 card-body">GSM линии </h3> <div class="loadingline"></div><a href="#" onClick="updateDongleAccounts()" id="dongle_ussd_send_btn" class="btn btn-primary title_line_button"><i class="fa fa-phone"></i>  Проверить баланс номеров</a></center>
<br/>

<table class="table" id="stats_block_table">
	<thead>
		<tr>
			<th>Порт</th>
			<th>Номер</th>
			<th>Статус</th>
			<th>IMEI</th>
			<!--th>Последнее USSD</th-->
			<th>Инфо</th>
			<th>Действие</th>
		</tr>
	</thead>
	<tbody id="chdongles_table_body"> </tbody> 
</table>

<script>

var afterLoadPage = function(){
	var updatingTm = false;
	var dn=0;
	var dnc=1;
	window.updateDongleOneAccount = function(device,numcode){
		loadAloVoiceInfo(
			{"restmethod":"alovoice_update_dongles_accounts","device":device,"numcode":numcode},
			function(res){
				console.log("AloVoice GSM dongle ACC RES:",res);
			}
		)
	};

	window.updateDongleAccounts = function(){
		console.log("ChLdPg:",window.loadedpage);
		updatingTm = true;
		var dnglLngth = $(".alv_table_lines").length;
		$(".loadingline").show().text("Отправка USSD №"+dnc);
		$("#dongle_ussd_send_btn").hide();
		console.log("AloVoice GSM dongle UPDATING...",dnglLngth, dnc);
		$(".alv_table_lines").each(function(ind){
			if(ind==dn){
				var device = $(this).attr("data-device");
				var numcode = $(this).attr("data-numcode");
				loadAloVoiceInfo(
					{"restmethod":"alovoice_update_dongles_accounts","device":device,"numcode":numcode},
					function(res){
						console.log("AloVoice GSM dongle ACC RES:",res);
						if(dnc==dnglLngth){
							console.log("accounting DONE!");
							setTimeout(afterLoadPage,10000);
							$(".loadingline").hide();
							$("#dongle_ussd_send_btn").show();
							updatingTm = false;
							dn=0;
							dnc=1;
						} else {
							setTimeout( updateDongleAccounts,2000);
							dn++;
							dnc++;
						}
					}
				);
				
				
			}
		});

	};
	
	var donglesUpdate = function(){
	
		if(updatingTm == true){ return; }
		loadAloVoiceInfo("alovoice_dongle_channels",function(res){
			console.log("AloVoice GSM dongles RES:",res);

			if(typeof res.gsmlines != 'undefined' ){
				$("#chdongles_table_body").html('');
				res.gsmlines.forEach(function(val){
					console.log("Each ACC:",val);
					var statusLine = (val.datas[2]=="free") ? '<span style="color:green;">'+val.datas[2]+'</span>' : val.datas[2];
					var statusLine = (statusLine=="dialing") ? '<span style="color:yellow;">'+statusLine+'</span>' : statusLine;
					var statusLine = (statusLine=="outgoing") ? '<span style="color:blue;">'+statusLine+'</span>' : statusLine;
					//var statusLine = (val.datas[2]=="dialing") ? '<span style="color:yellow;">'+val.datas[2]+'</span>' : val.datas[2];
					
					var lineHTML = '<tr class="alv_table_lines" id="dng_line_'+val.datas[1]+'" data-device="'+val.datas[1]+'" data-numcode="'+val.datas[5].substr(-9,2)+'">'
						+'<td class="nowrp" > <span>'+val.datas[1]+'</span></td>' //<i class="fa fa-telegram tgcolor"></i>
						+'<td class="nowrp" > <b>'+val.datas[5].substr(-9,2)+' '+val.datas[5].substr(-7)+'</b></td>'
						+'<td class="nowrp" >'+statusLine +'</td>'
						+'<td class="nowrp" >'+val.datas[3]+'</td>'
						+'<td>'+val.ussd+'</td>'
						+'<td class="nowrp" ><a href="#" onClick="updateDongleOneAccount(\''+val.datas[1]+'\',\''+val.datas[5].substr(-9,2)+'\');"><i class="fa fa-money"></i> Проверить</a></td>'
						+'</tr>';
					
					$("#chdongles_table_body").append(lineHTML);
				});
			}
			
			setTimeout(donglesUpdate,10000);
		}); 
	}
	
	donglesUpdate();
	//setInterval(donglesUpdate,10000);
};

</script>