<?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");
$sqlr = mysql_query("SELECT * FROM ramos ORDER BY ramo asc");
$sqlt = mysql_query("SELECT * FROM tags ORDER BY tag 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 class="active">Cadastro Rápido</li>
                </ul>


<div class="row">
   <div class="col-md-8">
<form name='cadastro' id='cadastro'>
        <table class="table table-bordered">

        <thead>

          <tr>
            <!-- <th>MOVER</th> -->

            <th colspan='4'>Selecione os critérios</th>  

            <th><a class='btn btn-warning form-control' href=''>Resetar</a></th>
          </tr>
        </thead>
        <tbody>
       
          <tr>
            <td  colspan='5'>
            <label></label>
          
               <div class="form-group">
                <div style='display:none' class="alert alert-success alert-dismissible fade in" id='msg' role="alert">
      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
      <strong>Cadastro realizado</strong> 
    </div>

                <div style='display:none' class="alert alert-warning alert-dismissible fade in" id='msg2' role="alert">
      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
      <strong>Preencha os campos corretamente</strong> 
    </div>

               <select id='ramo'  name='ramo' class="form-control">


                <?php while($lin = mysql_fetch_assoc($sqlr)){ ?>                
                  <option value='<?php  echo $lin['id'];  ?>'  ><?php  echo $lin['ramo'];  ?></option>
                <?php } ?>   

              </select>  <br />


              <select id='estados' name='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>  <br />

                <div id='retc'>


                <select id='cidades' name='cidades' class="form-control">
              <option> - Cidade - </option>


              </select>  
            </div><br />
              <div id='retb'>
                <select id='bairros' name='bairros' class="form-control">
              <option> - Bairro - </option>


              </select>  
            </div><br />

 

               <input type='hidden'  id='uf' name='uf'>
               <input type='hidden'  id='cidade' name='cidade'>
               <input type='hidden'  id='bairro' name='bairro'>
               <input type='hidden'  id='ramo2' name='ramo2'>
          <input type='text' class='form-control' name='empresa' placeholder='Empresa'>
          <br>
          <br>
            <input type='text' class='form-control' name='endereco' placeholder='Endereço'>
              <br />
              <br />          

              <input type='text' class='form-control' name='telefone' placeholder='Telefone'>
              <span id='avisotel' style='display:none' ></span>


               </div>
              <input type='button' class='btn btn-warning form-control' id='cadastrar'  value='Cadastrar' />


          </td>

        </tr>
           
              <!--tr>
                 <td colspan='5'><strong>Último Cadastro</strong></td>  
              </tr>


                  <tr>


            <td colspan='4'>Fast Pet Shop dos Gatos Felizes - Recife</td>  
            <td colspan='1'>

             <a href="" class="btn btn-default" ><span class="glyphicon glyphicon-pencil"></span></a>
              <a href="" onclick="return confirma('Deseja deletar esta adastro?');" class="btn btn-default" ><span class="glyphicon glyphicon-trash"></i></a>



            </td>  

  
          </tr-->


        </tbody>
      </table>
</form>

</div><!-- col1 -->
   <div class="col-md-4">
        <table class="table table-bordered">

        <thead>

          <tr>
            <!-- <th>MOVER</th> -->

            <th colspan='1'>Tags</th>  

          </tr>
        </thead>
        <tbody>
          <tr>
            <td>

                <?php while($lin = mysql_fetch_assoc($sqlt)){ ?>                
                   <label><input type='checkbox' class='tagin' value='<?php  echo $lin['tag'];  ?>' /> <?php  echo $lin['tag'];  ?></label><br/>
                <?php } ?> 
           


     
              <textarea class='form-control' id='nome' rows='10'></textarea>

            </td>
          </tr>

          <tr><td>
                <a class='btn btn-warning form-control' id='nameformat' href='#'>Formar nome</a>
            </td>
          </tr>
   </table>
</div>
</div><!-- row -->


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


 $("input[name=telefone]").keyup(function(){
    var tel = $("input[name=telefone]").val();
      if(tel.length > 7){

    $.ajax({   
      url: "checktel.php",
      data: {'tel':tel},
      type: 'POST',
      beforeSend: function() {    


      },
    success: function(data)
    {
 
      if(data=='2'){

           $('#avisotel').html('<a target="_blank" href="similares.php?tel='+tel+'">Telefone similar encontrado</a>');
            $('#avisotel').slideDown('slow');

     }else{
      $('#avisotel').slideUp('fast');
     }


    }

  });

      }

      });




 $("#ramo2").val($("#ramo").val());

 $(".tagin").change(function(){

    var tag  = $(this).val();
    var nome = $('#nome').val();

  if($(this).prop('checked')==true){ 

    $("#nome").val(nome+tag);

  }else{

    $("#nome").val(nome.replace(tag, ""));
   
  }

 });


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

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


      },
    success: function(data)
    {
 
        $('#retc').html(data);  
        $("#uf").val(uf);
        bairro();
    }
  });

      });
    

  
function bairro( ) {
      $("#cidades").change(function(){

     var c = $("#cidades").val();


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

      },
    success: function(data)
    {
 
        $('#retb').html(data);  
        $("#estados").attr('disabled', true);
            $("#cidades").attr('disabled', true);
        $("#cidade").val(c);
        travab()

       
    }
    });

      });

    }


    function travab(){ 
   $("#bairros").change(function(){

    var bairro = $("#bairros").val();

    
    $("#bairro").val(bairro);

   });
 }


 $("#ramo").change(function(){

        var ramo = $(this).val();
        //$("#bairros").attr('disabled', true);
        $("#ramo2").val(ramo);
   });

 

 $("#ramo").blur(function(){
  var ramo = $(this).val();
     $("#ramo2").val(ramo);
  $("#ramo").attr('disabled', true);

 });


  
 $("#cadastrar").click(function(){
         var str = $( "#cadastro" ).serialize();

    $.ajax({   
      url: "cadastrar.php",
      data: str,
      type: 'POST',
      beforeSend: function() {    

        $('#avisotel').slideUp('fast');

      },
    success: function(data)
    {
 
        //alert(data);
        if(data=='1'){
          $('input[name=empresa]').val("");
          $('input[name=telefone]').val("");
          $('input[name=endereco]').val("");

          $('#msg').slideDown('slow',function(){


            $('#msg').delay('2000').slideUp('fast');

          });
        }else{

//alert(data);
          $('#msg2').slideDown('slow',function(){

            $('#msg2').delay('2000').slideUp('fast');

          });

        }
    }

  });

      });

   $("#nameformat").click(function(){
         var nome = $( "#nome" ).val();
         var cidade = $( "#cidade" ).val();
         var bairro = $( "#bairro" ).val();
         var uf = $( "#uf" ).val();
         var empresa = $( "input[name=empresa]" ).val();

    $.ajax({   
      url: "nameformat.php",
      data: {'nome':nome,'cidade':cidade,'bairro':bairro,"uf":uf,'empresa':empresa},
      type: 'POST',
      beforeSend: function() {    


      },
    success: function(data)
    {

        $( "input[name=empresa]" ).val(data);

    }

  });

      });


    });
  </script>

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