<?php
include 'header.php';
include 'erpc_clientes_toolbar.php';
require_once 'inc/clientes.php';
require_once 'inc/usuario.php';

checkLogin('login.php');

?>

<div class="block">
  <div class="header">
    <h2>Cliente - Lista</h2>
  </div>
  <div class="content">
    <?php
    //echo '<pre>' . var_export($_SESSION, true) . '</pre>';
    displayMessagesAndErrors();
    ?>
    <div class="table-responsive">
      <table class="table table-bordered table-striped table-responsible code-sortable" data-order-by="0">
        <thead>
          <tr>
            <!--<th width="3%"><input type="checkbox" class="checkall" /></th>-->
            <th width="05%" >ID</th>
            <th width="30%" >Razão Social</th>
            <th width="30%" >Fantasia</th>
            <th width="18%">Telefone</th>
            <th width="03%" class="no-sort">Vender</th>
            <th width="03%" class="no-sort">Editar</th>
            <th width="03%" class="no-sort">Remover</th>
          </tr>
        </thead>
        <tbody>
          <?php
          $sql = 'select * from erpc_clientes where st_ativo=? ';
          $values[] = '1';
          
          if(isset($_POST['pesquisar'])){

              if(!empty($_POST['ds_razao'])) {
                  $ds_razao = $_POST['ds_razao'];
                  $sql .= " and ds_nome like '%$ds_razao%'";
              }
              if(!empty($_POST['ds_fantasia'])) {
                  $ds_fantasia = $_POST['ds_fantasia'];
                  $sql .= " and ds_fantasia like '%$ds_fantasia%'";
              }
              if(!empty($_POST['ds_documento'])) {
                  $ds_documento = $_POST['ds_documento'];
                  $sql .= " and ds_cnpj_cpf= '$ds_documento'";
              }
              if(!empty($_POST['ds_contato'])) {
                  $ds_contato = $_POST['ds_contato'];
                  $sql .= " and ds_contato like '%$ds_contato%'";
              }
              if(!empty($_POST['id_municipio'])) {
                  $id_municipio = $_POST['id_municipio'];
                  $sql .= " and id_municipio= '$id_municipio'";
              }
              if(!empty($_POST['ds_bairro'])) {
                  $ds_bairro = $_POST['ds_bairro'];
                  $sql .= " and ds_bairro like '%$ds_bairro%'";
              }
              if(!empty($_POST['ds_texto1'])) {
                  $ds_texto1 = $_POST['ds_texto1'];
                  $sql .= " and ds_texto1 like '%$ds_texto1%'";
              }
              
              if(!empty($_POST['id_categoria_cliente'])) {
                  $id_categoria_cliente = $_POST['id_categoria_cliente'];
                  $sql .= " and id_categoria_cliente= '$id_categoria_cliente'";
              }
              //echo "<br>" . $sql . "<br>";
                             
          }
          
          
          
          
          
          
          
          
          
          
          
          
          $lista = _prepareAndQuery($sql, $values);
          
          
          
          
          
          if ($lista !== false) {
            foreach($lista as $item) {
              //$idUrl = urlencode(base64_encode($item['id']));
              $idUrl = urlencode(base64_encode($item['id']));
              $cpf_cnpj = urlencode(base64_encode($item['ds_cnpj_cpf']));
              ?>
          <tr>
            <td><?= sgcws_html($item['id']); ?></td>
            <td><?= sgcws_html($item['ds_nome']); ?></td>
            <td><?= sgcws_html($item['ds_fantasia']); ?></td>
            <td><?= sgcws_html($item['ds_contato']); ?></td>
            
            <td class='text-center'>
              <a href="erpc_venda_rapida_add.php?id_c=<?= $idUrl; ?>"><i class="glyphicon glyphicon-usd" title='Vender'><span class="sr-only">Vender</span></i></a>
            </td>
            <td class='text-center'>
              <a href="erpc_clientes_edit.php?id=<?= $idUrl; ?>"><i class="glyphicon glyphicon-pencil" title='Editar'><span class="sr-only">Editar</span></i></a>
            </td>
            <span class="glyphicons glyphicons-usd"></span>
            <td class='text-center'>
              <a href="erpc_clientes_remove.php?id=<?= $idUrl; ?>"><i class="glyphicon glyphicon-remove" title='Remover'><span class="sr-only">Remover</span></i></a>
            </td>
          </tr>
              <?php
            }
          }
          ?>
        </tbody>
      </table>
    </div> <!-- table-responsive -->
  </div> <!-- content -->
</div> <!-- block -->

<?php
include 'footer.php';
?>
