<?php namespace AloVoice; ?>
<center><h3 class="h3 card-body">Настройки</h3></center>

<form  id="alv_settings_form">
	<div class="form-row">
		<div class="form-group col-md-12">
			<label for="asterisk_host">IP или доменный Адрес Менеджера Астериск (AMI)</label>
			<input type="text" class="form-control form-control" id="asterisk_host" placeholder="" value="">
		</div>
	</div>
	
	
	<div class="form-row">
		<div class="form-group col-md-12">
			<label for="asterisk_port">Порт Менеджера Астериск (AMI)</label>
			<input type="text" class="form-control form-control" id="asterisk_port" placeholder="" value="">
		</div>
	</div>
  
	<div class="form-row">
		<div class="form-group col-md-12">
			<label for="asterisk_user">Пользователь Менеджера Астериск (AMI)</label>
			<input type="text" class="form-control form-control" id="asterisk_username" placeholder="" value="">
		</div>
	</div>
  
	<div class="form-row">
		<div class="form-group col-md-12">
			<label for="asterisk_pass">Пароль к Менеджеру Астериск (AMI)</label>
			<input type="password" class="form-control form-control" id="asterisk_secret" placeholder="" value="">
		</div>
	</div>  
	<div class="form-row">
		<div class="form-group mx-2">
			<label class="" for="asterisk_pass">Диапазон номеров для автоназначения сотрудникам</label>
		</div>
		<div class="form-group form-row">
			<div class="col-3">
				<input type="number" class="form-control" id="unumbers_from" placeholder="" value="">
			</div>
			<div class="col-1 text-center"> - </div>
			<div class="col-3">
				<input type="number" class="form-control" id="unumbers_to" placeholder="" value="">
			</div>
		</div>
	</div> 
	
	<div class="form-row">  
		<div class="form-group mx-2">
			<label class="" for="sipaddress_local">SIP адрес и порт (локальный)</label>
		</div>
		<div class="form-group form-row">
			<div class="col-4">
				<input type="text" class="form-control" id="sipaddress_local" placeholder="" value="">
			</div>
			<div class="col-1 text-center"> - </div>
			<div class="col-2">
				<input type="text" class="form-control" id="sipport_local" placeholder="" value="">
			</div>
		</div>
	</div>
	<div class="form-row">
		<div class="form-group mx-2">
			<label class="" for="sipaddress_out">SIP адрес и порт (внешний)</label>
		</div>
		<div class="form-group form-row">
			<div class="col-4">
				<input type="text" class="form-control" id="sipaddress_out" placeholder="" value="">
			</div>
			<div class="col-1 text-center"> - </div>
			<div class="col-2">
				<input type="text" class="form-control" id="sipport_out" placeholder="" value="">
			</div>
		</div>
	</div>
	<div class="form-row">
		<div class="form-group mx-2">
			<label class="" for="miscallagentname">Кто ОТВЕТСТВЕННЫЙ по умолчанию: </label>
		</div>
		<div class="form-group">
			<input class="form-control" type="text" id="miscallagentname" disabled>
			<input type="hidden" id="miscallagentid">
		</div>
		<div class="form-group">
			<div class="btn btn-primary mb-2" onclick="BX24.selectUser(selectmiscallagentid)">Выбрать...</div>
		</div>
	</div>
	

	
	<div class="form-group row">
		<div class="form-group ml-4">
		 	 <div class="form-check">
				<input class="form-check-input" type="checkbox" id="showcallcard">
				<label class="form-check-label" for="showcallcard">Запускать окно обработки звонка</label>
		  </div>
		</div>
  	</div>
	
	<!--div class="form-group row">
		<div class="form-group ml-4">
		 	 <div class="form-check">
				<label class="form-check-label" for="autocreateentity">Авто создание лида при вызове</label>
				<select id="autocreateentity">
					<option value="0" selected>Не создовать</option>
					<option value="1">Авто создание ЛИДа при вызове</option>
					<option value="2">Авто создание СДЕЛКи при вызове</option>
					<option value="3">Авто создание КОНТАКТа при вызове</option>
				</select>
		  </div>
		</div>
  	</div-->
	
	<div class="form-group row">
		<div class="form-group ml-4">
		 	 <div class="form-check">
				<input class="form-check-input" type="checkbox" id="autocreatelead">
				<label class="form-check-label" for="autocreatelead">Авто создание лида при вызове</label>
		  </div>
		</div>
  	</div>
	
	<div class="form-group row">
		<div class="form-group ml-4">
		 	 <div class="form-check">
				<input class="form-check-input" type="checkbox" id="add_to_chat">
				<label class="form-check-label" for="add_to_chat">Оповещать о всех звонках в чате</label>
		  </div>
		</div>
  	</div>
  
	
	<br>
	<a href="#" class="btn btn-primary" id="save_alovoice_settings">Сохранить</a>
</form>
<script>
//-lg
var afterLoadPage = function(){
	window.selectmiscallagentid = function(user){
		if(typeof user !== 'undefined' && typeof user.id !== 'undefined' && typeof user.name !== 'undefined'){
			$("#miscallagentid").val(user.id);
			$("#miscallagentname").val(user.name);
		}
	}
	
	loadAloVoiceInfo("alovoice_settings",function(res){
		/*
		// 'host' => $this->config["asterisk_host"],
		// 'port' => $this->config["asterisk_port"],
		// 'username' => $this->config["asterisk_username"],
		// 'secret' => $this->config["asterisk_secret"]
		*/
		console.log("AloVoice settings RES:",res);
		
		$.each(res,function(ks,vs){
			if($("#"+ks)){
				if($("#"+ks).prop("tagName")=="SELECT"){
					$("#"+ks).val(vs).change();
				}
				else if($("#"+ks).attr('type')=="checkbox"){
					$("#"+ks).prop('checked',(vs=="true") ? true : false);
				}
				else{
					$("#"+ks).val(vs);
				}
			}
		});
		
		// if(typeof res.host !== 'undefined' ){ $("#asterisk_host").val(res.host); }
		// if(typeof res.port !== 'undefined' ){ $("#asterisk_port").val(res.port); }
		// if(typeof res.username !== 'undefined' ){ $("#asterisk_user").val(res.username); }
		// if(typeof res.secret !== 'undefined' ){ $("#asterisk_pass").val(res.secret); }
		
		// if(typeof res.unumbers_from !== 'undefined' ){ $("#unumbers_from").val(res.unumbers_from); }
		// if(typeof res.unumbers_to !== 'undefined' ){ $("#unumbers_to").val(res.unumbers_to); }
		
		$("#save_alovoice_settings").click(function(){
			
			var saveElements = {};
			var last = $("input").length-1;
			
			$.each($("select"),function(ind,obj){
				console.log("Check VAL:",$(obj).attr("id"), "[",$(obj).val(),"]");
				saveElements[$(obj).attr("id")] = $(obj).val();
			});
			
			$.each($("input"),function(ind,obj){
				
				var iVal = ($(obj).attr('type')=="checkbox") ? $(obj).prop('checked') : $(obj).val();
				
				console.log("Check VAL:",$(obj).attr("id"), "[",iVal,"]");
				saveElements[$(obj).attr("id")] = iVal;
				
				
				if(last == ind){
					console.log("Finish: ",saveElements);
					saveElements["restmethod"] = "savealovoicesettings";
					loadAloVoiceInfo(saveElements,function(res){
						console.log("Save RES: ",res);
						$("#alv_settings_form").prepend('<div class="alert alert-success" id="savesuccesbox" role="alert">Настройки сохранены!</div>');
						setTimeout(function(){
							$("#savesuccesbox").remove();
						},3000);
					});
				}
			});
		});
		
	}); 
}
</script>