<?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"; 

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




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

  $id_uf = $_GET['uf'];
  $sqlc = mysql_query("SELECT * FROM l_cidades WHERE id_uf = '$id_uf' ORDER BY cidade asc");
  $sqluf = mysql_query("SELECT * FROM l_estados WHERE id='$id_uf'");
  $numc = mysql_num_rows($sqlc);
  $line = mysql_fetch_assoc($sqluf);

}else{

  $sqlc = mysql_query("SELECT * FROM l_cidades WHERE id_uf = '33333333443' ORDER BY cidade asc");
  $sqluf = mysql_query("SELECT * FROM l_estados WHERE id='444444444444444444'");
  $line = mysql_fetch_assoc($sqluf); 

  $id_uf = '44444444444444444444444'; 

}

$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['uf'])){ echo $line['estado'].' ('.$numc.')';} else{ echo 'Escolha uma UF';}  ?></li>
        </ul>


        <table class="table table-bordered">

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

              <!--a href="add.php" class="btn btn-success" >Adicionar nova cidade <i class="icon-plus icon-white"></i></a-->



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

                <?php while($lin = mysql_fetch_assoc($sql)){ ?>                
                  <option value='<?php  echo $lin['id'];  ?>'  <?php if($lin['id']==$id_uf){ 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>Ativar</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='adicionac.php' method='post'>
               <div class="form-group">
              <input type='hidden' name='id_uf' value='<?php echo $id_uf; ?>'  />
              <textarea class='form-control' name='cidades'></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> <div class="box_img handle" style="cursor:move;display:block">
            <div class="mover"><i class='icon-move'></i></div></td> -->
            <td><?php echo $lin['cidade']; ?></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="editc.php?uf=<?php echo $id_uf.'&id='.$lin['id']; ?>" class="btn btn-default" ><span class="glyphicon glyphicon-pencil"></span></a>
              <a href="delc.php?uf=<?php echo $id_uf.'&id='.$lin['id']; ?>" onclick="return confirma('Deseja deletar esta cidade?');" class="btn btn-default" ><span class="glyphicon glyphicon-trash"></i></a>
              <a href="../bannercidades/index.php?uf=<?php echo $id_uf.'&id='.$lin['id']; ?>"  class="btn btn-default" ><span class="glyphicon glyphicon-picture"></i></a>


            </td>
            <td>
              <a href="ativar.php?id=<?php echo $lin['id'];  ?>&uf=<?php echo $id_uf;  ?>&ativo=<?php echo $lin['ativo']; ?>"  <?php if($lin['ativo']!=1){  ?> class="btn btn-danger" <?php }else{  ?> class="btn btn-success" <?php }  ?> ><i class="glyphicon glyphicon-ok"></i></a>
            </td>
          </tr>



          <?php  }  if($num==0){echo "<tr><td  colspan='5'>Não há cadastro de cidades para <strong>".$line['estado']."</strong>.</td></tr>";} ?>


        </tbody>
      </table>

<script type="text/javascript">
    $(document).ready(function(){
      //link atual momentaneo
      //$('header#topo nav ul li a.sobre').closest('li').addClass('atual');
      $("#estado").change(function(){
        var current = $("#estado").val();
        if (current != "select") {
          var novaURL = "<?php echo sti('adm'); ?>locais/cidades.php?uf="+current;
          $(window.document.location).attr('href',novaURL);
        };
      });
    });
  </script>

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