<?php 
session_start();
error_reporting(E_ALL);
include '../../conexao/index.php';
?>
<!DOCTYPE html>
<html lang="en">



<head>
    <?php include 'includes/head.php'; ?>
</head>

<body>
    <div class="main-wrapper">
        <div class="header">
            <?php include 'includes/header.php' ?>
        </div>
        <div class="sidebar" id="sidebar">
            <?php include 'includes/side_bar.php'; ?>
        </div>
        <div class="page-wrapper">
            <div class="content">
                <div class="row">
                    <div class="col-sm-4 col-3">
                        <h4 class="page-title">Artigos da Compra</h4>
                    </div>
                    <div class="col-sm-8 col-9 text-right m-b-20">
                        <a href="?id=inventario" class="btn btn btn-primary btn-rounded float-right"><i class="fa fa-plus"></i> Novo Inventário</a>
                    </div>
                </div>
				<div class="row">
					<div class="col-md-12">
						<div class="table-responsive">
							<table class="mb-0" id="example">
								<thead>
									<tr>
										<th>#</th>
										<th>Artigo</th>
										<th>Stock</th>
									</tr>
								</thead>
								<tbody>
									<?php 
										$sql = "SELECT * FROM produto";
										$rs = mysqli_query($db, $sql) or die(mysqli_error($db));
										while ($dados = mysqli_fetch_array($rs)) {
									?>
										<tr> 
											<td><?php echo $dados['idproduto']; ?></td>
											<td><?php echo $dados['nomeproduto']; ?></td>
											<td><?php echo $dados['stock']; ?></td>
										</tr>
									<?php
										}
									?>
								</tbody>
							</table>
						</div>
					</div>
                </div>
            </div>
        </div>
    </div>
    <div class="sidebar-overlay" data-reff=""></div>
    <?php include 'includes/footer_plugins.php'; ?>
    <link href="../datatables.min.css" rel="stylesheet"/>
    <script src="../datatables.min.js"></script>
    <script type="text/javascript" src="../data-table-act.js"></script>
    <script type="text/javascript" src="../jquery.dataTables.min.js"></script>
    <script type="text/javascript">
    	$(function() {
    		$('#example').DataTable();
    	});
    </script>
    <?php 
    	if (isset($_GET['id'])) {
    		$dia = date("d");
    		$mes = date("m");

    		$sqll = "SELECT * FROM produto";
			$rss = mysqli_query($db, $sqll) or die(mysqli_error($db));
			while ($dados = mysqli_fetch_array($rss)) {
				$prt = $dados['nomeproduto'];
				$stck = $dados['stock'];
				$sql = "SELECT artigo FROM inventario WHERE artigo = '$prt'";
				$rs = mysqli_query($db, $sql) or die(mysqli_error($db));
				if (mysqli_num_rows($rs) > 0) {
					$sql = "UPDATE inventario SET `$dia` = '$stck' WHERE artigo = '$prt'";
					$rs = mysqli_query($db, $sql) or die(mysqli_error($db));
				}else{
					$sql = "INSERT INTO inventario SET artigo = '".$dados['nomeproduto']."', `$dia` = '".$dados['stock']."'";
					$rs = mysqli_query($db, $sql) or die(mysqli_error($db));
				}
			}
			echo "<script>alert('Inventario criado com sucesso!'); </script>";
    	}
    ?>
</body>
<!-- patients23:19-->
</html>