<?php 
    session_start();
    if(!isset($_SESSION['idUsuario'])){
    	header("location:../../");
    }
    include_once '../../conexao/index.php';
    error_reporting(E_ALL);
    $_SESSION['idUsuario'] = $_SESSION['idUsuario'];
    $_SESSION['nomeUsuario'] = $_SESSION['nomeUsuario'];
    $_SESSION['categoriaUsuario'] = $_SESSION['categoriaUsuario']; 
?>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">
<?php 
                        $sql = "SELECT ps.mesa, MIN(DATE(ps.data)) AS data, MIN(DATE_FORMAT(ps.data, '%H:%i')) AS hora, COUNT(p2.id) AS posicao FROM pos_mesas_temp_artigos ps LEFT JOIN ( SELECT mesa, DATE(data) as data, COUNT(id) as id FROM pos_mesas_temp_artigos WHERE cat_prod = 3 AND entregue = 0 GROUP BY mesa, DATE(data), HOUR(data), MINUTE(data) DIV 10 ) p2 ON ps.mesa = p2.mesa AND DATE(ps.data) = p2.data AND HOUR(ps.data) = HOUR(p2.data) AND MINUTE(ps.data) DIV 10 = MINUTE(p2.data) DIV 10 GROUP BY ps.mesa, DATE(ps.data), HOUR(ps.data), MINUTE(ps.data) DIV 10 ORDER BY data ASC, hora ASC, mesa ASC;
                        ";
                        $rs = mysqli_query($db, $sql);
                         $contador = 1; // Inicializa o contador
                        while ($dados = mysqli_fetch_array($rs)) {
                            $ms = $dados['mesa'];
                            $sql = "SELECT (SELECT nome FROM users as u WHERE u.id = pos_mesas_temp_artigos.user) as usu FROM pos_mesas_temp_artigos WHERE mesa = $ms";
                            $resultado = $db->query($sql);
                            $dadosa = mysqli_fetch_array($resultado);
                            $us = $dadosa['usu'];
                    ?>
                        <div class="p-2 divi" style="display: flex;flex-direction: column;align-items: center;border-radius: 5px;box-shadow: 0px 0px 10px 0px rgba(0,0,0,0.5);height: 47%;margin-bottom: 10px;">
                            <div class="col-sm-12 pt-2 pb-2" style="border-bottom: 1px solid #ccc;">
                                <span style="padding: 5px 8px;border-radius: 5px;font-weight: bold;background: #00FF00;color: #fff;" class="btn"><?php echo $contador; ?><sup>o</sup></span>
                                <span>Mesa <?php echo $ms; ?> | Operador do Caixa: <?php echo $us; ?></span>
                            </div>
                            <div class="col-sm-12 pt-2" style="height: 17vh;overflow: auto;">
                                <?php 
                                    $sql1 = "SELECT 
                                                    *, 
                                                    (SELECT nomeproduto FROM produto as p WHERE p.idproduto = ps.artigo) as nome 
                                                FROM 
                                                    pos_mesas_temp_artigos as ps 
                                                WHERE 
                                                    cat_prod = 3 
                                                    AND mesa = '$ms' 
                                                    AND DATE(data) = '" . $dados['data'] . "' 
                                                    AND HOUR(data) = HOUR('" . $dados['hora'] . "') 
                                                    AND MINUTE(data) DIV 10 = MINUTE('" . $dados['hora'] . "') DIV 10 
                                                ORDER BY 
                                                    FIELD(
                                                        TIME(data),
                                                        TIME('" . $dados['hora'] . "'),
                                                        ADDTIME(TIME('" . $dados['hora'] . "'), '00:10:00')
                                                    )
                                                ";
                                    $rs1 = mysqli_query($db, $sql1);
                                    while ($dados1 = mysqli_fetch_array($rs1)) {
                                        if($dados1['pronto'] == 1){
                                ?>
                                        <div>
                                            <span><?php echo $dados1['qtd']; ?></span>
                                            <span><?php echo $dados1['nome']; ?></span>
                                            <i class="fa fa-thumbs-up" aria-hidden="true" style="color: #00FF00;"></i>
                                        </div>
                                        <br>
                                <?php
                                        } else {
                                ?>
                                        <div>
                                            <span><?php echo $dados1['qtd']; ?></span>
                                            <span><?php echo $dados1['nome']; ?></span>
                                        </div>
                                        <br>
                                <?php  
                                        }
                                    }
                                ?>

                            </div>
                            <div class="col-sm-12 pt-2">
                                <tr>
                                    <td><?php echo $dados['data']; ?></td>
                                    <td><?php echo $dados['hora']; ?></td>
                                    
                                </tr>
                            </div>
                            <div class="col-sm-12 pt-2" style="display: flex;flex-direction: row;justify-content: space-around;">
                                <button type="button" class="btn pnt" style="padding: 0.5em 1em;background:#38D0ED;color: #fff;" data-mesa="<?php echo $dados['mesa']; ?>" data-datas="<?php echo $dados['data']; ?>" data-hora="<?php echo $dados['hora']; ?>">Pronto</button>
                                <button type="button" data-mesa="<?php echo $dados['mesa']; ?>" data-datas="<?php echo $dados['data']; ?>" data-hora="<?php echo $dados['hora']; ?>" class="btn bust" style="padding: 0.5em 1em;background: #00FF00;color: #fff;">Entregue</button>
                            </div>
                        </div>
                    <?php
                    $contador++; // Incrementa o contador a cada iteração
                        }
                    ?>