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

error_reporting(E_ALL);

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

?>

<!DOCTYPE html>
<html>
<head>
	<title>iVone</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width-device-width, initial-scale=1">
	<link rel="shorcut icon"  href="../img/config/5087847.png">
	<link rel="stylesheet" type="text/css" href="css/nota_credito.css">
	<link rel="stylesheet" type="text/css" href="../font-awesome/css/font-awesome.css">
	<link rel="stylesheet" type="text/css" href="../font-awesome/css/font-awesome.min.css">
	<link rel="stylesheet" type="text/css" href="../css/jquery-ui.min.css"> 
	<link rel="stylesheet" type="text/css" href="../css/select2.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/select2.min.js"></script>
	<script type="text/javascript" src="../js/jquery-ui.js"></script>
	<link href="datatables.min.css" rel="stylesheet"/>
	<script src="datatables.min.js"></script>
	<style type="text/css">
		#examples tbody tr{
			transition: all 0.3s ease-in-out;
		}
		#examples tbody tr:hover{
			background: #ccc;
			cursor: pointer;
		}
		.principal .direita .rodapeDireita .div2{
			margin-top: 5px;
			display: flex;
			flex-direction: row;
			justify-content: flex-end;
		}
		.principal .direita .rodapeDireita .div2 a{
			padding: 5px 10px;
			color: #fff;
			cursor: pointer;
		}
		.principal .direita .rodapeDireita .div2 a:nth-child(1){
			background-color: #F43248;
		}
		.principal .direita .rodapeDireita .div2 a:nth-child(2){
			background-color: #EB7D08;
			margin-left: 10px;
		}
		.principal .direita .rodapeDireita .div1 .right input{
			width: 35%;
			padding: 8px;
			border: 1px solid #ccc;
		}

	</style>
</head>
<body>
<div class="principal">
	<div class="esquerda">
		<div class="cabecalhoEsquerda">
			<h4 class="operador">Operador: <?php echo $_SESSION['nomeUsuario']; ?></h4>
			<p class="relogio">
				<?php 
					$data_hora = date("Y-m-d H:i:s");
					echo $data_hora;
				?>
			</p>
			<a style="margin-left: 5px;" href="factura.php"><i class="fa fa-refresh" aria-hidden="true"></i></a>
		</div>
		<div class="conteudoEsquerda" style="padding: 15px;overflow-y: scroll;background: #fff;">
				<table id="examples" style="height: 75vh;">
					<thead>
						<td>#</td>
						<td>Descricao da factura</td>
						<td>Valor</td>
						<td>Cliente</td>
					</thead>
					<tbody>
				<?php 
						$sql = "SELECT id, descricao, valor, (SELECT CONCAT(nome, ' ', apelido) FROM clientes as c WHERE c.id = f.cliente) as cl FROM factura as f";
						$rs = mysqli_query($db, $sql);
						while ($dados = mysqli_fetch_array($rs)) {
					?>
						<tr>
							<td><?php echo $dados['id']; ?></td>
							<td><?php echo $dados['descricao'] ?></td>
							<td><?php echo $dados['valor'] ?></td>
							<td><?php echo $dados['cl'] ?></td>
						</tr>
					<?php
						}
					?>
					</tbody>
				</table>
		</div>
	</div>
	<div class="direita">
		<div class="cabecalhoDireita" style="width: 100%;">
			<a class="fecharSistema"><i class="fa fa-arrow-left" aria-hidden="true"></i></i> Sair</a>
			<a href="dash.php" style="margin-left: 10px;"><i class="fa fa-arrow-left" aria-hidden="true"></i></i> Dashboard</a>
			<h3 class="vnd">RC: 
				<?php 
					$sql = "SELECT MAX(id) as idp FROM nota_de_credito";
					$rs = mysqli_query($db, $sql);
					while ($dados = mysqli_fetch_array($rs)) {
						$f = $dados['idp'] + 1;
				?>
					<input type="number" name="" value="<?php echo $f; ?>" size="2" style="padding: 5px 2px;width: 30%;text-align: right;" max="<?php echo $f; ?>" min="1">
				<?php } ?>
			</h3>
		</div>
		<style type="text/css">
			.form{
				width: 100%;
				display: flex;
				flex-direction: column;
				align-items: center;
			}
			.form .gr{
				width: 90%;
				margin: 5px auto;
				display: flex;
				flex-direction: row;
				justify-content: space-between;
			}
			.form .gr .fa{
				width: 30%;
			}
			.form .gr .fa p{
				color: #fff;
				margin-bottom: 3px;
			}
			.form .gr .fa input{
				width: 90%;
				padding: 5px;
				text-align: center;
				font-size: 14pt;
			}
			.form .gr .ct{
				width: 65%;
			}
			.form .gr .ct p{
				color: #fff;
			}
			.form .gr .ct input{
				width: 95%;
				padding: 5px;
				text-align: center;
				font-size: 14pt;
			}
			.fm p{
				color: #fff;
			}
			button{
				padding: 10px 10px;
				background: #eee;
				color: #333;
				width: 100%;
				border: none;
				cursor: pointer;
			}
		</style>
		<div class="conteudoDireita">
			<div class="form">
				<div class="gr">
					<div class="fa">
						<p>Factura</p>
						<input type="number" id="fa" readonly="">
					</div>
					<div class="ct">
						<p>Cliente</p>
						<input type="text" id="clt" readonly="">
					</div>
				</div>
			</div>
			<form>
					<input type="number" id="valor_tot" placeholder="Valor a pagar" step="0.01">
			</form>
			<form>
					<select id="modo">
						<option value="0">Selecione o modo de pagamento</option>
						<option value="Cheque">Cheque</option>
						<option value="Numerario">Numerario</option>
						<option value="Outros">Outros</option>
					</select>
			</form>
		</div>
		
		<div class="rodapeDireita">
			
			<div class="div2">
					<a href=""><i class="fa fa-lock" aria-hidden="true"></i> Cancelar</a>
					<a class="processar"><i class="fa fa-lock" aria-hidden="true"></i> Processar</a>
			</div>
		</div>
	</div>
</div>
	<script type="text/javascript" src="../js/data-table-act.js"></script>
	<script type="text/javascript" src="../js/jquery.dataTables.min.js"></script>
	<script type="text/javascript" src="../js/funcoes.js"></script>
	<script type="text/javascript" src="../js/configuracoes.js"></script>
	<script type="text/javascript">
		$(function() {
			setInterval(lg, 1000);

			function lg(){
				$.ajax({
						url: 'relogio.php',
						type: 'GET',
						success: function(data) {
							$(".relogio").html(data);
						},
						error: function() {
							$(".relogio").html("Ocorreu um erro ao popular a hora!");
						}
				});
			}
			$("#examples tbody tr").on('click', function() {
				var id_f = $(this).children().first().text();
				$("#fa").val(id_f);
				$.ajax({
					url: 'popCliente.php',
					type: 'GET',
					data: {
						id: id_f
					},
					success: function(data) {
						$("#clt").val(data);
					}
				});
			});
			
			$(".processar").on('click', function() {
				var cliente = $("#clt").val();
				var fact = $("#fa").val();
				var val  = $("#valor_tot").val();
				var modo = $("#modo").val();
				if (modo == 0) {
					alert("Selecione um modo de pagamento valido!")
				}else{
					$.ajax({
						url: 'criarRecibo.php',
						type: 'POST',
						data: {
							cliente: cliente,
							fact: fact,
							val : val,
							modo: modo
						},
						success: function(data) {
							if (data == 0) {
								alert("Esta factura possui uma nota de credito, nao sera possivel emitir um recibo para a mesma.")
							}else{
								window.open("rc.php?id_rc="+data+"");
								location.reload();
							}
							
						}
					});
				}
				
			});

			$('#examples').DataTable();
		});
	</script>
</body>
</html>