<?php include('fncr-geral/configuracao_geral.php');
$pagina = "financeiro";
$blq_pag = "notas_fiscais";
include('fncr-geral/permissoes_restricao.php');
require_once('scripts/financeiro/functions_fingeral.php');

$aliquota_dds = sel_fingeral("nfs_aliquota");

if(isset($_POST['aliquota']) && $_POST['aliquota'] == 'cadastrar') {

  $percent =  moeda($_POST['percent']);
  $expire_mes_ano = $_POST['expire'];

  // Data de expiração
  if($expire_mes_ano!=""){
    list($mes, $ano) = explode('/', $expire_mes_ano);
    $expire = date('Y-m-t', strtotime("$ano-$mes-01")).' 23:59:59';
    $expire_echo = date('t/m/Y', strtotime("$ano-$mes-01")).' - 23:59:59';
  }
  if(!isset($expire)){
    $expire = date('Y-m-t').' 23:59:59';
    $expire_echo = date('t/m/Y').' - 23:59:59';
  }

  // Marca todos as aliquotas existentes como expirado
  $bd->getConexao()->prepare('UPDATE fingeral_nfs_aliquotas SET expirado = 1 WHERE expirado = 0')->execute();

  // Insere novo registro
  $sql = $bd->getConexao()->prepare('INSERT INTO fingeral_nfs_aliquotas
  (aliquota, data_hora_cadastro, data_hora_expire) VALUES
  (:aliquota, NOW(), :data_hora_expire)');
  $sql->bindValue(':aliquota', $percent, PDO::PARAM_STR);
  $sql->bindValue(':data_hora_expire', $expire, PDO::PARAM_STR);
  if($sql->execute());

  $aliquota_dds = sel_fingeral("nfs_aliquota");
}

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

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

    });
  </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">NFs</h3>
</div>

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

<div class="col-lg-2" style="margin-top: 15px">
  <section class="panel">
    <header class="panel-heading" style="padding: 6px">
      <div class="pull-left" style="margin: 5px">Alíquota</div>
      <a data-toggle="modal" href="#modal_nova_aliquota" data-backdrop="static" class="btn btn-primary btn-xs pull-right">Nova Alíquota</a>
    </header>
    <div class="table-responsive">
      <table id="logs" class="table table-striped b-t text-small">
        <thead>
        </thead>
        <tbody>
          <?php if($aliquota_dds){ ?>
            <tr>
              <td width="100">Alíquota</td>
              <td style="font-size: 17px; font-weight: bold; padding: 3px 15px"><?php echo echomoeda($aliquota_dds['aliquota'])?>%</td>
            </tr>
            <tr>
              <td>Validade</td>
              <td><?php echo $aliquota_dds['expire']?></td>
            </tr>
          <?php }else{ ?>
            <tr><td class="text-center text-danger" colspan="2">Nenhuma alíquota válida</td></tr>
          <?php } ?>
        </tbody>
      </table>
    </div>
  </section>
</div>

</section>
</section>

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

<div id="modal_nova_aliquota" class="modal fade">
  <form class="m-b-none" method="post" action="" data-validate="parsley" enctype="multipart/form-data">
    <input type="hidden" name="aliquota" value="cadastrar" />
    <div class="modal-dialog  modal-sm" style="width: 350px;">
      <div class="modal-content">
        <div class="modal-header bg-info">
          <button type="button" class="close" data-dismiss="modal"><i class="fa fa-times"></i></button>
          <h4 class="modal-title" id="myModalLabel">Nova Alíquota</h4>
        </div>
        <div class="modal-body">
          <div class="row">
            <div class="col-lg-12">
              <section class="panel">
                <table class="table table-striped b-t text-small">
                  <thead>
                  </thead>
                  <tbody>
                  <tr>
                    <td width="120"><div style="margin-top: 5px">Alíquota</div></td>
                    <td><input type="text" class="form-control" name="percent"></td>
                  </tr>
                  <tr>
                    <td><div style="margin-top: 5px">Mês/Ano</div></td>
                    <td><input type="text" class="form-control" name="expire" value="<?php echo date("m/Y")?>"></td>
                  </tr>
                  </tbody>
                </table>
              </section>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-sm btn-default" data-dismiss="modal">Cancelar</button>
          <button type="submit" class="btn btn-sm btn-info">Cadastrar</button>
        </div>
      </div>
    </div>
  </form>
</div>

<!-- 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;?>