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

$id_ct = $_GET['id_ct'];

$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>Cotacao <?php echo "$serie/$id_ct"; ?></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 style="font-size: 9pt;">
<?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>
<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 cotacao WHERE id = '$id_ct')";
	$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>Cotacao #<?php echo "$serie/$id_ct"; ?></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>Disconto</th>
		<th>Prazo</th>
		<th>Data</th>
	</thead>
	<tbody>
		<?php 
			$sql = "SELECT * FROM cotacao WHERE id = '$id_ct'";
			$rs = mysqli_query($db, $sql);
			while ($dados = mysqli_fetch_array($rs)) {
		?>
			<tr>
				<td><?php echo $dados['disconto']; ?></td>
				<td><?php echo $dados['prazo']; ?></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 = f.artigo) as descricao FROM ct_artigos_cotados as f WHERE cotacao = '$id_ct'";
			$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 cotacao as f WHERE id = '$id_ct'";
			$rs = mysqli_query($db, $sql);
			while ($dados = mysqli_fetch_array($rs)) {
			$v = $dados['valor'];
			$i = $dados['iva'];
			$d = $dados['disconto'];
			$vv = $v + $d;
			$t = $v + $i;
		?>
			<p>Total: <?php echo $v; ?></p>
			<p>IVA 16%: <?php echo $dados['iva']; ?></p>
			<p>Disconto: <?php echo $dados['disconto']; ?></p>
			<h4>Total Geral: <?php echo "$t Mt"; ?></h4>
		<?php
			}
		?>
	</div>
</div>

<div style="text-align: left;border: 1px solid #000;width: 35%;padding: 5px 10px;margin-bottom: 25px;">
		<p>Dados Bancarios</p>
		<?php 
			$sql = "SELECT * FROM empresa";
			$rs = mysqli_query($db, $sql);
			while ($dados = mysqli_fetch_array($rs)) {
		?>
		<p><b>BANCO:</b><b><?php echo $dados['banco']; ?></b></p>
		<p>CONTA: <?php echo $dados['conta']; ?></p>
		<p>NIB: <?php echo $dados['nib']; ?></p>
		<?php
			}
		?>
</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>