<?php
session_start();

require 'conexao.php';
require 'config.php';
require 'includes/inc_verifica_caixa.php';
require 'includes/limpar_session.php';

$pdo  = Conexao::getInstance();	
$sql = "SELECT ven_001, ven_027, ven_026 FROM venda WHERE sit_001 IN (8,21) AND ven_024 = 'C' ORDER BY ven_026";
$stm = $pdo->prepare($sql);
$stm->execute();
$comandas = $stm->fetchAll(PDO::FETCH_OBJ);

?>
<!DOCTYPE html>
<html>
<head>
	<title>Comanda Eletrônica</title>
  <?php require 'includes/inc_head.php'; ?>
</head>
<body>
   <div class = "mdl-layout mdl-js-layout mdl-layout--fixed-header">
     <header class = "mdl-layout__header">
        <div class = "mdl-layout__header-row login">
           <span class = "mdl-layout-title">Comanda Eletrônica</span>      
        </div>
      </header>

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

  	  <main class="mdl-layout__content">
        <div class="block-center">
          <p class="linha-5"></p>
          <button class="mdl-button mdl-js-button mdl-button--raised mdl-button--colored adicionar" data-upgraded=",MaterialButton">
            <i class="material-icons">add_circle</i> 
            Adicionar Comanda
          </button>
          <hr>
        </div>

        <?php foreach($comandas as $comanda):
          $n_comanda = $comanda->ven_026;
          $link = 'pedido.php?tipo=C&comanda='.$comanda->ven_026.'&venda='.$comanda->ven_001.'&cliente='.$comanda->ven_027;

          $sql = "SELECT sit_001 FROM venda WHERE sit_001 = 21 AND ven_026 = " . $comanda->ven_026;
          $stm = $pdo->prepare($sql);
          $stm->execute();
          $venda = $stm->fetchAll(PDO::FETCH_OBJ);
          $back  = (empty($venda)) ? '' : 'btn-yellow';
          $link  = (empty($venda)) ? $link : 'javascript:void(0)';
          $n_comanda  = (empty($venda)) ? $n_comanda : '';
          ?>
           <a href='<?=$link?>' class="mdl-button mdl-js-button mdl-button--raised mdl-button--colored btn-mesa <?=$back?>" data-upgraded=",MaterialButton">
           <?=$comanda->ven_026?>
          </a>
        <?php endforeach;?>
  	  </main>
      
  	</div>

  <?php include 'includes/inc_js.php'; ?>
  <?php include 'includes/inc_notiica_js.php'; ?>
  <script>
    if (localStorage.hasOwnProperty('carrinho')){
        localStorage.removeItem('carrinho')
	  }

    $(document).ready(function($) {	
        $('.adicionar').on('click', function(){

          var htmlModal = "<input autofocus type='number' id='numero' placeholder='Informe a Comanda' class='inpt-cliente text-red' autocomplete='off'><br>";
          htmlModal += "<br><input type='text' id='mesa' placeholder='Informe Nome Cliente' class='inpt-cliente text-red' autocomplete='off'>";
           
          swal({
              title: 'Comanda / Cliente',
              html: htmlModal,
              showCancelButton: true,
              confirmButtonColor: '#3085d6',
              cancelButtonColor: '#d33',
              confirmButtonText: 'Abrir Comanda',
              cancelButtonText: 'Cancelar',
              confirmButtonClass: 'btn btn-success',
              cancelButtonClass: 'btn btn-danger',
              buttonsStyling: false,
              reverseButtons: true,
              focusConfirm: true,
            }).then((result) => {
              if (result.value) {
                var comanda = $('#numero').val();
                var mesa    = $('#mesa').val();

                if (comanda != '' && numero != ''){
                    window.location = './pedido.php?tipo=C&comanda='+comanda+'&cliente='+mesa;
                }else{
                  swal(
                    'Alerta',
                    'Informe a Comanda e o Nome do Cliente !',
                    'warning'
                  )
                }
              } 
            })
        });
    });
  </script>
</body>
</html>