<?php 
session_start();
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">
                	<?php 
						$sql = "SELECT count(idproduto) as totprod, sum(stock) as smst FROM produto";
						$rs = mysqli_query($db, $sql) or die(mysqli_error($db));
						while ($dados = mysqli_fetch_array($rs)) {
					?>
	                    <div class="col-md-6 col-sm-6 col-lg-6 col-xl-4">
	                        <div class="dash-widget">
								<span class="dash-widget-bg1"><i class="fa fa-life-ring" aria-hidden="true"></i></span>
								<div class="dash-widget-info text-right">
									<h4><?php echo $dados['totprod']; ?> - <?php echo $dados['smst']; ?></h4>
									<span class="widget-title1">Artigos - Stock<i class="fa fa-check" aria-hidden="true"></i></span>
								</div>
	                        </div>
	                    </div>
                    <?php
						}
					?>
					<?php 
						$sql = "SELECT (SUM(stock * preco) - SUM(stock * preco_compra)) as balanco FROM produto";
						$rs = mysqli_query($db, $sql) or die(mysqli_error($db));
						while ($dados = mysqli_fetch_array($rs)) {
							$bal = $dados['balanco'];
					?>
	                    <div class="col-md-6 col-sm-6 col-lg-6 col-xl-4">
	                        <div class="dash-widget">
	                            <span class="dash-widget-bg2"><i class="fa fa-money"></i></span>
	                            <div class="dash-widget-info text-right">
	                                <h4><?php echo "".number_format($bal,2,".",","); ?></h4>
	                                <span class="widget-title2">Lucro <i class="fa fa-check" aria-hidden="true"></i></span>
	                            </div>
	                        </div>
	                    </div>
                    <?php
						}
					?>
					<?php 
						$sql = "SELECT count(id) as totcom FROM compras";
						$rs = mysqli_query($db, $sql) or die(mysqli_error($db));
						while ($dados = mysqli_fetch_array($rs)) {
					?>
	                    <div class="col-md-6 col-sm-6 col-lg-6 col-xl-4">
	                        <div class="dash-widget">
	                            <span class="dash-widget-bg3"><i class="fa fa-cart-plus" aria-hidden="true"></i></span>
	                            <div class="dash-widget-info text-right">
	                                <h4><?php echo $dados['totcom']; ?></h4>
	                                <span class="widget-title3">Compras <i class="fa fa-check" aria-hidden="true"></i></span>
	                            </div>
	                        </div>
	                    </div>
                    <?php
						}
					?>
					
                </div> -->
                <!-- <div class="row">
                	<?php 
						$sql = "SELECT COUNT(`idpedido`) as totped, SUM(`pagamentopedido`) as totval FROM `pedido`";
						$rs = mysqli_query($db, $sql) or die(mysqli_error($db));
						while ($dados = mysqli_fetch_array($rs)) {
							$vl = $dados['totval'];
					?>
	                    <div class="col-md-6 col-sm-6 col-lg-6 col-xl-6">
	                        <div class="dash-widget">
	                            <span class="dash-widget-bg4"><i class="fa fa-credit-card" aria-hidden="true"></i></span>
	                            <div class="dash-widget-info text-right">
	                                <h4><?php echo $dados['totped']; ?> - <?php echo "".number_format($vl,2,".",","); ?></h4>
	                                <span class="widget-title4">Vendas - Custo <i class="fa fa-check" aria-hidden="true"></i></span>
	                            </div>
	                        </div>
	                    </div>
                    <?php
						}
					?>
	                <div class="col-md-6 col-sm-6 col-lg-6 col-xl-6">
	                    <div class="dash-widget">
	                        <span class="dash-widget-bg1"><i class="fa fa-credit-card" aria-hidden="true"></i></span>
	                        <div class="dash-widget-info text-right ac">
	                                
	                        </div>
	                    </div>
	                </div>
                </div> -->
                <div class="row">
                <div class="col-12 col-md-12 col-lg-12 col-xl-12">
						<div class="card">
							<div class="card-header" id="db" style="display: flex;flex-direction: row;justify-content: space-between;">
								<h4 class="card-title d-inline-block">Alerta Stock Minimo</h4>
							</div>
							<div class="card-body p-0">
								<div class="table-responsive">
									<table class="table mb-0">
										<thead class="d-none">
											<tr>
												<th>Artigo</th>
												<th>Stock</th>
												<th>Stock Minimo</th>
												<th class="text-right">Alerta</th>
											</tr>
										</thead>
										<tbody class="tbs">
											
										</tbody>
									</table>
								</div>
							</div>
						</div>
					</div>
                </div>
				<div class="row">
					<div class="col-12 col-md-6 col-lg-6 col-xl-6">
						<div class="card">
							<div class="card-header">
								<h4 class="card-title d-inline-block">Artigos mais vendidos</h4>
							</div>
							<div class="card-body p-0">
								<div class="table-responsive">
									<table class="table mb-0">
										<thead class="d-none">
											<tr>
												<th>Patient Name</th>
												<th>Doctor Name</th>
												<th>Timing</th>
												<th class="text-right">Status</th>
											</tr>
										</thead>
										<tbody class="tb1">
											
										</tbody>
									</table>
								</div>
							</div>
						</div>
					</div>
                    <div class="col-12 col-md-6 col-lg-6 col-xl-6">
                    	<div class="card">
							<div class="card-header">
								<h4 class="card-title d-inline-block">Vendas Recentes </h4><!--  <a href="patients.html" class="btn btn-primary float-right">View all</a> -->
							</div>
							<div class="card-block">
								<div class="table-responsive">
									<table class="table mb-0 new-patient-table">
										<tbody class="tb2">
												
										</tbody>
									</table>
								</div>
							</div>
						</div>
                    </div>
				</div>
				<div class="row">
					<div class="col-12 col-md-12 col-lg-8 col-xl-8">

					</div>
				</div>
            </div>
        </div>
    </div>
    <div class="sidebar-overlay" data-reff=""></div>
    <?php include 'includes/footer_plugins.php'; ?>
    <script type="text/javascript">
    	$(function() {
    		setInterval(atualizarProdutosMaisVendidos, 1000);
    		setInterval(atualizarVendasRecentes, 1000);
    		setInterval(atualizarTotalDiario, 1000);
            setInterval(atualizarStockMin, 1000);
    		function atualizarProdutosMaisVendidos() {
    			$.ajax({
    				url: 'daos/atualizarProdutosMaisVendidos.php',
    				type: 'GET',
    				success: function(data){
    					$(".tb1").html(data);
    				},
    				error: function(){
    					$(".tb1").html("<tr><td colspan='4'>Erro ao popular os produtos mais vendidos!</td></tr>")
    				}
    			});
    		}
            function atualizarStockMin() {
    			$.ajax({
    				url: 'daos/atualizarStockMin.php',
    				type: 'GET',
    				success: function(data){
    					$(".tbs").html(data);
                        		var audio = $("#db");
    				},
    				error: function(){
    					$(".tbs").html("<tr><td colspan='4'>Erro ao popular os artigos com stock min!</td></tr>")
    				}
    			});
    		}
    		function atualizarVendasRecentes() {
    			$.ajax({
    				url: 'daos/atualizarVendasRecentes.php',
    				type: 'GET',
    				success: function(data){
    					$(".tb2").html(data);
    				},
    				error: function(){
    					$(".tb2").html("<tr><td colspan='4'>Erro ao popular as vendas mais recentes!</td></tr>")
    				}
    			});
    		}
    		function atualizarTotalDiario() {
    			$.ajax({
    				url: 'daos/atualizarTotalDiario.php',
    				type: 'GET',
    				success: function(data){
    					$(".ac").html(data);
    				},
    				error: function(){
    					$(".ac").html("Erro ao popular o total diario!")
    				}
    			});
    		}
    	});
    </script>
</body>



</html>