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


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

/* Edival 02/04/2019 - Alteração dos labels da cotacao */
$sql_update = "update erpc_cotacao_compra as cotacao 
inner join  (
select  st.id, max(IFNULL(st_aprovada, 0)) as st_aprovada
from    erpc_cotacao_compra       st
join  erpc_cotacao_compra_itens it
  on  it.id_cotacao_compra = st.id 
where   it.st_ativo = 1
group by st.id) status_item on cotacao.id = status_item.id
set   id_status_cotacao =  case when st_aprovada =1 then 3 else 2 end
where   id_status_cotacao <> 3";
$stmt = mysqli_prepare($conexao, $sql_update);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);

// Método para post de Query Avançada
$consulta = "select   cc.id as id_cotacao_compra,
                      solicitante.ds_nome as solicitante ,
                      comprador.ds_nome as comprador ,
                      responsavel.ds_nome as responsavel ,
                      date_format(cc.dt_cotacao,'%d/%m/%Y') as dt_cotacao , 
                      IFNULL(cc.id_status_cotacao, 1) as id_status_cotacao,
                      st.ds_path
            from erpc_cotacao_compra cc 
            left join erpc_usuarios as comprador 
            on cc.id_comprador = comprador.id 
            left join erpc_usuarios as responsavel 
            on cc.id_responsavel_aprov = responsavel.id 
            left join erpc_solicitacao_compra sc 
            on cc.id_solicitacao = sc.id 
            left join erpc_usuarios as solicitante 
            on sc.id_solicitante = solicitante.id 
            left join erpc_dom_status_cotacao_compra st
              ON  IFNULL(cc.id_status_cotacao, 1) =  st.id
            where cc.st_ativo = 1 and sc.id_status_solicitacao = 2";

              // echo $consulta;

	//Efetuando busca avançada - se passado os parametros
	if(isset($_POST['pesquisar'])) {
		$id_cotacao_compra                   = $_POST['id_cotacao_compra'];
		$solicitante	        = trim($_POST['solicitante']);
		$dt_cotacao	          = trim($_POST['dt_cotacao']);
		$comprador	          = trim($_POST['comprador']);
		$responsavel	        = trim($_POST['responsavel']);
	//	$st_aprovada	        = trim($_POST['st_aprovada']);


	  $consulta .= (!empty($id_cotacao_compra)  		           ? " and id_cotacao_compra like '%$id_cotacao_compra%'"            	: '');
	  $consulta .= (!empty($solicitante)               ? " and solicitante like '%$solicitante%'"    : '');
	  $consulta .= (!empty($dt_cotacao)            ? " and dt_cotacao like '%$dt_cotacao%'"  : '');
	  $consulta .= (!empty($comprador)              ? " and comprador like '%$comprador%'"  : '');
	  $consulta .= (!empty($responsavel)    ? " and responsavel like '%$responsavel%'"  : '');
	//  $consulta .= (!empty($st_aprovada)           ? " and st_aprovada like '%$st_aprovada%'"  : '');


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

<div class="block">
  <div class="header">
  <h2>Cotação 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_cotacoes_agrupar.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%>Solicitante</th>
  			    <th width=10%>Comprador</th>
  			    <th width=10%>Aprovador</th>
            <th width=10%>Dt. Emissão</th>
            <th width=03%>Status</th>
            <th width=03%>Cotar</th>
            <th width=03%>Aprovar</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_cotacao_compra  = $registro['id_cotacao_compra'];
            $id_crip            = base64_encode($id_cotacao_compra);

            $solicitante  = $registro['solicitante'];
            $comprador    = $registro['comprador'];
            $responsavel  = $registro['responsavel'];
            $dt_cotacao   = $registro['dt_cotacao'];
            $id_status_cotacao = $registro['id_status_cotacao'];
            $ds_path  = $registro['ds_path'];

			echo "<tr>";
			
              $disabled = "disabled";
            if($id_status_cotacao=="3"){
              $disabled = "";
            }
            echo "<td class='text-center'>
                    <input type='checkbox' $disabled value='$id_cotacao_compra' name='id_cotacao_compra[]'/></td>";
            echo "<td>$id_cotacao_compra</td>";
            echo "<td>$solicitante</td>";
            echo "<td>$comprador</td>";
            echo "<td>$responsavel</td>";
            echo "<td>$dt_cotacao</td>";

            echo "<td><img width='24px' heigth='24px' src='$ds_path'/></td>";

            if($id_status_cotacao=="3"){
                echo "<td>&nbsp;</td>";
                echo "<td>&nbsp;</td>";
            }else if($id_status_cotacao=="1"){
                echo "<td class='text-center'><a href='erpc_cotacao_compra_cotar.php?id_cotacao_compra=$id_crip&id=$id_crip'>  <i class='glyphicon glyphicon-pencil' title='Cotar e Incluir'><span class='sr-only'>Cotar</span></i></a></td>";
                echo "<td>&nbsp;</td>";
            }else if($id_status_cotacao=="2"){
                echo "<td class='text-center'><a href='erpc_cotacao_compra_cotar.php?id_cotacao_compra=$id_crip&id=$id_crip'>  <i class='glyphicon glyphicon-pencil' title='Cotar e Incluir'><span class='sr-only'>Cotar</span></i></a></td>";
                
                echo "<td class='text-center'><a href='erpc_cotacao_compra_aprovar.php?id=$id_crip'>  <i class='glyphicon glyphicon-ok' title='Aprovar'><span class='sr-only'>Aprovar</span></i></a></td>";
            }


            if($id_status_cotacao!="3"){
                echo "<td class='text-center'><a href='erpc_cotacao_compra_ws.php?remover=true&id=$id_crip'><i class='glyphicon glyphicon-trash' title='Remover'><span class='sr-only'>Remover</span></i></a></td>";
            }else{
                echo "<td>&nbsp;</td>";
            }

            /*
            echo "<td class='text-center'><a href='erpc_cotacao_compra_detalhe.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_cotacao_compra_remove.php?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-agrupar" class="btn btn-primary btn-cleanx" type="submit" value="agrupar" onClick="$('#validate_custom').validationEngine('hide');" >Agrupar Pedidos por Fornecedor</button>
              <!--
    		   <button  name="btn-cancelar" class="btn btn-primary btn-cleanx" type="submit" value="faturar" onClick="$('#validate_custom').validationEngine('hide');" >Cancelar Cotação</button> -->
	      </div>
	    </div>
	    <div class="form-row clear">&nbsp;</div>
	    <?php include 'inc/label_status_cotacao.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: 165px;
  width:  300px;
  overflow: auto;
  overflow-y: hidden;
  background-color: #666666;
  scrollbar-base-color:#ffeaff
}
</style>
