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

function testaid($id,$tabela){

  global $con;
  $sql = mysql_query("SELECT id FROM ".$tabela." WHERE id = '$id'");

  if(mysql_num_rows($sql)==0){
    return TRUE;
  }else{

    return FALSE;
  }


}


$sqlA = mysql_query("SELECT id,id_cidade,id_uf,id_bairro FROM empresas ORDER BY id DESC");

  $geral = array();
while($linha = mysql_fetch_assoc($sqlA)){

  $teste = FALSE;
  $erro = array();


  if(testaid($linha['id_uf'],'l_estados')){

    $erro[] = 'UF';
    $teste = TRUE;

  }

  if(testaid($linha['id_cidade'],'l_cidades')){

    $erro[] = 'Cidade';
    $teste = TRUE;

  }

  if(testaid($linha['id_bairro'],'l_bairros')){

    $erro[] = 'Bairro';
    $teste = TRUE;

  }

  if($teste){

      $geral[$linha['id']] = $erro;

  }

}



?>
<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 count($geral);    ?> Empresas com erro</li>
        </ul>


        <table class="table table-bordered">

        <thead>

          <tr>
   

             <th>ID</th>
            <th>Nome</th>

            
             

             <th>Telefone</th>
             <th>slug</th>
             <th>Endereço</th>
             <th>Cidade / UF</th>
             <th>Bairro</th>
             <th>Erros</th>
             <th></th>
          </tr>
        </thead>
        <tbody id='test-list'>

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

          foreach ($geral as $key => $value) { ?>
            
          <tr id="listItem_<?php echo $lin['id']; ?>">

            <?php

            $lin    = retorna($key, 'empresas');
            $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 $lin['empresa'];  ?></td>
            <td><?php echo $lin['telefone'];  ?></td>
            <td><?php echo $lin['slug'];  ?></td>
            <td><?php echo $lin['endereco'];  ?></td>
            <td><?php echo $cidade['cidade'];  ?> - <?php echo strtoupper($estado['uf']);  ?></td>
            <td><?php echo $bairro['bairro'];  ?></td>
            <td><?php echo implode(', ', $value);  ?></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'];  ?>" onclick="return confirma('Deseja deletar este cliente?');" class="btn btn-default" ><span class="glyphicon glyphicon-trash"></span></a>
            </td>
          </tr>
          <?php  } if(count($geral)==0){echo "<tr><td  colspan='8'>Não há cadastro de empresas</td></tr>";} ?>


        </tbody>
      </table>

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