<div id="result" class="notice_fixed" style="margin-top:0px;"></div>
<?
if( isset( $_POST['SnSave'] ) ) {
	if(!preg_match("/[A-Z]{2}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}/i", $CRMsn)){
		echo '<div id="result_check_sn" class="clr_high centered" style="margin-top:10px;">Ошибка! Неверный серийный номер</div>';
	}
}
?>
<form name="crm-sn" class="form-horisontal" style="display: block;" autocomplete="off" method="post">
	<div class="row">
		<div class="col-lg-4">
			<h3><?=Dict::_('TTLCRMSN')?></h3>
			<div class="panel panel-default" <? if($nCRMsn == '') { ?>style="background:rgba(224, 0, 0 , 0.1);border-color:rgba(224, 0, 0 , 0.2);"<? } ?> >
				<div class="panel-body">
					<div class="row">
						<div class="form-group col-lg-12">
							<div class="input-group">
								<span class="input-group-addon" title="<?=Dict::_('TTLCRMSN')?>"><i class="fa fa-fw fa-shield" aria-hidden="true"></i></span>
								<input type="text" class="form-control" style="text-align:center;" name="inputCRMsn" value="<? echo $nCRMsn; ?>" placeholder="ХХ-ХХХХХ-ХХХХХ-ХХХХХ" minlength="20" maxlength="20" required <?php if ($nCRMdateTo > $nCRMdateFrom) { ?>readonly<?php } ?>/>
								<span class="input-group-addon" title="<?=Dict::_('TXTUSERWORDSHOW')?>"><i style="cursor:pointer;" class="fa fa-fw fa-eye-slash" aria-hidden="true" onclick="$('input[name=inputCRMword]').attr('type','text');"></i></span>
							</div>
						</div>
						<div class="form-group col-lg-12" style="margin-bottom: 0px;">
							<div class="input-group">
								<span class="input-group-addon" title="<?=Dict::_('TXTUSERWORD')?>"><i class="fa fa-fw fa-key" aria-hidden="true"></i></span>
								<input type="password" class="form-control" name="inputCRMword" value="<? echo $nCRMuserword; ?>" placeholder="<?=Dict::_('TXTUSERWORD')?>" readonly onfocus="this.removeAttribute('readonly')" required />
								<input type="hidden" name="inputCRMdate" value="" />
								<span class="input-group-btn">
									<button class="btn btn-primary" type="button" name="SnSave"><?=Dict::_('CHECK')?></button>
								</span>
							</div>
						</div>
						<div class="clear"></div>

					</div>
				</div>
			</div>
			<h3><?=Dict::_('TTLCRMREG1')?></h3>
			<div class="panel panel-default" <? if($nCRMusername == '' or $nCRMuserphone == '' or $nCRMuseremail == '') { ?>style="background:rgba(224, 0, 0 , 0.1);border-color:rgba(224, 0, 0 , 0.2);"<? } ?>>
				<div class="panel-body">
					<div class="row">
						<div class="form-group col-lg-12">
							<div class="input-group">
								<span class="input-group-addon" title="<?=Dict::_('TXTUSERNAME')?>"><i class="fa fa-fw fa-user" aria-hidden="true"></i></span>
								<input type="text" onchange="userrule();" onkeyup="userrule();" onclick="userrule();" class="form-control" name="inputCRMuname" value="<? echo $nCRMusername; ?>" placeholder="<?=Dict::_('TXTUSERNAME')?>" required />
							</div>
						</div>
						<div class="form-group col-lg-12">
							<div class="input-group">
								<span class="input-group-addon" title="<?=Dict::_('TXTUSERPHONE')?>"><i class="fa fa-fw fa-phone" aria-hidden="true"></i></span>
								<input type="text" class="form-control" name="inputCRMuphone" value="<? echo $nCRMuserphone; ?>" placeholder="<?=Dict::_('TXTUSERPHONE')?>" required />
							</div>
						</div>
						<div class="form-group col-lg-12" style="margin-bottom: 0px;">
							<div class="input-group">
								<span class="input-group-addon" title="<?=Dict::_('TXTUSERMAIL')?>"><i class="fa fa-fw fa-at" aria-hidden="true"></i></span>
								<input type="email" class="form-control" name="inputCRMumail" value="<? echo $nCRMuseremail; ?>" placeholder="<?=Dict::_('TXTUSERMAIL')?>" required />
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="col-lg-8">
			<h3><?=Dict::_('TTLCRMRUL')?></h3>
			<div class="panel panel-default">
				<div class="panel-body" style="padding-bottom:7px;">
					<div class="form-group" style="margin-bottom: 0px;">
						<iframe src="/<? echo $folder; ?>/admin/crm-rules.php" frameborder="0" width="100%" height="250px"></iframe>
						<input id="chkCRMrul_1" type="checkbox" value="accept" name="checkboxCRMrules" <? if($nCRMrules == 'accept'){ echo 'checked'; } ?> required /><label for="chkCRMrul_1"><?=Dict::_('TXTCRMRUL')?></label>
						<input id="chkCRMrul_2" type="checkbox" value="accept" name="checkboxCRMrules" <? if($nCRMrules == 'accept'){ echo 'checked'; } ?> required /><label for="chkCRMrul_2"><?=Dict::_('TXTCRMRUL2')?></label>
					</div>
				</div>
			</div>
		</div>
	</div>
</form>
		
<script>
	function checksn(){
		var fd 		= new FormData();
		var snum 	= $("input[name=inputCRMsn]").val();
		var domain 	= document.location.hostname;
		var word 	= $("input[name=inputCRMword]").val();;
		fd.append('sn', snum);
		fd.append('domain', domain);
		fd.append('word', word);
		$.ajax({
            url: 'https://muststyle.ru/_workbox/wb_check.php',
            type: 'POST',                        
            data: fd,
			contentType: false,
			processData: false,
			beforeSend: function(){
				//loader();
			},
            success: function(response) {
                var result = JSON.parse(response);
				$("input[name=inputCRMuname]").val(result.user);
				$("input[name=inputCRMuphone]").val(result.phone);
				$("input[name=inputCRMumail]").val(result.email);
				$("input[name=inputCRMword]").val(result.word);
				$("input[name=inputCRMsn]").val(result.sn);
				$("input[name=inputCRMdate]").val(result.date);
				if(result.status == "ok"){
					$("input[name=inputCRMsn]").parent().children('.input-group-addon').css('color','rgba(63,163,63, 1.0)');
					$("button[name=SnSave]").attr('class','btn btn-success').attr('type','submit').html('<?=Dict::_('SAVE2')?>');
					$("input[name=inputCRMword], input[name=inputCRMuphone], input[name=inputCRMumail]").attr('readonly','readonly');
				} else {
					$("#result").html("Ошибка! Данный серийный номер не зарегистрирован в системе!").fadeIn(800).css('color','#e00000');
					$("input[name=inputCRMsn]").parent().children('.input-group-addon').css('color','#e00000');
					$("input[name=inputCRMsn]").removeAttr('readonly','');
				}
                
            },
			error: function(xhr) {
				
			}
        });
	}
	$('button[name=SnSave]').click(function(){
		checksn();
	});
</script>
