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

// Configurações
if(isset($_POST['configs']) && $_POST['configs'] == 'adicionar'){

  $sql = $bd->getConexao()->prepare('INSERT INTO planos_configs
  (id_plano_sistema, id_modulo, tipo, controle, site_titulo, site_descricao, site_ordem, site_oculto, site_title) VALUES
  (:id_plano_sistema, :id_modulo, :tipo, :controle, :site_titulo, :site_descricao, :site_ordem, :site_oculto, :site_title)');
  $sql->bindValue(':id_plano_sistema', $_POST['id_plano_sistema'], PDO::PARAM_STR);
  $sql->bindValue(':id_modulo', $_POST['id_modulo'], PDO::PARAM_STR);
  $sql->bindValue(':tipo', $_POST['tipo'], PDO::PARAM_STR);
  $sql->bindValue(':controle', $_POST['controle'], PDO::PARAM_STR);
  $sql->bindValue(':site_titulo', $_POST['site_titulo'], PDO::PARAM_STR);
  $sql->bindValue(':site_descricao', $_POST['site_descricao']!="" ? $_POST['site_descricao'] : null, PDO::PARAM_STR);
  $sql->bindValue(':site_ordem', $_POST['site_ordem'], PDO::PARAM_STR);
  $sql->bindValue(':site_oculto', $_POST['site_oculto'], PDO::PARAM_STR);
  $sql->bindValue(':site_title', $_POST['site_title'], PDO::PARAM_STR);
  if($sql->execute());

  $id_config = $bd->lastId();

  // ADICIONA A CONFIG, PARA OS PLANOS DO SISTEMA
  $sql = $bd->getConexao()->prepare('
  SELECT pn.id
  FROM planos_niveis pn
    LEFT JOIN planos_grupos pg ON pg.id = pn.id_plano_grupo
  WHERE pg.id_plano_sistema = :id_plano_sistema');
  $sql->bindValue(':id_plano_sistema', $_POST['id_plano_sistema'], PDO::PARAM_STR);
  if( $sql->execute() ) { $niveis = $sql->fetchall(PDO::FETCH_ASSOC);}

  $inserts = array();
  foreach($niveis as $l){
    $inserts[] = '("'.$l['id'].'", "'.$id_config.'", 0)';
  }
  if(count($inserts)>0){
    $sql = $bd->getConexao()->prepare('INSERT INTO planos_niveis_configs
      (id_plano_nivel, id_plano_config, valor) VALUES '.implode(',', $inserts));
    if($sql->execute());
  }




  $mensagem = "ok";
}

$sql = $bd->getConexao()->prepare('SELECT * FROM planos_sistemas');
if($sql->execute()){$sistemas = $sql->fetchall(PDO::FETCH_ASSOC);}

$sql = $bd->getConexao()->prepare('SELECT * FROM modulos');
if($sql->execute()){$modulos = $sql->fetchall(PDO::FETCH_ASSOC);}

$sql = $bd->getConexao()->prepare('
SELECT
  pc.*,
  ps.sistema,
  m.nome as modulo
FROM planos_configs pc
  LEFT JOIN planos_sistemas ps ON ps.id = pc.id_plano_sistema
  LEFT JOIN modulos m ON m.id = pc.id_modulo
ORDER BY pc.id_plano_sistema');
if($sql->execute()){$configs = $sql->fetchall(PDO::FETCH_ASSOC);}

?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
  <?php include('fncr-geral/html-head.php');?>
  <style>
    .td_control{padding: 0 !important;}
  </style>
</head>
<body>

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

<section id="content">
<!-- MENSAGENS DE ALERTA -->
<?php include('fncr-geral/mensagens.php');?>
<div class="clearfix padder bg-fncr-clearfix">
<h3 class="m-b">Planos - Nova Configuração</h3>
</div>

<section id="content" class="content-sidebar bg-white">
<section class="main">
<br>

  <div class="col-lg-3">
    <form class="m-b-none" method="post" action="" data-validate="parsley" enctype="multipart/form-data">
      <input type="hidden" name="configs" value="adicionar" />
      <section class="panel">
        <header class="panel-heading">
          <button type="submit" class="btn btn-xs btn-inverse text-center pull-right" style="margin: 0">Criar</button>
          Configurações
        </header>
        <div class="table-responsive">
          <table class="table table-striped b-t text-small">
            <thead>
            <tr>
              <th>Descrição</th>
              <th width="200">Valor</th>
            </tr>
            </thead>
            <tbody>
            <tr>
              <td>Sistema</td>
              <td class="td_control">
                <select class="form-control" name="id_plano_sistema">
                  <?php foreach($sistemas as $l){ ?>
                    <option value="<?php echo $l['id']?>"><?php echo $l['sistema']?></option>
                  <?php } ?>
                </select>
              </td>
            </tr>
            <tr>
              <td>Módulo</td>
              <td class="td_control">
                <select class="form-control" name="id_modulo">
                  <?php foreach($modulos as $l){ ?>
                    <option value="<?php echo $l['id']?>"><?php echo $l['nome']?></option>
                  <?php } ?>
                </select>
              </td>
            </tr>
            <tr>
              <td>Tipo</td>
              <td class="td_control">
                <select class="form-control" name="tipo">
                  <option value="boolean">Boolean</option>
                  <option value="qtd">Qtd</option>
                  <option value="valor">Valor</option>
                </select>
              </td>
            </tr>
            <tr>
              <td>Controle</td>
              <td class="td_control"><input type="text" class="form-control" name="controle"></td>
            </tr>
            <tr>
              <td>Site - Título</td>
              <td class="td_control"><input type="text" class="form-control" name="site_titulo"></td>
            </tr>
            <tr>
              <td>Site - Descrição</td>
              <td class="td_control"><input type="text" class="form-control" name="site_descricao"></td>
            </tr>
            <tr>
              <td>Site - Ordem</td>
              <td class="td_control"><input type="text" class="form-control" name="site_ordem"></td>
            </tr>
            <tr>
              <td>Site - Visibilidade</td>
              <td class="td_control">
                <select class="form-control" name="site_oculto">
                  <option value="1">Oculto</option>
                  <option value="0">Visível</option>
                </select>
              </td>
            </tr>
            <tr>
              <td>Site - Tipo</td>
              <td class="td_control">
                <select class="form-control" name="site_title">
                  <option value="0">Normal</option>
                  <option value="1">Título</option>
                </select>
              </td>
            </tr>
            </tbody>
          </table>
        </div>
      </section>
    </form>
  </div>

  <div class="col-lg-9">
    <section class="panel">
      <header class="panel-heading">
        Todas as Configurações
      </header>
      <div class="table-responsive">
        <table class="table table-striped b-t text-small" style="font-size: 11px">
          <thead>
          <tr>
            <th rowspan="2" width="10">ID</th>
            <th rowspan="2" width="50">Sistema</th>
            <th rowspan="2" width="50">Módulo</th>
            <th rowspan="2" width="70">Tipo</th>
            <th rowspan="2" width="120">Controle</th>
            <th colspan="5" class="text-center">Site</th>
          </tr>
          <tr>
            <th width="150">Título</th>
            <th width="100">Descrição</th>
            <th width="20">Ordem</th>
            <th width="20">Visibilidade</th>
            <th width="50">Tipo</th>
          </tr>
          </thead>
          <tbody>
          <?php foreach($configs as $l){ ?>
            <tr>
              <td><?php echo $l['id']?></td>
              <td><?php echo $l['sistema']?></td>
              <td><?php echo $l['modulo']?></td>
              <td><?php echo $l['tipo']?></td>
              <td><?php echo $l['controle']?></td>
              <td><?php echo $l['site_titulo']?></td>
              <td><?php echo $l['site_descricao']?></td>
              <td><?php echo $l['site_ordem']?></td>
              <td><?php echo $l['site_oculto']==1 ? "Oculto" : "Visível"?></td>
              <td><?php echo $l['site_title']==1 ? "Título" : "Normal"?></td>
            </tr>
          <?php } ?>

          </tbody>
        </table>
      </div>
    </section>
  </div>

</section>
<!-- /.main -->
</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>

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