<?php
include 'header.php';
include 'erpc_pedidos_compra_toolbar.php'; //Nome da tabela _list.php
include 'connect.php';


$id_usuario = $_SESSION[SESSION_USER_NAME]['id'];

// Método para post de Query Avançada
	$consulta = "select pc.id
       ,fr.ds_razao
       ,concat('R$ ',format(pc.vl_total,2,'de_DE')) as vl_total
       ,date_format(pc.dt_lancamento, '%d/%m/%Y') as dt_lancamento
       ,st.ds_path
       ,pc.id_status_pedido
from   erpc_pedidos_compra pc 
join   erpc_fornecedores   fr
  on   pc.id_fornecedor = fr.id
join   erpc_dom_status_pedidos_compra st
  on   pc.id_status_pedido = st.id
 and   pc.st_ativo = 1";

	//Efetuando busca avançada - se passado os parametros
	if(isset($_POST['pesquisar'])) {
		$id                   = $_POST['id'];
		$ds_razao	          = trim($_POST['ds_razao']);
		$dt_lancamento	        = trim($_POST['dt_lancamento']);
		$vl_total	            = trim($_POST['vl_total']);


	  $consulta .= (!empty($id)  		? " and id like '%$id%'"            	: '');
	  $consulta .= (!empty($ds_razao)? " and ds_razao like '%$ds_razao%'"    : '');
	  $consulta .= (!empty($dt_lancamento)? " and dt_lancamento like '%$dt_lancamento%'"  : '');
	  $consulta .= (!empty($vl_total)? " and vl_total like '%$vl_total%'"  : '');


	}
	$consulta .= " order by 1"
?>

<div class="block">
  <div class="header">
  <h2>Pedidos de Compra</h2>
  <div class="side pull-right">
      <button id='btnShow' class="btn btn-default btn-clean">Legenda</button>
    </div>
  </div>
    <div class="block">
      <div class="content">
        <?php
        displayMessagesAndErrors(true);
        ?>
      </div>
    </div>

  <form method="POST" action="erpc_pedidos_compra_ws.php"  id=validate_custom >
  <input type="hidden" value="<?php echo $id_usuario; ?>" name="id_usuario" id="id_usuario" />
  <div class="content">
    <div class="table-responsive">
      <table class="table table-bordered table-striped table-responsible sortable">
        <thead>
          <tr>
            <th width=02%><input type="checkbox" class="checkall" /></th>
      			<th width=02%>ID</th>
      			<th width=30%>Fornecedor</th>
      			<th width=10%>Dt. Lançamento.</th>
      			<th width=10%>Vl. Total</th>
      			<th width=10%>Status</th>
      			<th width=03%>Excluir</th>
          </tr>
        </thead>
        <tbody>

          <?php

			$resultado = mysqli_query($conexao, $consulta) or die ($erro = mysqli_error($conexao));
			if (mysqli_num_rows($resultado)>0) {
            while ($registro = mysqli_fetch_assoc($resultado)){

            $id            = $registro['id'];
            $id_crip       = base64_encode($id);
            $ds_razao      = $registro['ds_razao'];
            $dt_lancamento = $registro['dt_lancamento'];
            $vl_total      = $registro['vl_total'];
            $ds_path       = $registro['ds_path'];
            $id_status_pedido=$registro['id_status_pedido'];

            $disabled="";
            if($id_status_pedido!="1"){
              $disabled="disabled";
            }			

			echo "<tr>";
			      echo "<td class='text-center'><input type='checkbox' " . $disabled . " value=" . $id . " name='id_pedidos[]' /></td>";
            echo "<td>$id</td>";
            echo "<td>$ds_razao</td>";
            echo "<td>$dt_lancamento</td>";
            echo "<td>$vl_total</td>";
            echo "<td><img src='$ds_path' ></td>";

            /*
            echo "<td class='text-center'><a href='erpc_pedidos_compra_edit.php?id=$id_crip'>  <i class='glyphicon glyphicon-pencil' title='Editar'><span class='sr-only'>Editar</span></i></a></td>";
            */
            echo "<td class='text-center'><a href='erpc_pedidos_compra_ws.php?remove=true&id=$id_crip'><i class='glyphicon glyphicon-trash' title='Remover'><span class='sr-only'>Remover</span></i></a></td>";
          
            echo "</tr>";
            }
          }
          ?>
        </tbody>
      </table>
    </div> <!-- table-responsive -->
	
      <div class="side pull-right">
        <div class=btn-group>
          <!--
          <button  name="btn-aprovar" class="btn btn-primary btn-cleanx" type="submit" value="faturar" onClick="$('#validate_custom').validationEngine('hide');" >Aprovar Solicitação</button>
          -->
          <button  name="btn-cancelar" class="btn btn-primary btn-cleanx" type="submit" value="faturar" onClick="$('#validate_custom').validationEngine('hide');" >Cancelar Pedidos</button>
        </div>
      </div>

	    <div class="form-row clear">&nbsp;</div>
	    <?php include 'inc/label_status_pedidos_compra.php';?>
  </div> <!-- content -->
  </form>
</div> <!-- block -->
<?php include 'footer.php';?>
<script type="text/javascript" src="js/acme.js"></script>
<style>
#div_label{
  position: absolute;
  top:0px;
  height: 200px;
  width:  300px;
  overflow: auto;
  overflow-y: hidden;
  background-color: #666666;
  scrollbar-base-color:#ffeaff
}
</style>
