<?php
session_start();
if(strpos(strtolower($_SERVER['REQUEST_URI']), 'phpsessid') !== false)
{
  session_destroy();
  session_regenerate_id();
  session_start();
                              
}
///Construtoras listar
include "../phpconfig/config.php";
include "../phpconfig/valida_login.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'];


}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'];

}

$num  =  mysql_num_rows($sqlc);

?>

<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-city.php?"+order);  
   

      }
    });
}); 

</script>

        <ul class="breadcrumb">
          <li><a href="<?php echo sti('adm');  ?>">Home</a></li>
          <li>Cidades</li>
          <li class="active"><?php if(isset($_GET['c'])){ echo $line['cidade'].' ('.$numc.')';} else{ echo 'Escolha uma Cidade';}  ?></li>
        </ul>


        <table class="table table-bordered">

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

              <!--a href="add.php" class="btn btn-success" >Adicionar novo bairro <i class="icon-plus icon-white"></i></a-->
              
              <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>MOVER</th> -->

            <th>Nome</th>  

            <th>Slug</th>   
  
<th>ID</th>
             <th>Ação</th>

             <!-- <th>Deletar</th> -->
          </tr>
        </thead>
        <tbody id='test-list'>

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

          <tr><td  colspan='5'>
            <label>Adicionar a cidades separadas por vírgula.</label>
            <form action='adicionab.php' method='post'>
               <div class="form-group">
              <input type='hidden' name='id_uf' value='<?php echo $id_uf; ?>'  />
              <input type='hidden' name='id_cidade' value='<?php echo $id_cidade; ?>'  />
              <textarea class='form-control' name='bairros'></textarea>
               </div>
              <label><input type='submit' class='btn btn-default form-control'  value='Cadastrar' /> </label>

            </form>

          </td></tr>
                    
          <?php while($lin = mysql_fetch_assoc($sqlc)){ ?>

          <tr id="listItem_<?php echo $lin['id']; ?>">
          
          
          
            <td><?php echo $lin['bairro']; ?></td>
            <td><p class='pull-right'><?php echo $lin['slug']; ?></p></td>
            <td><p class='pull-right'><?php echo $lin['id']; ?></p></td>
            
              <td>
             <a href="editb.php?id=<?php echo $lin['id']; ?>&c=<?php echo $id_cidade; ?>" class="btn btn-default" ><span class="glyphicon glyphicon-pencil"></span></a>
              <a href="delb.php?id=<?php echo $lin['id']; ?>&c=<?php echo $id_cidade; ?>" onclick="return confirma('Deseja deletar esta cidade?');" class="btn btn-default" ><span class="glyphicon glyphicon-trash"></i></a>

          </tr>
            </td>
          <!--   <td>
              <a href="del.php?id=<?php echo $lin['id'];  ?>" onclick="return confirma('Deseja deletar este bairro?');" class="btn btn-danger" ><i class="icon-trash"></i></a>
            </td> -->
          </tr>



          <?php  }  if($num==0){echo "<tr><td  colspan='5'>Não há cadastro de bairros para <strong>".$line['cidade']."</strong>.</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'); ?>locais/bairros.php?c="+current;
          $(window.document.location).attr('href',novaURL);
        };
      });

    }




    });
  </script>

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