<?php
session_start();
if($_SESSION['logInSucces'] == NULL){
	?><script type="text/javascript">window.location.href="index"</script><?
}
require("../config.php");
require("../conexao.php");
?>
			<div class="content-box">
				<div class="content-config">
					<div class="header-conf">
						<p>Novo Usuario</p>
					</div>
					<div class="conteudo-config">
						<div class="input-group">
							<span>Nome</span>
							<input type="text"  value="" id="nome" placeholder="Digite um nome para este usuario" required="">
						</div>
						<div class="input-group">
							<span>E-mail</span>
							<input type="email"  value="" id="email" placeholder="Digite um e-mail para este usuario" required="">
						</div>
						<div class="input-group">
							<span>Senha</span>
							<input type="password" value="" id="pass" placeholder="Digite uma senha para este usuario" required="">
						</div>
						<div class="input-group">
							<span>Plano</span>
							 <select name="plan" id="plan">
							     <option value="0">Teste</option>
							<?php 
							    $sel = $con->prepare("SELECT * FROM `plans-md`");
							    $sel->execute();
							    while($dados = $sel->fetch(PDO::FETCH_ASSOC)){
							        ?>
							            <option value="<?=$dados['id']?>"><?=$dados['titulo']?></option>
							        <?
							    }
							?>
							</select>
							        
						</div>
						<div class="input-group">
							<span>Data Expirar</span>
							<input type="date" value="<?=date("Y-m-d")?>" id="date" required="">
						</div>
						<div class="input-group">
							<button class="cad-success">Cadastrar</button>
						</div>
					</div>
				</div>
			</div>	
<script type="text/javascript">
	$('.cad-success').click(function(){
		var nome = $('#nome').val();
		var email= $('#email').val();
		var pass = $('#pass').val();
		var plan = $('#plan').val();
		var date = $('#date').val();
		var nomel = $('#nome').val().length;
		var emaill= $('#email').val().length;
		var passl = $('#pass').val().length;

		if(nomel != 0){ $('#nome').css('border','1px solid rgba(0,0,0,.3)');
			if(emaill != 0){$('#email').css('border','1px solid rgba(0,0,0,.3)');
				if(passl != 0){$('#pass').css('border','1px solid rgba(0,0,0,.3)');
				$.ajax({
						  type: "POST",
						  url: 'function/api.php?action=addUsers',
						  data: {
						  	'nome' : nome,
						  	'email': email,
						  	'pass' : pass,
						  	'plan' : plan,
						  	'data' : date
							},
						  success: function(data,status){
						  	console.log(data);
						  	if(data == 'E1'){
						  		$('.status').css('display','block');
					  			$('.status').css('color','white');
					  			$('.status').css('background','rgba(215,50,50,1)');
						  		$('.status p').html('Email já existente.');
						  		status_close();
						  	}else if(data == 'S1'){
						  		$('.status').css('display','block');
						  		$('.status').css('color','white');
					  			$('.status').css('background','rgba(50,215,50,1)');
						  		$('.status p').html('Cadastrado Com Sucesso.');
						  		status_close();
						  	}
						  },
						  
						});
				}else{
					$('#pass').css('border','1px solid red');
				}
			}else{
				$('#email').css('border','1px solid red');
			}
		}else{
			$('#nome').css('border','1px solid red');
		}
				
				
	});	

</script>