<?php namespace AloVoice; ?>
<style>
	.alv_table_lines .nowrp {
		white-space: nowrap;
	}
	.loadingline{
		display:none;
	}
</style>
<center>
	<h3 class="h3 card-body">Линии</h3> 
	<div class="loadingline"></div>
	<!---a href="#" onClick="addNewTrunk()" id="trunk_add_button" class="btn btn-primary title_line_button"><i class="fa fa-arrow-down"></i>  + Добавить </a--->
</center>
<br/>

<table class="table" id="stats_block_table">
	<thead>
		<tr id="alvtrunks_table_header"></tr>
	</thead>
	<tbody id="alvtrunks_table_body"> </tbody> 
</table>

<script>

var afterLoadPage = function(){
	var isHeadered = 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 trunksUpdate = function(){
	
		doAloVoiceCmd('loadTrunksList', {}, function(res) {
         
			console.log("AloVoice Trunks RES:",res);
			
			if(!isHeadered){
				if(typeof res.headers != 'undefined' ){
					isHeadered = true;
					$("#alvtrunks_table_header").html('');
					
					$.each( res.headers, function( key, value ) {					
						console.log("Each Header:",key, value);
						$("#alvtrunks_table_header").append('<th id="hd_'+key+'">'+value+'</th>');
					});
				}
			}
			
			if(typeof res.datas != 'undefined' ){
				$("#alvtrunks_table_body").html('');
				$.each( res.datas, function( tkey, trunk ) {
					console.log("Each Trunk:",trunk);
					/*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];
					*/
					let status = (trunk.status == 'On') ? '<span class="active_tg_status badge badge-success">Активный</span>' : '<span class="off_tg_status badge badge-secondary">Выключен</span>';
					var lineHTML = '<tr class="alv_table_lines" id="trk_line_'+trunk.id+'" data-device="'+trunk.type+'" data-numcode="'+trunk.name+'">'
						+'<td class="nowrp" > <span>'+trunk.id+'</span></td>'
						+'<td class="nowrp" > <b>'+trunk.name+'</b></td>'
						+'<td class="nowrp" >'+trunk.type +'</td>'
						+'<td>'+trunk.number+'</td>'
						+'<td>'+trunk.maxchannels+'</td>'
						+'<td>'+status+'</td>'
						+'<td class="nowrp" ><a href="#" onClick="updateOneTrunk(\''+trunk.id+'\');"><i class="fa fa-money"></i> Настройки</a></td>'
						+'</tr>';
					
					$("#alvtrunks_table_body").append(lineHTML);
				});
			}
			
			//setTimeout(donglesUpdate,100000);
		}); 
		
		// loadAloVoiceInfo("alovoice_trunks_list",function(res){
			// console.log("AloVoice Trunks RES:",res);
			
			// if(!isHeadered){
				// if(typeof res.headers != 'undefined' ){
					// isHeadered = true;
					// $("#alvtrunks_table_header").html('');
					
					// $.each( res.headers, function( key, value ) {					
						// console.log("Each Header:",key, value);
						// $("#alvtrunks_table_header").append('<th id="hd_'+key+'">'+value+'</th>');
					// });
				// }
			// }
			
			// if(typeof res.datas != 'undefined' ){
				// $("#alvtrunks_table_body").html('');
				// $.each( res.datas, function( tkey, trunk ) {
					// console.log("Each Trunk:",trunk);
					// /*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];
					// */
					// let status = (trunk.status == 'On') ? '<span class="active_tg_status badge badge-success">Активный</span>' : '<span class="off_tg_status badge badge-secondary">Выключен</span>';
					// var lineHTML = '<tr class="alv_table_lines" id="trk_line_'+trunk.id+'" data-device="'+trunk.type+'" data-numcode="'+trunk.name+'">'
						// +'<td class="nowrp" > <span>'+trunk.id+'</span></td>'
						// +'<td class="nowrp" > <b>'+trunk.name+'</b></td>'
						// +'<td class="nowrp" >'+trunk.type +'</td>'
						// +'<td>'+trunk.number+'</td>'
						// +'<td>'+trunk.maxchannels+'</td>'
						// +'<td>'+status+'</td>'
						// +'<td class="nowrp" ><a href="#" onClick="updateOneTrunk(\''+trunk.id+'\');"><i class="fa fa-money"></i> Проверить</a></td>'
						// +'</tr>';
					
					// $("#alvtrunks_table_body").append(lineHTML);
				// });
			// }
			
			// //setTimeout(donglesUpdate,100000);
		// }); 
	}
	
	trunksUpdate();
};

</script>