<?php include('fncr-geral/configuracao_geral.php');
$pagina = "suporte";
$blq_pag = "suporte";
include('fncr-geral/permissoes_restricao.php');

// Contas e solicitações
$sql = $bd->getConexao()->prepare('
SELECT
  regs.*,
  c.nome as clube
FROM (
  (
    SELECT
      "conta" as tipo,
      bc.nome,
      bc.email,
      bc.cidade,
      bc.id_clube,
      sub.aprovado,
      CASE
        WHEN COUNT(doc.id) = 0 THEN "[]"
        ELSE JSON_ARRAYAGG(
          JSON_OBJECT(
            "ext_doc_title", doc.ext_doc_title,
            "ext_doc_description", doc.ext_doc_description,
            "ext_doc_status", doc.ext_doc_status,
            "ext_doc_type", doc.ext_doc_type,
            "ext_url", doc.ext_url,
            "ext_reprovado_motivo", doc.ext_reprovado_motivo
          )
        )
        END AS documentos
    FROM pwbank_subcontas sub
      LEFT JOIN pwbank_contas bc ON bc.id = sub.id_conta
      LEFT JOIN pwbank_subcontas_documentos doc ON doc.id_subconta = sub.id AND doc.ext_doc_type != "IDENTIFICATION" AND doc.id IS NOT NULL
    WHERE bc.interno = 0
    GROUP BY sub.id
  )
  
  UNION ALL
  
  SELECT
    "solicitacao" as tipo,
    bs.nome,
    bs.email,
    bs.cidade,
    bs.id_clube,
    null,null
  FROM pwbank_contas_solicitacoes bs
    LEFT JOIN pwbank_contas bc ON bc.id_clube = bs.id_clube
  WHERE bc.id IS NULL
) as regs
  LEFT JOIN clubes c ON c.id = regs.id_clube
ORDER BY regs.tipo DESC, regs.aprovado, c.nome');
if($sql->execute()){$contas = $sql->fetchall(PDO::FETCH_ASSOC);}

?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <?php include('fncr-geral/html-head.php');?>

  <link rel="stylesheet" type="text/css" href="js/datatables2/media/css/jquery.dataTables.css">
  <script type="text/javascript" language="javascript" src="js/datatables2/media/js/jquery.dataTables.js"></script>
  <link rel="stylesheet" href="js/checkbox/jquery.checkbox.css" />

  <style>
    .account_status{font-weight: bold}
    .account_status.status_PENDING{color: #ab5600}
    .account_status.status_AWAITING_APPROVAL{color: #9335c5}
    .account_status.status_APPROVED{color: #008345}
    .account_status.status_REJECTED{color: #ff0000}

    .doc_status{font-weight: bold}
    .doc_status.status_NOT_SENT{color: #ab5600}
    .doc_status.status_AWAITING_APPROVAL, .doc_status.status_PENDING{color: #9335c5}
    .doc_status.status_APPROVED{color: #008345}
    .doc_status.status_REJECTED{color: #ff0000}
  </style>

  <script type="text/javascript" language="javascript" class="init">

    $(document).ready(function() {

      // Formatação de Tabelas
      $('table.subcontas').DataTable({
        stateSave: false, // Salvar última pesquisa
        "dom": '<"top"fl>rt<"bottom"ip><"clear">', // Layout
        "pagingType": "full_numbers", // Modo Paginação
        "iDisplayLength": 50,
        "order": [],
        "language": { // Tradução
          "lengthMenu": "Resultados por P&aacute;gina: _MENU_ ",
          "zeroRecords": "N&atilde;o existe Registros para esta pesquisa",
          "info": "P&aacute;gina _PAGE_ de _PAGES_",
          "infoEmpty": "N&atilde;o existe Registros para esta pesquisa",
          "infoFiltered": "(Filtrando de um total de _MAX_ Registros)",
          "search": "Localizar Registro: ",
          "paginate": {
            "first": "Primeira",
            "previous": "Anterior",
            "next": "Pr&oacute;xima",
            "last": "&Uacute;ltima"
          }
        }
      });

    });

  </script>
</head>
<body>

<!-- BARRA SUPERIOR -->
<?php include('barra-sup.php');?>
<!-- MENU PRINCIPAL -->
<?php include('menu.php');?>

<section id="content">

<div class="clearfix padder bg-fncr-clearfix">
<h3 class="m-b">GameID - Solicitações</h3>
</div>

<section id="content" class="content-sidebar bg-white" style="padding-bottom: 100px;line-height: ">
<section class="main">
<br>

  <div class="col-lg-12">
    <section class="panel">
      <header class="panel-heading">
        <div style="margin-top: 5px">GameID - Solicitações</div>
      </header>
      <div class="table-responsive">
        <table class="table table-striped b-t text-small subcontas">
          <thead>
          <tr>
            <th width="200">Clube</th>
            <th width="100">Status</th>
            <th width="300">Pendências</th>
            <th width="200">Url - Selfie</th>
            <th>Reprovação</th>
            <th width="300">Dados</th>
          </tr>
          </thead>
          <tbody>
          <?php foreach($contas as $l){
            $solicitacao = $l['tipo']=="solicitacao";

            $docs_pendentes = $ext_reprovado_motivo = $ext_url = false;
            $docs = json_decode($l['documentos'], true);
            foreach((array)$docs as $d){
              if(in_array($d['ext_doc_status'],["NOT_SENT","REJECTED"])){$docs_pendentes = true;}
              if(!$ext_reprovado_motivo && in_array($d['ext_doc_status'],["REJECTED"]) && $d['ext_reprovado_motivo']!=""){ $ext_reprovado_motivo = $d['ext_reprovado_motivo'];}
              if(!$ext_url && in_array($d['ext_doc_status'],["NOT_SENT","REJECTED"]) && $d['ext_url']!=""){ $ext_url = $d['ext_url'];}
            }
            ?>
            <tr>
              <td><?php echo $l['clube']?></td>
              <td style="font-weight: bold">
                <?php
                  if($solicitacao){ echo '<div style="color: #0F769F">SOLICITADO</div>'; }
                  else{
                    if($l['aprovado']==1){ echo '<div style="color: #008345">APROVADO</div>'; }
                    else{
                      // Lista status de docs se pendente
                      if($docs_pendentes){
                        echo '<div style="color: #ab5600">PENDÊNCIAS</div>';
                      }else{
                        echo '<div style="color: #9335c5">EM ANÁLISE</div>';
                      }
                    }
                  }
                ?>
              </td>
              <td>
                <?php if($l['aprovado']!=1){
                  $doc_tatus=[
                    'NOT_SENT'=>'NÃO ENVIADO',
                    'PENDING'=>'EM ANÁLISE',
                    'APPROVED'=>'APROVADO',
                    'REJECTED'=>'REPROVADO'
                  ];
                  foreach((array)$docs as $d){
                    $doc_status_final = isset($doc_tatus[$d['ext_doc_status']]) ? $doc_tatus[$d['ext_doc_status']] : $d['ext_doc_status'];
                    ?>
                    <div class="doc_pendente">
                      <?php echo $d['ext_doc_title']?>: <span class="doc_status status_<?php echo $d['ext_doc_status']?>"><?php echo $doc_status_final?></span>
                    </div>
                <?php }} ?>
              </td>
              <td><?php if($ext_url){ echo $ext_url; }?></td>
              <td><?php if($ext_reprovado_motivo){ echo $ext_reprovado_motivo; }?></td>
              <td>
                <?php if($l['aprovado']!=1){ echo $l['nome'].'<br>'.$l['cidade'].'<br>'.$l['email']; } ?>
              </td>
            </tr>
          <?php } ?>
          </tbody>
        </table>
      </div>
    </section>
  </div>

</section>
</section>

<!-- FOOTER -->
<?php include('footer.php');?>

<!-- app -->
<script src="js/app.js"></script>
<script src="js/app.plugin.js"></script>
<script src="js/app.data.js"></script>
<!-- Sparkline Chart -->
<script src="js/charts/sparkline/jquery.sparkline.min.js"></script>
<!-- Easy Pie Chart -->
<script src="js/charts/easypiechart/jquery.easy-pie-chart.js"></script>
  <!-- select2 -->
<script src="js/select2/select2.min.js"></script>
  <!-- fuelux -->
<script src="js/fuelux/fuelux.js"></script>
<!-- datepicker -->
<script src="js/datepicker/bootstrap-datepicker.js"></script>
<!-- slider -->
<script src="js/slider/bootstrap-slider.js"></script>
<!-- file input -->
<script src="js/file-input/bootstrap.file-input.js"></script>
<!-- combodate -->
<script src="js/combodate/moment.min.js"></script>
<script src="js/combodate/combodate.js"></script>
<!-- parsley -->
<script src="js/parsley/parsley.min.js"></script>
<!-- checkbox -->
<script src="js/checkbox/jquery.checkbox.min.js"></script>

</body>
</html>
<?php $bd = null; $bd_fn = null;?>