<!-- <meta http-equiv="refresh" content="5; url=miolo.php?miolo=clientes&acao=gerenciar" /> -->

<?php
if($_SESSION['nivel'] == '1' || $_SESSION['nivel'] == '2'){
	if(!empty($_POST['empresa']) && !empty($_POST['login']) && !empty($_POST['cod']) && !empty($_POST['email'])){
		$queryQuantidade = "SELECT 
							  COUNT(id) AS qtd
							FROM
							  (SELECT 
							    id 
							  FROM
							    crm_cliente
							  WHERE login = '".$_POST['login']."' 
							    AND id <> '".$_POST['cod']."' 
							  UNION
							  SELECT 
							    id 
							  FROM
							    usuarios 
							  WHERE login = '".$_POST['login']."') tabela";
		$selectLogin = mysql_query($queryQuantidade);
		$qtd = mysql_fetch_array($selectLogin);
		$qtdLogin = $qtd['qtd'];
		$qtdLogin = mysql_result($selectLogin, 0,0);
		if($qtdLogin == 0){

			$queryRegistrados=mysql_query("SELECT
									cli.id,
									cli.empresa,
									cli.logradouro,
									cli.numero,
									cli.complemento,
									cli.bairro,
									cli.cidade,
									cli.uf,
									cli.cep,
									cli.telefone,
									cli.telefone01,
									cli.telefone02,
									cli.telefone03,
									cli.segmento,
									cli.qtfunc,
									cli.email,
									cli.email01,
									cli.email02,
									cli.email03,
									cli.login,									
									cli.total_credito,
									cli.responsavel,
									cli.cargo
								FROM 
			     					crm_cliente cli 
			            		WHERE
			            			cli.id='".$_POST['cod']."'") or die (mysql_error());

			list($cod, $nome, $logradouro, $numero, $complemento, $bairro, $cidade, $uf,
				$cep, $telefone, $telefone01, $telefone02, $telefone03, $segmento, $qtfunc,
				$email, $email01, $email02, $email03, $loginemp, $total_credito, $responsavel,
				$cargo)=$linhaRegistrados=mysql_fetch_array($queryRegistrados);

			$queryCliente = "SELECT empresa, email, email01, email02, email03 FROM crm_cliente WHERE id='".$_POST['cod']."'";
			$selectCliente = mysql_query($queryCliente);
			$empresa = $_POST['empresa'];
			$resultEmails[0] = mysql_result($selectCliente, 0, 1);
			$resultEmails[1] = mysql_result($selectCliente, 0, 2);

			$resultEmails[2] = $_POST['email'];
			$resultEmails[3] = $_POST['email01'];

			$conteudoEmail .= utf8_decode("  <table width='600' border='0' align='left'><b>Prezado cliente: </b>".utf8_decode($empresa)." <br><br>
			<b>Alguns de seus dados foram alterados no sistema, confira os mesmos abaixo:</b><br>
			<b>Acesse sua conta aqui:</b> http://www.controlecontabiliadejf.com.br/

			<br></br>
				<table>");

			if ($nome != $_POST['empresa']) {
				$conteudoEmail.= "<tr><td>Empresa: &nbsp;&nbsp; </td> <td> $nome </td> <td> &nbsp;&nbsp; >>> &nbsp;&nbsp; </td> <td> ".utf8_decode($_POST['empresa'])." </td></tr>";
			}
			if ($logradouro != $_POST['logradouro']) {
				$conteudoEmail.= "<tr><td>Logradouro: &nbsp;&nbsp;</td> <td> $logradouro </td> <td> &nbsp;&nbsp; >>> &nbsp;&nbsp; </td> <td> ".utf8_decode($_POST['logradouro'])." </td></tr>";
			}
			if ($numero != $_POST['numero']) {
				$conteudoEmail.= "<tr><td>Número: &nbsp;&nbsp;</td> <td> $numero </td> <td> &nbsp;&nbsp; >>> &nbsp;&nbsp; </td> <td> ".$_POST['numero']." </td></tr>";
			}
			if ($complemento != $_POST['complemento']) {
				$conteudoEmail.= "<tr><td>Complemento: &nbsp;&nbsp;</td> <td> $complemento </td> <td> &nbsp;&nbsp; >>> &nbsp;&nbsp; </td> <td> ".utf8_decode($_POST['complemento'])." </td></tr>";
			}
			if ($bairro != $_POST['bairro']) {
				$conteudoEmail.= "<tr><td>Bairro: &nbsp;&nbsp;</td> <td> $bairro </td> <td> &nbsp;&nbsp; >>> &nbsp;&nbsp; </td> <td> ".utf8_decode($_POST['bairro'])." </td></tr>";
			}
			if ($cidade != $_POST['cidade']) {
				$conteudoEmail.= "<tr><td>Cidade: &nbsp;&nbsp;</td> <td> $cidade </td> <td> &nbsp;&nbsp; >>> &nbsp;&nbsp; </td> <td> ".utf8_decode($_POST['cidade'])." </td></tr>";
			}
			if ($uf != $_POST['uf']) {
				$conteudoEmail.= "<tr><td>UF: &nbsp;&nbsp;</td> <td> $uf </td> <td> &nbsp;&nbsp; >>> &nbsp;&nbsp; </td> <td> ".$_POST['uf']." </td></tr>";
			}
			if ($cep != $_POST['cep']) {
				$conteudoEmail.= "<tr><td>CEP: &nbsp;&nbsp;</td> <td> $cep </td> <td> &nbsp;&nbsp; >>> &nbsp;&nbsp; </td> <td> ".$_POST['cep']." </td></tr>";
			}
			if ($telefone != $_POST['telefone']) {
				$conteudoEmail.= "<tr><td>Telefone: &nbsp;&nbsp;</td><td> $telefone </td> <td> &nbsp;&nbsp; >>> &nbsp;&nbsp; </td> <td> ".$_POST['telefone']." </td></tr>";
			}
			if ($telefone01 != $_POST['telefone01']) {
				$conteudoEmail.= "<tr><td>Telefone: &nbsp;&nbsp;</td> <td> $telefone01 </td> <td> &nbsp;&nbsp; >>> &nbsp;&nbsp; </td> <td> ".$_POST['telefone01']." </td></tr>";
			}
			if ($email != $_POST['email']) {
				$conteudoEmail.= "<tr><td>E-Mail: &nbsp;&nbsp; </td> <td> $email </td> <td> &nbsp;&nbsp; >>> &nbsp;&nbsp; </td> <td> ".$_POST['email']." </td></tr>";
			}
			if ($email01 != $_POST['email01']) {
				$conteudoEmail.= "<tr><td>E-Mail Alternativo &nbsp;&nbsp; </td> <td> $email01 </td> <td> &nbsp;&nbsp; >>> &nbsp;&nbsp; </td> <td> ".$_POST['email01']." </td></tr>";
			}
			if ($segmento != $_POST['segmento']) {
				$conteudoEmail.= "<tr><td>Segmento &nbsp;&nbsp;</td> <td> $segmento </td> <td> &nbsp;&nbsp; >>> &nbsp;&nbsp; </td> <td> ".utf8_decode($_POST['segmento'])." </td></tr>";
			}
			if ($qtfunc != $_POST['qtfunc']) {
				$conteudoEmail.= "<tr><td>Qntd. Funcionários: &nbsp;&nbsp;</td> <td> $qtfunc </td> <td> &nbsp;&nbsp; >>> &nbsp;&nbsp; </td> <td> ".$_POST['qtfunc']." </td></tr>";
			}
			if ($loginemp != $_POST['login']) {
				$conteudoEmail .= "<tr><td>Login: &nbsp;&nbsp;</td> <td> $loginemp </td> <td> &nbsp;&nbsp; >>> &nbsp;&nbsp; </td> <td> ".$_POST['login']." </td></tr>";
			}

			if ($_POST['senha'] != '') {
				$conteudoEmail .= "<tr><td colspan='2'> Nova senha </td> <td> &nbsp;&nbsp; >>> &nbsp;&nbsp; </td> <td> ".$_POST['senha']." </td></tr>";
			}

			$conteudoEmail .= "</table>
													<br><br><br>
													Atenciosamente,
													<br>
													Sistema CRM<br>
												</table>";

			// echo $conteudoEmail;
		
		 	$assuntoEmail = utf8_decode("Alteração de dados, Empresa:". $empresa);

			require 'controladores/emails/resolveEmailChamado.php';	

			$result = mysql_query("update crm_cliente set empresa = '".utf8_decode($_POST['empresa'])."', logradouro = '".utf8_decode($_POST['logradouro'])."', numero = '".utf8_decode($_POST['numero'])."', complemento = '".utf8_decode($_POST['complemento'])."', bairro = '".utf8_decode($_POST['bairro'])."', cidade='".utf8_decode($_POST['cidade'])."', uf='".utf8_decode($_POST['uf'])."', cep='".utf8_decode($_POST['cep'])."', telefone = '".utf8_decode($_POST['telefone'])."', telefone01 = '".utf8_decode($_POST['telefone01'])."', telefone02 = '".utf8_decode($_POST['telefone02'])."', telefone03 = '".utf8_decode($_POST['telefone03'])."', segmento='".utf8_decode($_POST['segmento'])."', qtfunc='".$_POST['qtfunc']."', email='".$_POST['email']."', email01='".$_POST['email01']."', login='".$_POST['login']."', senha='".$_POST['senha']."', responsavel='".$_POST['responsavel']."', cargo='".$_POST['cargo']."' where id = '".$_POST['cod']."'");
			if($result){ ?>
				<p align="center" style="margin-top:100"><font color="#000066" face="Arial, Helvetica, sans-serif" size="+2"> Dados atualizados com sucesso</font></p>
			<?	} }

		if($qtdLogin > 0){
			?>
				<p align="center" style="margin-top:100"><font color="#000066" face="Arial, Helvetica, sans-serif" size="+2"> Já existe cliente com o login `<strong><?php echo $_POST['login']; ?></strong>`.<br/>Por favor, utilize um login diferente.</font></p>
			<?
		}
	}
	if(empty($_POST['empresa']) || empty($_POST['login']) || empty($_POST['email'])){
		?>
			<p align="center" style="margin-top:100"><font color="#000066" face="Arial, Helvetica, sans-serif" size="+2"> Informe pelo menos o nome e o cnpj da empresa.</font></p>
		<?	
	}
}
?>