<?php
	session_start();
	if (isset($_SESSION['acesso_admin'])){
		$acesso_idusuario=$_SESSION['acesso_idusuario'];
		$acesso_idempresa=$_SESSION['acesso_idempresa'];
		$acesso_admin=$_SESSION['acesso_admin'];
		$acesso_revenda=$_SESSION['acesso_revenda'];
		include ('../Amfphp/Services/conexao.php');
		include('funcoes.php');
		$con = new conexao();
		$dia=date('d');
		$mes=date('m');
		$ano=date('Y');
		$dataatual=date('d/m/Y');
		$horaatual=date('h:i');	
	} else {
		echo "<script>window.location='index.php';</script>";
	}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CSPADM Mobile - Clientes</title>
<link href="cspadm.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="funcoes.js"></script>
</head>

<body>
<form id="form1" name="form1" method="post" action="?oqfazer=salvar">
  <table width="410" border="1">
    <tr class="txtAlertaBOX">
      <td colspan="2" bgcolor="#FFFFCC"><a href="menu_mobile.php">(&lt;-- Voltar)</a> <br />
      CSPADM Mobile - Cadastro de Clientes</td>
    </tr>
    <tr class="txtBOX">
      <td width="149">Nome</td>
      <td width="245"><label for="edt_nome"></label>
    <input name="edt_nome" type="text" id="edt_nome" size="30" /></td></tr>
    <tr class="txtBOX">
      <td>E-mail</td>
      <td><label for="edt_email"></label>
      <input name="edt_email" type="text" id="edt_email" size="30" /></td>
    </tr>
    <tr class="txtBOX">
      <td>Dia Vencimento:</td>
      <td><label for="edt_dia"></label>
      <input name="edt_dia" type="text" id="edt_dia" size="3" maxlength="2" value="<?php echo $dia;?>"/></td>
    </tr>
    <tr class="txtBOX">
      <td>Plano Cobranca:</td>
      <td><label for="sel_plano"></label><?php include ('sel_plano.php'); ?></td>
    </tr>
    <tr class="txtBOX">
      <td><strong>Login Acesso:</strong></td>
      <td><strong>
      <input name="edt_login" type="text" id="edt_login" size="15" />
      </strong></td>
    </tr>
    <tr class="txtBOX">
      <td><strong>Senha Acesso:</strong></td>
      <td><strong>
      <input name="edt_senha" type="text" id="edt_senha" size="15" value="<?php echo "$dia$mes$ano";?>" />
      </strong></td>
    </tr>
    <tr class="txtBOX">
      <td><strong>Valido ATE:</strong></td>
      <td><strong>
      <input name="edt_validoate" type="text" id="edt_validoate" size="12" maxlength="10" OnKeyPress="formatar(this, '##/##/####')" />
      </strong></td>
    </tr>
    <tr class="txtBOX">
      <td><strong>Profile Acesso:</strong></td>
      <td><strong>
        <?php include ('sel_profile.php'); ?>
      </strong></td>
    </tr>
    <tr class="txtBOX">
      <td colspan="2" align="center"><input type="submit" name="Gravar" id="Gravar" value="Gravar" /></td>
    </tr>
  </table>
</form>
<?php
if ($oqfazer=="salvar"){
	if ($edt_nome == "" || $edt_email == "" || $edt_dia=="" || $sel_plano == "" || $edt_login =="" || $edt_senha =="" || $edt_validoate == "" || $sel_profile == ""){
		echo "<script>window.alert('ERRO. Todos os campos sao de preenchimento obrigatorio');</script>";
		echo "<script>window.location='cliente_mobile.php';</script>";
	} else {
		$sql = "select name from csp_user where name='$edt_login' and idempresa='$acesso_idempresa'";
		$resultado = $con->banco->Execute($sql);
		$registro = $resultado->FetchNextObject();
		if ($registro->NAME != ""){
			$edt_login=$dia . $registro->NAME . $dia;
			echo "<script>window.alert('Atencao. Login ja existe. Alterado automaticamente para $edt_login');</script>";
		}

		if ($edt_dia < 1 || $edt_dia > 28){
			echo "<script>window.alert('Atencao. Dia Incorreto.');</script>";
		}
		if ($edt_dia < 1){
			$edt_dia = 1;
		}
		if ($edt_dia > 28){
			$edt_dia=28;
		}
		$sql="select idprovedor from provedor order by idprovedor DESC limit 1";
		$resultado = $con->banco->Execute($sql);
		$registro = $resultado->FetchNextObject();
		$idprovedor = $registro->IDPROVEDOR;
		$idprovedor++;
		$valido=ymd($edt_validoate);
		$sql="insert into provedor (idprovedor, idplano, idempresa, idrevendedor,nome, diavencimento, email) values ('$idprovedor', '$sel_plano', '$acesso_idempresa', '$acesso_idusuario', '$edt_nome', '$edt_dia', '$edt_email')";
		$sql2="insert into csp_user (name, idempresa, id, idprofile, password, displayname, expiredate) values ('$edt_login', '$acesso_idempresa', '$idprovedor', '$sel_profile', '$edt_senha', '$edt_nome', '$valido')";
		$con->banco->Execute($sql);
		$con->banco->Execute($sql2);
		echo "<script>window.alert('Cliente cadastrado com sucesso. Login $edt_login com a senha $edt_senha valido ate $edt_validoate');</script>";
		echo "<script>window.location='cliente_mobile.php';</script>";
	}
}
?>
</body>
</html>