<?php
session_start();
///Construtoras listar
include "../phpconfig/config.php";
include "../phpconfig/valida_login.php";
include "../phpconfig/funcoes.php";
include "../includes/topo.php"; 



$sqle = mysql_query("SELECT * FROM l_estados ORDER BY estado asc");


if(isset($_GET['c'])){

  $id_cidade = $_GET['c'];
  $sqlc = mysql_query("SELECT * FROM l_bairros WHERE id_cidade = '$id_cidade' ORDER BY bairro asc");
  $sqluf = mysql_query("SELECT * FROM l_cidades WHERE id='$id_cidade'");
  $numc = mysql_num_rows($sqlc);
  $line = mysql_fetch_assoc($sqluf);
  $id_uf = $line['id_uf'];



$sql = mysql_query("SELECT * FROM empresas WHERE  id_cidade='$id_cidade'  ORDER BY id ASC");
$num2 = mysql_num_rows($sql);



}else{

  $sqlc = mysql_query("SELECT * FROM l_bairros WHERE id_cidade = '333333333' ORDER BY bairro asc");
  $sqluf = mysql_query("SELECT * FROM l_cidades WHERE id='4444444444'");
  $line = mysql_fetch_assoc($sqluf);  
    $id_uf = $line['id_uf'];


    $sql = mysql_query("SELECT * FROM empresas WHERE id_uf='555555555555555555555555555555555' AND id_cidade='5555555555555555555555555555555555555'  ORDER BY id ASC");
$num2 = mysql_num_rows($sql);



}




?>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<script type="text/javascript">
  // When the document is ready set up our sortable with it's inherant function(s)
  $(document).ready(function() {
    $("#test-list").sortable({
      handle : '.handle',
      update : function () {
      var order = $('#test-list').sortable('serialize');
      $("#info").load("process-sortable.php?"+order);  
   

      }
    });
}); 

</script>

        <ul class="breadcrumb">
          <li><a href="<?php echo sti('adm');  ?>">Home</a></li>
          <li class="active"><?php echo $num2;    ?> empresas</li>
        </ul>


        <table class="table table-bordered">

        <thead>
          <tr>
            <th  colspan='9'> 

                           
              <label class='pull-right'>
                <div id='xdata'>
                <select id='cidade' class="form-control">
              <option> </option>


              </select>  
            </div>

            </label>

              <label class='pull-right'><select id='estados' class="form-control">
             <option>- Estado -</option>

                <?php while($lin = mysql_fetch_assoc($sqle)){ ?>                
                  <option value='<?php  echo $lin['id'];  ?>' <?php if(isset($id_uf)){ if($id_uf==$lin['id']) {echo 'selected';}    } ?> ><?php  echo $lin['estado'];  ?></option>
                <?php } ?>   

              </select>  </label>
              
            </th>

          </tr>
          <tr>
   


             <th>ID</th>
             <th>Bairro</th>
             <th></th>
             <th></th>
          </tr>
        </thead>
        <tbody id='test-list'>

          <div id="info"></div>
          <?php  

          while($lin = mysql_fetch_assoc($sql)){  ?>
          <tr id="listItem_<?php echo $lin['id']; ?>">

            <?php

            $cidade = retorna($lin['id_cidade'], 'l_cidades');
            $estado = retorna($lin['id_uf'], 'l_estados');
            $bairro = retorna($lin['id_bairro'], 'l_bairros');

            ?>

            <td><?php echo $lin['id'];  ?></td>
            <td><?php echo $bairro['bairro'];  ?></td>
              <td>
              <a href="edit.php?id=<?php echo $lin['id'];  ?>" class="btn btn-default" ><span class="glyphicon glyphicon-pencil"></span></a>
              <a href="del.php?id=<?php echo $lin['id'];  ?>&c=<?php echo $id_cidade;  ?>" onclick="return confirma('Deseja deletar este cliente?');" class="btn btn-default" ><span class="glyphicon glyphicon-trash"></span></a>
            </td>
                        <td>
              <a href="../completo/edit.php?id=<?php echo $lin['id'];  ?>" class="btn btn-warning" ><span class="glyphicon glyphicon-list"></span></a>
              <a href="../galerias/index.php?id=<?php echo $lin['id'];  ?>" class="btn btn-warning" ><span class="glyphicon glyphicon-camera"></span></a>
            
            </td>
          </tr>
          <?php  } if($num2==0){echo "<tr><td  colspan='9'>Não há cadastro de empresas</td></tr>";} ?>


        </tbody>
      </table>
<script type="text/javascript">
    $(document).ready(function(){

      $("#estados").change(function(){
        var uf = $("#estados").val();

    $.ajax({   
      url: "retCidades.php?uf="+uf,
      type: 'GET',
      beforeSend: function() {    


      },
    success: function(data)
    {
 
        $('#xdata').html(data);  
        grab()

    }
  });

      });

      <?php   

if(isset($_GET['c'])){ ?>

    $.ajax({   
      url: "retCidades.php?uf=<?php echo $id_uf; ?>&c=<?php echo $id_cidade; ?>",
      type: 'GET',
      beforeSend: function() {    

      },
    success: function(data)
    {
 
        $('#xdata').html(data);  
        grab()

    }
});

<?php } ?>

      function grab(){ 

      $("#cidades").change(function(){
        var current = $("#cidades").val();
        if (current != "select") {
          var novaURL = "<?php echo sti('adm'); ?>empresas/listar-por-bairros.php?c="+current;
          $(window.document.location).attr('href',novaURL);
        };
      });

    }




    });
  </script>

<?php include "../includes/rodape.php"; ?>
