<?php
include("seguranca.php"); // Inclui o arquivo com o sistema de segurança  
protegePagina(); // Chama a função que protege a página 
?>
<?php
include "conexao.php";
?>
<!DOCTYPE html>
<html lang="en">
    <head>        
        <!-- META SECTION -->
        <title>Portal dos Patrocinadores - Sistema Administrativo</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        
        <link rel="icon" href="favicon.ico" type="image/x-icon" />
        <!-- END META SECTION -->
        
        <!-- CSS INCLUDE -->        
        <link rel="stylesheet" type="text/css" id="theme" href="css/theme-default.css"/>
        <!-- EOF CSS INCLUDE -->                                   
    </head>
    <body>
        <!-- START PAGE CONTAINER -->
        <div class="page-container">
            
            <?php include 'topo.php'; ?>
                <!-- END X-NAVIGATION VERTICAL -->                     
                
        
                <ul class="breadcrumb">
                    <li><a href="#">Home</a></li>                    
                    <li class="active">Empresas</li>
                </ul>
				                
                
                <!-- PAGE CONTENT WRAPPER -->
                <div class="page-content-wrap" style="min-height: 1200px;">
						
                   
					 <div class="col-md-12">

                         <form action="empresas.php" method="get" enctype="multipart/form-data" target="_self">


                             <div class="panel panel-default panel-toggled">
                                 <div class="panel-heading">
                                     <h3 class="panel-title">Filtrar Empresas</h3>
                                     <ul class="panel-controls">
                                         <li><a href="#" class="panel-collapse"><span class="fa fa-angle-down"></span></a></li>
                                         <li><a href="#" class="panel-fullscreen"><span class="fa fa-expand"></span></a></li>
                                     </ul>


                                 </div>
                                 <div class="panel-body">


                                     <div class="col-sm-6" style="margin-top: 15px;">
                                         <div class="form-group">
                                             <label class="control-label">Nome da Empresa</label>
                                             <input type="text" class="form-control" name="nome" placeholder="Nome da Empresa" />
                                         </div>
                                     </div>


                                     <div class="col-sm-6" style="margin-top: 15px;">
                                         <div class="form-group">
                                             <label class="control-label">Setores</label>
                                             <select class="form-control" name="id_setor">
                                                 <option value="">Selecione</option>
                                                 <?php
                                                 $sql_setores = $sqli->query("SELECT * FROM setores WHERE deleted_at IS NULL ORDER BY setor ASC");
                                                 while($row_setores = $sql_setores->fetch_assoc()) {
                                                     ?>
                                                     <option value="<?php echo $row_setores['id'] ?>"><?php echo $row_setores['setor'] ?></option>
                                                     <?php
                                                 }
                                                 ?>
                                             </select>
                                         </div>
                                     </div>

                                     <div class="col-sm-6" style="margin-top: 15px;">
                                         <div class="form-group">
                                             <label class="control-label">Estado</label>
                                             <select class="form-control" name="estado">
                                                 <option value="">Selecione</option>
                                                 <option value="AM">AM</option>
                                                 <option value="BA">BA</option>
                                                 <option value="CE">CE</option>
                                                 <option value="DF">DF</option>
                                                 <option value="ES">ES</option>
                                                 <option value="GO">GO</option>
                                                 <option value="MA">MA</option>
                                                 <option value="MG">MG</option>
                                                 <option value="MS">MS</option>
                                                 <option value="MT">MT</option>
                                                 <option value="PA">PA</option>
                                                 <option value="PB">PB</option>
                                                 <option value="PE">PE</option>
                                                 <option value="PI">PI</option>
                                                 <option value="PR">PR</option>
                                                 <option value="RJ">RJ</option>
                                                 <option value="RN">RN</option>
                                                 <option value="RO">RO</option>
                                                 <option value="RS">RS</option>
                                                 <option value="SC">SC</option>
                                                 <option value="SE">SE</option>
                                                 <option value="SP">SP</option>
                                                 <option value="TO">TO</option>
                                             </select>
                                         </div>
                                     </div>

                                     <div class="col-sm-6" style="margin-top: 15px;">
                                         <div class="form-group">
                                             <label class="control-label">Grupo</label>
                                             <select class="form-control" name="id_grupo">
                                                 <option value="">Selecione</option>
                                                 <?php
                                                 $sql_grupos = $sqli->query("SELECT * FROM grupos WHERE deleted_at IS NULL ORDER BY grupo ASC");
                                                 while($row_grupos = $sql_grupos->fetch_assoc()) {
                                                     ?>
                                                     <option value="<?php echo $row_grupos['id'] ?>"><?php echo $row_grupos['grupo'] ?></option>
                                                     <?php
                                                 }
                                                 ?>
                                             </select>
                                         </div>
                                     </div>


                                     <div class="col-sm-6" style="margin-top: 15px;">
                                         <div class="form-group">
                                             <label class="control-label">Transparência (estrelas)</label>
                                             <select class="form-control" name="transparencia">
                                                 <option value="">Selecione</option>
                                                 <option value="0">0</option>
                                                 <option value="1">1</option>
                                                 <option value="2">2</option>
                                                 <option value="3">3</option>
                                                 <option value="4">4</option>
                                                 <option value="5">5</option>
                                             </select>
                                         </div>
                                     </div>


                                     <div class="col-sm-6" style="margin-top: 15px;">
                                         <div class="form-group">
                                             <label class="control-label">Atividade</label>
                                             <select class="form-control" name="id_atividade">
                                                 <option value="">Selecione</option>
                                                 <?php
                                                 $sql_atividades = $sqli->query("SELECT * FROM atividades WHERE deleted_at IS NULL ORDER BY atividade ASC");
                                                 while($row_atividades = $sql_atividades->fetch_assoc()) {
                                                     ?>
                                                     <option value="<?php echo $row_atividades['id'] ?>"><?php echo $row_atividades['atividade'] ?></option>
                                                     <?php
                                                 }
                                                 ?>
                                             </select>
                                         </div>
                                     </div>


                                     <div class="col-sm-6" style="margin-top: 15px;">
                                         <div class="form-group">
                                             <label class="control-label">Tem politica de patrocinio?</label>
                                             <select class="form-control" name="politica_patrocinio">
                                                 <option value="">Selecione</option>
                                                 <option value="1">Sim</option>
                                                 <option value="0">Não</option>
                                             </select>
                                         </div>
                                     </div>

                                     <div class="col-sm-6" style="margin-top: 15px;">
                                         <div class="form-group">
                                             <label class="control-label">Segmentos</label>
                                             <select class="form-control" name="id_segmento">
                                                 <option value="">Selecione</option>
                                                 <?php
                                                 $sql_segmentos = $sqli->query("SELECT * FROM segmentos WHERE deleted_at IS NULL ORDER BY segmento ASC");
                                                 while($row_segmentos = $sql_segmentos->fetch_assoc()) {
                                                     ?>
                                                     <option value="<?php echo $row_segmentos['id'] ?>"><?php echo $row_segmentos['segmento'] ?></option>
                                                     <?php
                                                 }
                                                 ?>
                                             </select>
                                         </div>
                                     </div>

                                     <div class="col-sm-6" style="margin-top: 15px;">
                                         <div class="form-group">
                                             <label class="control-label">Investe em Esportes?</label>
                                             <select class="form-control" name="esportes">
                                                 <option value="">Selecione</option>
                                                 <option value="Sim">Sim</option>
                                                 <option value="Não">Não</option>
                                             </select>
                                         </div>
                                     </div>

                                     <div class="col-sm-6" style="margin-top: 15px;">
                                         <div class="form-group">
                                             <label class="control-label">Investe em Sociocultural?</label>
                                             <select class="form-control" name="sociocultura">
                                                 <option value="">Selecione</option>
                                                 <option value="Sim">Sim</option>
                                                 <option value="Não">Não</option>
                                             </select>
                                         </div>
                                     </div>




                                 </div>


                                 <div class="panel-footer">
                                     <button type="submit" class="btn btn-success pull-right"><span class="fa fa-search"></span> Pesquisar</button>
                                 </div>


                             </div>


                         </form>



<?php
$b1 = ""; $b2 = ""; $b3 = ""; $b4 = ""; $b5 = ""; $b6 = ""; $b7 = "";
$groups = ""; $tables = "empresas"; 
$b8 = ""; $b9 = ""; $b10 = "";
$b11 = "ORDER BY empresas.nome ASC";

if(array_key_exists('nome', $_GET) && $_GET['nome'] != "") { $b1 = "AND empresas.nome LIKE '%" . $_GET['nome'] . "%'"; }
if(array_key_exists('id_setor', $_GET) && $_GET['id_setor'] != "") { $b2 = "AND empresas.id_setor='".$_GET['id_setor']."'"; }
if(array_key_exists('estado', $_GET) && $_GET['estado'] != "") { $b3 = "AND empresas.estado='".$_GET['estado']."'"; }
if(array_key_exists('id_grupo', $_GET) && $_GET['id_grupo'] != "") { $b4 = "AND empresas.id_grupo='".$_GET['id_grupo']."'"; }
if(array_key_exists('transparencia', $_GET) && $_GET['transparencia'] != "") { $b5 = "AND empresas.transparencia='".$_GET['transparencia']."'"; }
if(array_key_exists('id_atividade', $_GET) && $_GET['id_atividade'] != "") { $b6 = "AND empresas.id_atividade='".$_GET['id_atividade']."'"; }
if(array_key_exists('politica_patrocinio', $_GET) && $_GET['politica_patrocinio'] != "") { $b7 = "AND empresas.politica_patrocinio='".$_GET['politica_patrocinio']."'"; }
if(array_key_exists('id_segmento', $_GET) && $_GET['id_segmento'] != "") {
    $groups = "AND segmentos_empresa.id_empresa=empresas.id GROUP BY segmentos_empresa.id_empresa";
    $tables = "empresas, segmentos_empresa";
    $b8 = "AND segmentos_empresa.id_segmento='".$_GET['id_segmento']."'";
}
if(array_key_exists('esportes', $_GET) && $_GET['esportes'] != "") { $b9 = "AND empresas.esportes='".$_GET['esportes']."'"; }
if(array_key_exists('sociocultura', $_GET) && $_GET['sociocultura'] != "") { $b10 = "AND empresas.sociocultura='".$_GET['sociocultura']."'"; }
if(array_key_exists('ordenacao', $_GET) && $_GET['ordenacao'] != "") { $b11 =  "ORDER BY empresas.".$_GET['ordenacao']." ASC"; }

?>

					 
				<div class="panel panel-default">
                                <div class="panel-heading">                                
                                    <h3 class="panel-title">Empresas</h3>

                                    <ul class="panel-controls">
                                        <li><a href="#" class="panel-collapse"><span class="fa fa-angle-down"></span></a></li>
								        <li><a href="#" class="panel-fullscreen"><span class="fa fa-expand"></span></a></li>
                                    </ul>
                                    <a href="cadastro_empresa.php" target="_self" class="btn btn-success pull-right"><i class="fa fa-plus"></i> Adicionar Empresa</a>
                                </div>
                                <div class="panel-body">
                                    <div class="table-responsive">
                                        <table class="table table-actions tabledata">
                <thead>
                <tr>
                    <th>Nome</th>
                    <th>Razão Social</th>
                    <th>Cidade / UF</th>
                    <th>Atividade</th>
                    <th>&nbsp;</th>
                    <th>&nbsp;</th>
                    <th>&nbsp;</th>
                    <th>&nbsp;</th>
                  </tr>
                </thead>
                                            <tbody>
                                            <?php
                                            $sql_consult2 = $sqli->query("SELECT empresas.* FROM $tables WHERE empresas.deleted_at IS NULL $b1 $b2 $b3 $b4 $b5 $b6 $b7 $b8 $b9 $b10 $groups $b11");
                                            while ($dados = $sql_consult2->fetch_assoc()) {
                                                $id = $dados['id'];

                                                $sql_atividade = $sqli->query("SELECT * FROM atividades WHERE id='".$dados['id_atividade']."'");
                                                $row_atividade = $sql_atividade->fetch_assoc();

                                                ?>
                                                <tr>
                                                    <td><?php echo $dados['nome'] ?></td>
                                                    <td><?php echo $dados['razao_social'] ?></td>
                                                    <td><?php echo $dados['cidade'] ?> / <?php echo $dados['estado'] ?></td>
                                                    <td><?php echo $row_atividade['atividade'] ?></td>
                                                    <td>
                                                        <a href="visua_empresa.php?id=<?php echo $id ?>" target="_blank"><button class="btn btn-sm btn-success"><span class="fa fa-search"></span></button></a>
                                                    </td>
                                                    <td>
                                                        <a href="ficha_empresa.php?id=<?php echo $id ?>" target="_blank"><button class="btn btn-sm btn-warning"><span class="fa fa-edit"></span></button></a>
                                                    </td>
                                                    <td>
                                                        <a href="export_word.php?id=<?php echo $id ?>" target="_blank"><button class="btn btn-sm btn-primary"><span class="fa fa-file-text-o"></span> Word</button></a>
                                                    </td>
                                                    <td>
                                                        <a data-toggle="modal" data-target="#pop_excluir_<?php echo $id ?>"><button class="btn btn-sm btn-success"><span class="fa fa-trash-o"></span></button></a>
                                                    </td>
                                                </tr>


                                                <div class="modal modal-primary" id="pop_excluir_<?php echo $id ?>">
                                                    <form action="delete_empresa.php" method="post" enctype="multipart/form-data" target="_self">
                                                        <div class="modal-dialog">
                                                            <div class="modal-content">
                                                                <div class="modal-header">
                                                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                                        <span aria-hidden="true">×</span></button>
                                                                    <h4 class="modal-title">Excluir Empresa</h4>
                                                                </div>
                                                                <div class="modal-body">

                                                                    <div class="row">

                                                                        <div class="col-md-12">
                                                                            <p>Tem certeza que deseja excluir a Empresa <?php echo $dados['nome'] ?>?</p>
                                                                        </div>

                                                                        <input type="hidden" value="<?php echo $id ?>" name="id"/>

                                                                    </div>
                                                                </div>
                                                                <div class="modal-footer">
                                                                    <input type="submit" class="btn btn-success pull-right" value="Sim" />
                                                                    <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Não</button>
                                                                </div>
                                                            </div>
                                                            <!-- /.modal-content -->
                                                        </div>
                                                        <!-- /.modal-dialog -->
                                                    </form>
                                                </div>



                                                <?php
                                            }
                                            ?>

                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>					 
					 
					 
					 
					 
					 </div>




					
					
                </div>
                <!-- END PAGE CONTENT WRAPPER -->                
            </div>            
            <!-- END PAGE CONTENT -->

        <!-- END PAGE CONTAINER -->

        
        <!-- START PRELOADS -->
        <script type="text/javascript" src="js/plugins/jquery/jquery.min.js"></script>
        <script type="text/javascript" src="js/plugins/jquery/jquery-ui.min.js"></script>
        <script type="text/javascript" src="js/plugins/bootstrap/bootstrap.min.js"></script>        
        <!-- END PLUGINS -->

        <!-- THIS PAGE PLUGINS -->
        <script type='text/javascript' src='js/plugins/icheck/icheck.min.js'></script>
        <script type="text/javascript" src="js/plugins/mcustomscrollbar/jquery.mCustomScrollbar.min.js"></script>
        
        <script type="text/javascript" src="js/plugins/datatables/jquery.dataTables.min.js"></script>    
        

        <!-- END PAGE PLUGINS -->         

        <!-- START TEMPLATE -->
        <script type="text/javascript" src="js/plugins.js"></script>        
        <script type="text/javascript" src="js/actions.js"></script>

        <script src="js/jquery.maskedinput.js"></script>
        <script src="js/jquery.maskMoney.js" type="text/javascript"></script>

		<script src="js/valor.js"></script>
		
		
		<script>
  $(function () {

      $('.tabledata').DataTable({
          "paging": true,
          "lengthChange": true,
          "searching": true,
          "ordering": true,
          "info": true,
          "autoWidth": true,
          "order": [[ 0, "asc" ]]
      });

  });
  </script>



        <!-- END TEMPLATE -->
    <!-- END SCRIPTS -->       
    </body>
</html>






