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

$id_nc = $_GET['id_nc'];

$sql = "SELECT MAX(ano_fiscal) as serie FROM serie_factura";
$rs = mysqli_query($db, $sql) or die(mysqli_error($db));
if (mysqli_num_rows($rs) > 0) {
	$dados = mysqli_fetch_array($rs);
	$serie = $dados['serie'];
}

error_reporting(E_ALL);

$_SESSION['idUsuario'] = $_SESSION['idUsuario'];
$_SESSION['nomeUsuario'] = $_SESSION['nomeUsuario'];
$_SESSION['categoriaUsuario'] = $_SESSION['categoriaUsuario']; 
/*Variaveis do Sistema*/
/*********************************************/
?>
<!DOCTYPE html>
<html>
<head>
	<title>Nota de Credito <?php echo "$serie/$id_nc"; ?></title>
	<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css">
	<script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script>
</head>
<body>
<?php 
	$sql = "SELECT * FROM empresa";
	$rs = mysqli_query($db, $sql);
	while ($dados = mysqli_fetch_array($rs)) {
?>
<div class="row">
	<div class="col-md-8">
		<h5><b><?php echo $dados['nome']; ?></b></h5>
		<p>Contribuinte: <?php echo $dados['nuit']; ?></p>
	</div>
	<div class="col-md-4">
		<img src="">
	</div>
</div>
<br>
<div class="row">
	<div class="col-md-10">
		<p><?php echo $dados['endereco']; ?></p>
		<p><?php echo $dados['provincia']; ?> - <?php echo $dados['pais']; ?></p>
		<p>Tel.: +258 <?php echo $dados['contacto']; ?></p>
		<p>Capital Social: <?php echo $dados['capital_social']; ?></p>
		<p><?php echo $dados['email']; ?></p>
	</div>
</div>
<?php
	}
?>

<?php 
	$sql = "SELECT * FROM clientes WHERE id = (SELECT cliente FROM nota_de_credito WHERE id = '$id_nc')";
	$rs = mysqli_query($db, $sql);
	while ($dados = mysqli_fetch_array($rs)) {
?>
<div class="row">
	<div class="col-md-12" style="text-align: right;">
		<p>Exmo.(s) Sr.(s)</p>
		<p><?php echo $dados['nome']; ?> <?php echo $dados['apelido']; ?></p>
		<p><?php echo $dados['endereco']; ?></p>
		<p>Contribuinte: <?php echo $dados['nuit']; ?></p>
	</div>
</div>

<?php
	}
?>
<div style="display: flex;flex-direction: row;justify-content: space-between;">
	<h4>Nota de Credito #<?php echo "$serie/$id_nc"; ?></h4>

	<p style="margin-top: 8px;">Original</p>
</div>
<div class="row">
	<hr class="col-md-12" style="margin-top: -5px;background-color: #000;border-style: inset;border-width: 1px;">
</div>

<table class="table" style="margin-top: -10px;">
	<thead>
		<th>Descricao</th>
		<th>Motivo</th>
		<th>Data</th>
	</thead>
	<tbody>
		<?php 
			$sql = "SELECT * FROM nota_de_credito WHERE id = '$id_nc'";
			$rs = mysqli_query($db, $sql);
			while ($dados = mysqli_fetch_array($rs)) {
		?>
			<tr>
				<td><?php echo $dados['descricao']; ?></td>
				<td><?php echo $dados['motivo']; ?></td>
				<td><?php echo $dados['data']; ?></td>
			</tr>
		<?php
			}
		?>
	</tbody>
</table>
<div class="row">
	<hr class="col-md-12" style="margin-top: -5px;background-color: #000;border-style: inset;border-width: 1px;">
</div>

<table class="table" style="margin-top: -10px;">
	<thead>
		<th>Artigo</th>
		<th>Descricao</th>
		<th>Quantidade</th>
		<th>Preco</th>
		<th>Total</th>
	</thead>
	<tbody>
		<?php 
			$sql = "SELECT artigo,qtd,preco,total,(SELECT nomeproduto FROM produto as p WHERE p.idproduto = n.artigo) as descricao FROM nc_artigos as n WHERE id_nota = '$id_nc'";
			$rs = mysqli_query($db, $sql);
			while ($dados = mysqli_fetch_array($rs)) {
		?>
			<tr>
				<td><?php echo $dados['artigo']; ?></td>
				<td><?php echo $dados['descricao']; ?></td>
				<td><?php echo $dados['qtd']; ?></td>
				<td><?php echo $dados['preco']; ?></td>
				<td><?php echo $dados['total']; ?></td>
			</tr>
		<?php
			}
		?>
	</tbody>
</table>
<div class="row">
	<hr class="col-md-12" style="margin-top: -5px;background-color: #000;border-style: inset;border-width: 1px;">
</div>
<div>
	<div class="col-md-12" style="text-align: right;">
		<?php 
			$sql = "SELECT * FROM nota_de_credito WHERE id = '$id_nc'";
			$rs = mysqli_query($db, $sql);
			while ($dados = mysqli_fetch_array($rs)) {
			$v = $dados['valor'];
			$i = $dados['iva'];
			$t = $v + $i;
		?>
			<p>Total: <?php echo $v; ?></p>
			<p>IVA 16%: <?php echo $i; ?></p>
			<h4>Total Geral: <?php echo "$t Mt"; ?></h4>
		<?php
			}
		?>
	</div>
</div>

<div class="especial" style="display: flex;flex-direction: row;justify-content: space-between;width: 80%;margin: 5px auto;padding-top: 15px;">
	<div style="text-align: center;">
		<p>_________________________________</p>
		<p>Assinatura do Cliente</p>
	</div>
	<div style="text-align: center;">
		<p>_________________________________</p>
		<p>Assinatura do Administrativo</p>
	</div>
</div>
<script type="text/javascript">
	$(function() {
		window.print();
	});
</script>
</body>
</html>