<?php
session_start();
require 'verifica_cookie.php';
require 'conexao.php';
require 'config.php';

$termo = (isset($_POST['termo'])) ? strtoupper($_POST['termo']) : '';

$pdo  = Conexao::getInstance();
$sql = "SELECT ven_001, ven_027, ven_026 FROM venda ";
$sql .= "WHERE sit_001 IN (8,21) AND ven_024 = 'C' ";

if ($termo != ''):
  $sql.= "AND ven_026 = '".$termo."' ";
endif;

$sql .= "ORDER BY ven_026";
$stm = $pdo->prepare($sql);
$stm->execute();
$comandas = $stm->fetchAll(PDO::FETCH_OBJ);

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

  if ($comanda->ven_027 != ''):
    $link .= '&cliente='.$comanda->ven_027;
  endif;

  $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;?>
