<?php 
session_start();
if(!isset($_SESSION['idUsuario'])){
	header("location:../");
}
include_once '../conexao/index.php';

$_SESSION['idUsuario'] = $_SESSION['idUsuario'];
$_SESSION['nomeUsuario'] = $_SESSION['nomeUsuario'];
$_SESSION['categoriaUsuario'] = $_SESSION['categoriaUsuario']; 

?>
<!DOCTYPE html>
<html>
<head>
	<title></title>
	<meta charset="utf-8">
	<meta name="viewport" content="width-device-width, initial-scale=1">
	<link rel="shorcut icon"  href="../../../img/config/ios7-cart.png">
	<link rel="stylesheet" type="text/css" href="../../../css/administracao.css">
	<link rel="stylesheet" type="text/css" href="../../../css/jquery-ui.min.css">
	<link rel="stylesheet" type="text/css" href="../../../css/jquery-ui.css">
	<script type="text/javascript" src="../../../js/jquery-3.3.1.min.js"></script>
	<script type="text/javascript" src="../../../js/jquery-ui.min.js"></script>
	<script type="text/javascript" src="../../../js/jquery-ui.js"></script>
	<script type="text/javascript" src="../../../js/funcoesAdministracao.js"></script>
	<script type="text/javascript" src="../../../js/configuracoesAdministracao.js"></script>
</head>
<body>
<div class="geralHeader">
	<div class="box grupo-head">
		<p class="p">Cadastro de Empresas/Seguradoras</p>
		<p><img src="../img/config/ios7-home-outline.png"></p>
	</div>
	<div class="box grupo-inputs">
		<p><span>Cod</span><span><input type="text" id="cod" placeholder="iP"></span></p>
		<p><span>Nome</span><span><input type="text" id="nome" placeholder="iProjects"></span></p>
		<p>
			<span>Contrato</span>
			<span>
				<select id="contrato">
					<option value="Mensal">Mensal</option>
					<option value="Semanal">Semanal</option>
					<option value="Diario">Diario</option>
				</select>
			</span>
		</p>
		<p><span>Nuit</span><span><input type="text" id="nuit" placeholder="143294277"></span></p>
		<p><span>Endereco</span><span><input type="text" id="endereco" placeholder="Bairro Chingodzi"></span></p>
		<p><span>Contacto</span><span><input type="phone" id="contacto" placeholder="878884862"></span></p>
		<p><span>Email</span><span><input type="email" id="email" placeholder="milagrelazaro1@gmail.com"></span></p>
	</div>
	<div class="box">
		<div class="btns">
			<button type="reset">
				Limpar
			</button>
			<button id="addEmpresa">
				Enviar
			</button>
		</div>
	</div>
	<div class="box table">
		<div class="look">
			<p>
				<span>Numero de registros</span>
				<span>
					<select id="numeroRegistros">
						<option value="1">1</option>
						<option value="5">5</option>
						<option value="10">10</option>
						<option value="20">20</option>
						<option value="50">50</option>
					</select>
				</span>
				<span><a class="listar" style="cursor: pointer;">Listar</a></span>
			</p>
			<p><input type="text" id="look" placeholder="Procurar"></p>
		</div>
		<div class="tabela">
			<p id="p"></p>
			<table>
				<thead>
					<th>#</th>
					<th>Cod</th>
					<th>Nome</th>
					<th>Contrato</th>
					<th>Nuit</th>
					<th>Endereco</th>
					<th>Contacto</th>
					<th>Email</th>
					<th>Data</th>
					<th>Apagar</th>
				</thead>
				<tbody>
					
				</tbody>
			</table>
		</div>
	</div>
	<script type="text/javascript">
		$(function() {
			mostrarEmpresa();
			procurarEmpresa();
			$(".listar").on('click', function() {
				mostrarEmpresa();
			});
			function contentEditable() {
				$("table tbody tr td.editavel").on('dblclick', function() {
				if ($('td > input').length > 0) {
					return;
				}
				var conteudoOriginal = $(this).text();
				var novoElemento = $('<input/>', {type: 'text', value:conteudoOriginal});
				$(this).html(novoElemento.bind('blur keydown', function(e) {
					var keyCode = e.which;
					var conteudoNovo = $(this).val();
					// $(this).parent().html(conteudoNovo);
					if (keyCode == 13 && conteudoNovo != "" && conteudoNovo != conteudoOriginal) {
						var objecto = $(this);
						$.ajax({
							url: 'adminViews/daos/alterarEmpresa/alterarEmpresa.php',
							type: 'POST',
							data: {
								id: $(this).parents('tr').children().first().text(),
								campo: $(this).parent().attr('title'),
								valor: conteudoNovo 
							},
							success: function(data) {
								objecto.parent().html(conteudoNovo);
								$("#p").text("");
								$("#p").append(data);
								// mostrarEmpresa();
							}
						});
					}
					if (e.type == "blur" || keyCode == 27) {
						$(this).parent().html(conteudoOriginal);
					}
				}));
				$(this).children().select();
			});
			}
			function mostrarEmpresa() {
				$.ajax({
					url: 'adminViews/daos/mostrarEmpresa/mostrarEmpresa.php',
					type: 'GET',
					data: {
						numeroRegistros: $("#numeroRegistros").val()
					},
					success: function(data) {
						$(".table tbody").html(data);
						contentEditable();
						apagarEmpresa();
						procurarEmpresa();
					},
					error: function() {
						$(".table tbody").html("Ocorreu um erro na tentativa de mostrar as empresas!");
					}
				});
			}
			function apagarEmpresa() {
				$(".img").on('click', function() {
					$.ajax({
					url: 'adminViews/daos/apagarEmpresa/apagarEmpresa.php',
					type: 'POST',
					data: {
						id: $(this).attr('ida'),
					},
					success: function(data) {
						$("#p").text("");
						$("#p").append(data);
						mostrarEmpresa();
					},
					error: function() {
						$("#p").text("");
						$("#p").append("Ocorreu um erro na tentativa de apagar a empresa");
					}
				});
				});
			}
			function procurarEmpresa() {
				$("#look").on('input', function() {
					$.ajax({
						url: 'adminViews/daos/consultarEmpresa/consultarEmpresa.php',
						type: 'GET',
						data: {
							procura: $("#look").val()
						},
						success: function(data) {
							$(".table tbody").html(data);
							contentEditable();
							apagarEmpresa();
						},
						error: function() {
							$("#p").html('Ocorreu um erro na tentativa de procurar o nome');
						}
					});
				});
			}
		});
	</script>
</div>
</body>
</html>        