<?php
session_start();

include "../phpconfig/config.php";
include "../phpconfig/valida_login.php";
include "../includes/topo.php"; 

$id = $_GET['id'];

$sql = mysql_query("SELECT * FROM empresas WHERE id='$id'");
$sql2 = mysql_query("SELECT * FROM empresamais WHERE id_emp='$id'");
$lin = mysql_fetch_assoc($sql);
$lin2 = mysql_fetch_assoc($sql2);




$id_uf = $lin['id_uf'];
$id_cidade = $lin['id_cidade'];
$id_bairro = $lin['id_bairro'];
$slug = $lin['slug'];




$sqle = mysql_query("SELECT * FROM l_estados ORDER BY estado asc");
$sqlc2 = mysql_query("SELECT * FROM l_cidades WHERE id_uf='$id_uf' ORDER BY cidade asc");
$sqlb2 = mysql_query("SELECT * FROM l_bairros WHERE id_cidade='$id_cidade' ORDER BY bairro asc");

?>

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <style>    
    #mapCanvas{
        width:100%;
        height:300px;

    }
    #mapCanvas img {
        border: 0 none;
        height: auto;
        max-width: none;

     }
      body {
        padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
      }

      body {
        padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
      }
    </style>

        <ul class="breadcrumb">
          <li><a href="<?php echo sti('adm');  ?>">Home</a></li>
          <li class="active">Editar Serviços</li>
        </ul>

<form method="post" action="edita.php"    enctype="multipart/form-data">
        <fieldset>

       

          <div class="form-group">

              <label class="control-label" >Nome</label>
         
              <input type="hidden"name='id' value='<?php echo $lin['id'];  ?>' >
              <input type="text" class="form-control" name='empresa' value='<?php echo $lin['empresa'];  ?>' >
        
          </div>

          <div class="form-group">
            <label class="control-label" >Slug</label>
         
              <input type="text" class="form-control" name='slug' value='<?php echo $lin['slug'];  ?>' >
            
          </div>

              <select id='estados' name='estados' class="form-control">
             <option>- Estado -</option>

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

              </select>  <br />

                <div id='retc'>


                <select id='cidades' name='cidades' class="form-control">
              <option> - Cidade - </option>
                <?php while($line = mysql_fetch_assoc($sqlc2)){ ?>                
                  <option value='<?php  echo $line['id'];  ?>' <?php if(isset($id_cidade)){ if($id_cidade==$line['id']) {echo 'selected';}    } ?> ><?php  echo $line['cidade'];  ?></option>
                <?php } ?>   

              </select>  
            </div><br />
              <div id='retb'>
                <select id='bairros' name='bairros' class="form-control">
              <option> - Bairro - </option>
                <?php while($line = mysql_fetch_assoc($sqlb2)){ ?>                
                  <option value='<?php  echo $line['id'];  ?>' <?php if(isset($id_bairro)){ if($id_bairro==$line['id']) {echo 'selected';}    } ?> ><?php  echo $line['bairro'];  ?></option>
                <?php } ?>  

              </select>  
            </div><br />     
           
               <input type='hidden'  id='id' name='id' value='<?php echo  $lin['id']; ?>'>
               <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='telefone' value='<?php echo $lin['telefone']; ?>' placeholder='Telefone1'>
              <span id='avisotel' style='display:none' ></span>

              <input type='text' class='form-control' name='telefone2' value='<?php echo $lin['telefone2']; ?>' placeholder='Telefone2'>
              <span id='avisotel' style='display:none' ></span>

              <input type='text' class='form-control' name='telefone3' value='<?php echo $lin['telefone3']; ?>' placeholder='Telefone3'>
              <span id='avisotel' style='display:none' ></span>
     
          <hr>

          <div class="form-group">
            <label class="control-label" >Lista (escreva os itens separados por //)</label>
         
              <input type="text" class="form-control" name='lista' value='<?php echo $lin2['lista'];  ?>' >
            
          </div>    

          <div class="form-group">
            <label class="control-label" >Site</label>
         
              <input type="text" class="form-control" name='site' value='<?php echo $lin2['site'];  ?>' >
            
          </div>   

          <div class="form-group">
            <label class="control-label" >Email</label>
         
              <input type="text" class="form-control" name='email' value='<?php echo $lin2['email'];  ?>' >
            
          </div>   
          <div class="form-group">
            <label class="control-label" >Facebook</label>
         
              <input type="text" class="form-control" name='face' value='<?php echo $lin2['face'];  ?>' >

            
          </div>   


<div class="form-group">
            <label class="control-label" >Instagram</label>
         
              <input type="text" class="form-control" name='instagram' value='<?php echo $lin2['instagram'];  ?>' >

            
          </div>

          <div class="form-group">
            <label class="control-label" >Whastapp</label>
         
              <input type="text" class="form-control" name='zap' value='<?php echo $lin2['zap'];  ?>' >
            
          </div>   


          <hr>
          
          <h2>SEO</h2>

          <div class="form-group">
            <label class="control-label" >Título</label>
         
              <input type="text" class="form-control" name='titulo' id='titulo' value='<?php echo $lin2['titulo'];  ?>' >
              <div ><span id='titulov'>0</span> de 70</div>
            
          </div>   
          <div class="form-group">
            <label class="control-label" >Decrição</label>
         
              <input type="text" class="form-control" name='descri' id='descri' value='<?php echo $lin2['descri'];  ?>' >
              <div ><span id='descriv'>0</span> de 160</div>
            
          </div>   

          <div class="form-group">
            <label class="control-label" >Keywords</label>
         
              <input type="text" class="form-control" name='keywords' id='keywords' value='<?php echo $lin2['keywords'];  ?>' >
            
          </div>   


         <div class="form-group">
            <label class="control-label" >Formas de pagamento</label>
          
          <?php 

            $pagto = array();

            if($lin2['pag']!=''){ $pagto = unserialize($lin2['pag']); }

            //print_r($pagto);



          foreach($pag as $key => $value){ 

           ?>

             <div class="checkbox"><label><input type="checkbox"  name='pag[]' <?php  if(in_array($key, $pagto)){echo 'checked';} ?> value='<?php echo $key; ?>' ><?php echo $value; ?></label></div>
          <?php }  ?>  
          </div>   

          <div class="form-group">
            <label class="control-label" for="textarea">Conteúdo</label>
            
              <textarea class="form-control"  id="myArea2" name="sobre" rows="10"><?php echo $lin2['sobre'];  ?></textarea>
           
          </div>

          <?php if (strlen($lin2['logo'])>5): ?>
                      
          <div class="form-group">
            <label class="control-label" for="input01">Logo</label>
          
              <img width='300' src='<?php echo sti('down').'logos/'.$lin2['logo'];  ?>'  />
               <a class="btn btn-large" title="Deletar imagem" href="delImagem.php?p=<?php echo $id; ?>"><span class="glyphicon glyphicon-remove"></span></a>

      
          </div>
           <?php endif ?>  


         
        <div class="form-group">
            <label class="control-label" for="imagem">Logo</label>
           
              <input id="logo" name='logo' accept="image/*" type="file">
           
          </div>


          <div class="form-group">
            <label class="control-label" for="textarea">Mapa - IFrame</label>
            
              <textarea class="form-control"   name="mapa" rows="10"><?php echo $lin2['mapa'];  ?></textarea>
           
          </div>





          <div class="form-actions">
            <button type="submit" class="btn btn-primary">Salvar</button>
            <a class="btn btn-warning pull-right" href="../empresas/listar.php?c=<?php echo $id_cidade;  ?>">Voltar</a>
          </div>
        </fieldset>
      </form>
      <script src="http://js.nicedit.com/nicEdit-latest.js" type="text/javascript"></script>
    <script type="text/javascript">
      //<![CDATA[
      var area1;

      function toggleArea1() {
      
            area1 = new nicEditor({fullPanel : true}).panelInstance('myArea2',{hasPanel : true});

      }


      bkLib.onDomLoaded(function() { 
      
          new nicEditor({buttonList : ['fontFormat','bold','italic','underline','strikeThrough','subscript','superscript','html','image','link','ul']}).panelInstance('myArea2');
      
      toggleArea1(); });

   //]]>


     function conta(conta, alvo){

      $("#"+conta).keyup(function() {
        var tamanho = $(this).val().length;
        $("#"+alvo).html(tamanho);
        });
     }


conta('titulo', 'titulov');
conta('descri', 'descriv');




 $("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');
     }


    }

  });

      }

      });






      $("#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);

   });
 }

   $("#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();
         var id = $("#id").val();
    $.ajax({   
      url: "edita.php",
      data: str,
      type: 'POST',
      beforeSend: function() {    

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

      },
    success: function(data)
    {
 
        //alert(data);
        if(data=='1'){


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


            $('#msg').delay('2000').slideUp('fast');
             window.location = "<?php echo sti('urlbase'); ?>ncms/empresas/listar.php?c=<?php echo $id_cidade; ?>";

          });
        }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);

    }

  });

      });


   var ufEmp = "<?php echo $lin['id_uf'];  ?>";
    var ramoEmp = "<?php echo $lin['ramo'];  ?>";

    var cidadeEmp = "<?php echo $lin['id_cidade'];  ?>";
    var bairroEmp = "<?php echo $lin['id_bairro'];  ?>";

       $('#estados').val(ufEmp);
    $('#cidade').val(cidadeEmp);
    $('#bairro').val(bairroEmp);


 $('#uf').val(ufEmp);
    $('#ramo').val(ramoEmp);



      </script>

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

