<?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>
	<head>

		<!-- Basic -->
		<meta charset="UTF-8">

		<title>Portal dos Patrocinadores</title>
		<meta name="keywords" content="HTML5 Admin Template" />
		<meta name="description" content="Porto Admin - Responsive HTML5 Template">
		<meta name="author" content="okler.net">

		<!-- Mobile Metas -->
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

		<!-- Web Fonts  -->
		<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800|Shadows+Into+Light" rel="stylesheet" type="text/css">

        <!-- Vendor CSS -->
        <link rel="stylesheet" href="assets/vendor/bootstrap/css/bootstrap.css" />
        <link rel="stylesheet" href="assets/vendor/font-awesome/css/font-awesome.css" />
        <link rel="stylesheet" href="assets/vendor/magnific-popup/magnific-popup.css" />
        <link rel="stylesheet" href="assets/vendor/bootstrap-datepicker/css/datepicker3.css" />


        <!-- Theme CSS -->
        <link rel="stylesheet" href="assets/stylesheets/theme.css" />

        <!-- Skin CSS -->
        <link rel="stylesheet" href="assets/stylesheets/skins/default.css" />

        <!-- Theme Custom CSS -->
        <link rel="stylesheet" href="assets/stylesheets/theme-custom.css">

		<!-- Head Libs -->
		<script src="assets/vendor/modernizr/modernizr.js"></script>

	</head>
	<body>
		<section class="body">

			<!-- start: header -->
<?php
include 'topo.php';
?>
				<!-- end: sidebar -->


            <?php

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

            $sql_empresas = $sqli->query("SELECT empresas.* FROM $tables WHERE empresas.deleted_at IS NULL $b1 $b2 $b3 $b4 $b5 $b6 $b7 $b8 $b9 $b10 $groups $b11");
            $n_empresas = $sql_empresas->num_rows;
            ?>





            <section role="main" class="content-body">
					<header class="page-header">
						<h2>Empresas</h2>
                        <div class="right-wrapper pull-right">
                            <ol class="breadcrumbs">
                                <li>
                                    <a>
                                        <i class="fa fa-database"></i>
                                        Total de <?php echo $n_empresas ?> Empresas
                                    </a>
                                </li>
                            </ol>

                            <a class="sidebar-right-toggle" href="#modalAnim"><i class="fa fa-search"></i> Filtros</a>
                        </div>
					</header>


                <div id="modalAnim" class="zoom-anim-dialog modal-header-color modal-block-lg modal-block modal-block-success mfp-hide">
                    <form action="index.php" method="post" enctype="multipart/form-data">
                    <section class="panel">
                        <header class="panel-heading">
                            <h2 class="panel-title">Filtrar Empresas</h2>
                        </header>
                        <div class="panel-body">
                            <div class="row" style="margin-bottom: 20px;">

                                <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 class="col-sm-12" style="margin-top: 15px;">
                                    <div class="form-group">
                                        <label class="control-label" for="inputSuccess">Ordenação:</label>
                                            <label class="checkbox-inline">
                                                <input type="radio" name="ordenacao" value="nome"> Nome
                                            </label>
                                            <label class="checkbox-inline">
                                                <input type="radio" name="ordenacao" value="data_abertura"> Data de abertura
                                            </label>
                                    </div>
                                </div>


                            </div>
                        </div>
                        <footer class="panel-footer">
                            <div class="row">
                                <div class="col-md-12 text-right">
                                    <button type="submit" class="btn btn-success pull-right" style="margin-left: 10px;"><i class="fa fa-search" style="color:#fff!important;"></i> Pesquisar</button>
                                    <a href="index.php" class="btn btn-warning pull-right"><i class="fa fa-refresh" style="color:#fff!important;"></i> Limpar</a>
                                    <button class="btn btn-default modal-dismiss pull-left">Fechar</button>
                                </div>
                            </div>
                        </footer>
                    </section>
                </form>
                </div>





                    <input type="hidden" id="row" value="0">
                    <input type="hidden" id="todas_empresas" value="<?php echo $n_empresas; ?>"

					<!-- start: page -->

					<div class="row">



                        <?php
                        $sql_consult = $sqli->query("SELECT empresas.* FROM $tables WHERE empresas.deleted_at IS NULL $b1 $b2 $b3 $b4 $b5 $b6 $b7 $b8 $b9 $b10 $groups $b11 LIMIT 0, 60");
                        while($row_consult = $sql_consult->fetch_assoc()) {
                        ?>

                        <div class="col-md-4 col-xl-12 empresas">
                            <section class="panel panel-success bt-empresa" data-nome="<?php echo $row_consult['nome'] ?>" data-id="<?php echo $row_consult['id'] ?>"  style="cursor: pointer; min-height: 79px!important;">
                                <header class="panel-heading" style="border-radius: 5px 5px 5px 5px;">
                                    <h2 class="panel-title" style="font-size: 2.1rem; font-weight: bold;"><?php echo $row_consult['nome'] ?></h2>
                                </header>
                            </section>
                            </div>
                        <?php
                        }
                        ?>

					</div>

                    <div class="row" id="loader">
                        <div class="col-md-12">
                            <div class="bars-cont bars-cont1">
                                <div class="load-bar"></div>
                                <div class="load-bar"></div>
                                <div class="load-bar"></div>
                                <div class="load-bar"></div>
                                <div class="load-bar"></div>
                                <div class="load-bar"></div>
                                <div class="load-bar"></div>
                                <div class="load-bar"></div>
                            </div>
                        </div>
                    </div>
					
					


					<!-- end: page -->
				</section>
			</div>

		</section>




        <div id="popup-empresa" class="zoom-anim-dialog modal-header-color modal-block-full modal-block modal-full-color modal-block-warning mfp-hide">
                <section class="panel" style="background: #efefef;">
                    <header class="panel-heading">
                        <h2 class="panel-title" id="titulo-empresa" style="font-weight: bold;"></h2>
                    </header>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-md-12" id="visu-empresa">

                            </div>
                        </div>
                    </div>
                </section>
        </div>









        <!-- Vendor -->
        <script src="assets/vendor/jquery/jquery.js"></script>
        <script src="assets/vendor/jquery-browser-mobile/jquery.browser.mobile.js"></script>
        <script src="assets/vendor/bootstrap/js/bootstrap.js"></script>
        <script src="assets/vendor/nanoscroller/nanoscroller.js"></script>
        <script src="assets/vendor/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
        <script src="assets/vendor/magnific-popup/magnific-popup.js"></script>
        <script src="assets/vendor/jquery-placeholder/jquery.placeholder.js"></script>

        <!-- Specific Page Vendor -->
        <script src="assets/vendor/pnotify/pnotify.custom.js"></script>

        <!-- Theme Base, Components and Settings -->
        <script src="assets/javascripts/theme.js"></script>

        <!-- Theme Custom -->
        <script src="assets/javascripts/theme.custom.js"></script>

        <!-- Theme Initialization Files -->
        <script src="assets/javascripts/theme.init.js"></script>



        <script>
            let bars = document.querySelectorAll('.load-bar');
            console.log(bars);

            for(let i = 0; i <bars.length; i++){
                bars[i].style.animationDelay = `${i/15}s`;
            }


            $(document).on('click','.bt-empresa',function() {

                $('#visu-empresa').empty();
                $('#titulo-empresa').text($(this).data('nome'));
                $('#visu-empresa').load('ficha_empresa.php?id='+$(this).data('id'));

                $.magnificPopup.open({
                    items: {
                        type: 'inline',

                        fixedContentPos: false,
                        fixedBgPos: true,

                        overflowY: 'auto',

                        closeBtnInside: true,
                        preloader: false,

                        midClick: true,
                        removalDelay: 300,
                        mainClass: 'my-mfp-zoom-in',
                        modal: true,
                        src: '#popup-empresa'
                    }
                });

                var PluginToggle = function($el, opts) {
                    return this.initialize($el, opts);
                };

                PluginToggle.defaults = {
                    duration: 350,
                    isAccordion: false,
                    addIcons: true
                };
            });


            $(document).ready(function () {
                $('#loader').hide();

                $('.sidebar-right-toggle').magnificPopup({
                    type: 'inline',

                    fixedContentPos: false,
                    fixedBgPos: true,

                    overflowY: 'auto',

                    closeBtnInside: true,
                    preloader: false,

                    midClick: true,
                    removalDelay: 300,
                    mainClass: 'my-mfp-zoom-in',
                    modal: true
                });

                $(document).on('click', '.modal-dismiss', function (e) {
                    e.preventDefault();
                    $.magnificPopup.close();
                });



                $(window).scroll(function(){
                    var position = $(window).scrollTop();
                    var bottom = $(document).height() - $(window).height();
                    console.log(bottom, ' - ',(position + 60));
                    if( (position + 60) > bottom ){
                        var row = Number($('#row').val());
                        var allcount = Number($('#todas_empresas').val());
                        var rowperpage = 60;
                        row = row + rowperpage;
                        if(row <= allcount){
                            $('#loader').show();
                            var url = "list_empresa.php";
                            $('#row').val(row);
                            $.ajax({
                                url: url,
                                type: 'post',
                                data: {
                                    row: row,
                                    nome: '<?php echo $_POST['nome'] ?>',
                                    id_setor: '<?php echo $_POST['id_setor'] ?>',
                                    estado: '<?php echo $_POST['estado'] ?>',
                                    id_grupo: '<?php echo $_POST['id_grupo'] ?>',
                                    transparencia: '<?php echo $_POST['transparencia'] ?>',
                                    id_atividade: '<?php echo $_POST['id_atividade'] ?>',
                                    politica_patrocinio: '<?php echo $_POST['politica_patrocinio'] ?>',
                                    id_segmento: '<?php echo $_POST['id_segmento'] ?>',
                                    ordenacao: '<?php echo $_POST['ordenacao'] ?>'
                                },
                                success: function(response){
                                    $('#loader').hide();
                                    $(".empresas:last").after(response).show().fadeIn("slow");
                                }
                            });
                        }
                        else{
                            $('#loader').hide();
                        }
                    }
                });

            })

        </script>

		<!-- Examples -->


	</body>
</html>