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

$id_oc = $_GET['id_oc'];

$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>Ordem de Compra <?php echo "$serie/$id_oc"; ?></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>
<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 fornecedor WHERE id = (SELECT fornecedor FROM ordem_compra WHERE id = '$id_oc')";
	$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']; ?></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>Ordem de Compra #<?php echo "$serie/$id_oc"; ?></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>Prazo</th>
		<th>Modo de Pagamento</th>
		<th>Data</th>
	</thead>
	<tbody>
		<?php 
			$sql = "SELECT * FROM ordem_compra WHERE id = '$id_oc'";
			$rs = mysqli_query($db, $sql);
			while ($dados = mysqli_fetch_array($rs)) {
		?>
			<tr>
				<td><?php echo $dados['descricao']; ?></td>
				<td><?php echo $dados['prazo']; ?></td>
				<td><?php echo $dados['modo']; ?></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 id,artigo,qtd,preco,total FROM ordem_compra_artigos WHERE ordem_compra = '$id_oc'";
			$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 ordem_compra WHERE id = '$id_oc'";
			$rs = mysqli_query($db, $sql);
			while ($dados = mysqli_fetch_array($rs)) {
			$v = $dados['valor'];
		?>
			<h4>Total Geral: <?php echo "$v Mt"; ?></h4>
		<?php
			}
		?>
	</div>
</div>

<div class="especial" style="display: flex;flex-direction: row;justify-content: center;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>