<div class="alvtop_header" id="alv_mainblock"> <?php //card?>
	<center>
		<h3 class="h3 card-body">Интеграции</h3>
		<div class="container" id="integrationsblock">
			<table class="table" id="stats_block_table">
				<thead>
					<tr>
						<th>Интеграция</th>
						<th>Статус</th>
						<th>Действие</th>
					</tr>
				</thead>
				<tbody id="integrations_table_body"></tbody> 
			</table>
		</div>
	</center>

</div>
<script>
var loadAlovoiceStatusLine = function(method,cbk){
	console.log(method+" BEGIN... ");
	$.ajax({
		url: location.origin+location.pathname, 
		dataType: "json",
		data: {
			restmethod:(method) ? method : 'status_integration'
		},
		success: function( result ){
			console.log(method+" RES: " , result);
			if(result.lines){
				result.lines.forEach(function(itg){
					
					if(typeof itg.code !== 'undefined' ){
						$("#"+itg.code).remove();
						$("#integrations_table_body").append(getStatusLineHtml(itg));
					}
				});
			}
			if(typeof result.nextintegration !== 'undefined' ){
				loadAlovoiceStatusLine(result.nextintegration);
			}
			if(typeof cbk !== 'undefined'){
				cbk(result);
			}
		
		}
	});
}

// BX24.callMethod(
   // 'crm.activity.add',
   // {
      // fields:
         // {
            // "OWNER_TYPE_ID": 2,
            // "OWNER_ID": 882,
            // "PROVIDER_ID": 'REST_APP',
            // "PROVIDER_TYPE_ID": 'ecpsign',
            // "SUBJECT": "Документ подписан",
            // "COMPLETED": "Y",
            // "RESPONSIBLE_ID": 1,
            // "DESCRIPTION": "Подписанный документ: Договор (SOFT) PRO-23"
         // }
   // },
   // function(result)
   // {
      // if(result.error())
         // alert("Error: " + result.error());
      // else
      // {
         // alert("Success: " + result.data());
      // }
   // }
// );
// BX24.callMethod(
   // 'crm.activity.add',
   // {
      // fields:
         // {
            // "OWNER_TYPE_ID": 2,
            // "OWNER_ID": 882,
            // "PROVIDER_ID": 'REST_APP',
            // "PROVIDER_TYPE_ID": 'ecpsign',
            // "SUBJECT": "Подписан документ: Договор (SOFT) PRO-23",
            // "COMPLETED": "Y",
            // "RESPONSIBLE_ID": 1,
            // "DESCRIPTION": '[2198]',
         // }
   // },
   // function(result)
   // {
      // if(result.error())
         // console.log("Error: " , result.error());
      // else
      // {
         // console.log("Success: " , result.data());
      // }
   // }
// );


var start_integration = function(code){
	//console.log("integration_on code:" , code);
	loadAloVoiceInfo(
		{"restmethod":"integration_on","code":code},
		function(res){
			console.log("integration_on RES:" , code, res);
			if(res.result && res.result == "success"){
				var iTitle = $('#title_'+code).text();
				$("#"+code).remove();
				var ingHtml = getStatusLineHtml({code:code,title:iTitle,status:'on'});
				console.log("itg HTML:",ingHtml);
				$("#integrations_table_body").append(ingHtml);
			}
		}
	);
	
};

var stop_integration = function(code){
	aloModal({
		label: 'Отключения интеграции',
		body: 'Вы уверены отключить - "'+$('#title_'+code).text()+'"?',
		btn: 'Отключить',
		callback:function(){
			$.ajax({
				url: location.origin+location.pathname, 
				dataType: "json",
				data: {
					restmethod:'integration_off',
					code:code
				},
				success: function( result ){
					console.log("integration_off RES: " , code, result);
					if(result.result && result.result == "success"){
						var iTitle = $('#title_'+code).text();
						$("#"+code).remove();
						var ingHtml = getStatusLineHtml({code:code,title:iTitle,status:'off'});
						console.log("itg HTML:",ingHtml);
						$("#integrations_table_body").append(ingHtml);
					}
				}
			});
		}
	});

	
};

var getStatusLineHtml = function(itg){
	var stCode = (typeof itg.code !== 'undefined' ) ? itg.code : 'Code';
	var stTitle = (typeof itg.title !== 'undefined' ) ? itg.title : 'Title';
	var stStatus = (typeof itg.status !== 'undefined' ) ? itg.status : 'Status';
	
	
	var stText = (stStatus=="off") ? '<span class="badge badge-secondary">Отключён</span>' : '<span class="badge badge-success">Включён</span>';
	
	var stLineHtml = '<tr id="'+stCode+'">';
	stLineHtml += '<td id="title_'+stCode+'">'+stTitle+'</td>';
	stLineHtml += '<td>'+stText+'</td>';
	if(stStatus=="off"){
		stLineHtml += '<td><div onclick="start_integration(\''+stCode+'\');" class="btn btn-success">Вкл</div></td>';
	} else {
		stLineHtml += '<td><div onclick="stop_integration(\''+stCode+'\');" class="btn btn-danger">Выкл</div></td>';
	}
	
	stLineHtml += '</tr>';
	return stLineHtml;
}

var afterLoadPage = function(reload){
	console.log("Integrations page running...");
	loadAlovoiceStatusLine(); //'status_integration'
};


</script>