<div class="card">
    <form action="<?php echo base_url(); ?>manutencao/listarTodosBoletosPorFiltro" method="post"
          name="formAddPreventiva" id="formAddPreventiva"
          class="floating-labels m-t-40">
        <div class="form-body">
            <!--                <input type="hidden" id="peca_id" name="peca_id">-->
            <!--                <input type="hidden" id="servico_id" name="servico_id" value="-->
            <?php //echo $servicoEditar->ID_SERVICO ?><!--">-->
            <div class="row p-t-20 ">


                <div class="form-group col-lg-4" style="margin-left: 2%; ">
                    <input type="text" class="form-control" id="nomeEmitente" name="nomeEmitente"
                    ">
                    <span class="bar"></span>
                    <label for="input1">Emitente </span></label>
                </div>

                <div class="form-group col-lg-2">
                    <select class="form-control p-0" id="situacaoBoleto" name="situacaoBoleto">
                        <option value="A" selected>Em Aberto</option>
                        <option value="Q">Quitado</option>
                        <option value="">Todos</option>
                    </select><span class="bar"></span>
                    <label for="input6">Situação</label>
                    <!--                        <span class="">Quantidade</span>-->
                </div>

                <div class="form-group col-lg-2">
                    <input type="date" class="form-control text-center" id="dataInicial"
                           name="dataInicial"
                           required value="<?php echo date('Y-m-d') ?>">
                    <label for="input6">Inicial <span style="color: red">*</span></label>
                </div>

                <div class="form-group col-lg-2">
                    <input type="date" class="form-control text-center" id="dataFinal"
                           name="dataFinal"
                           required value="<?php echo date('Y-m-d') ?>">
                    <label for="input6">Final <span style="color: red">*</span></label>
                </div>


                <div class="form-group col-lg-1 ">
                    <button type="submit" id="botaoPecaServico" class="btn btn-info"
                    ><i
                                class="fa fa-search"></i> Pesquisar
                    </button>
                </div>


            </div>


        </div>

    </form>
    <div class="card-block">
        <h2 class="card-title">Listagem de Boletos por Filtro</h2>


        <!--        <div align="right">-->
        <!--            <a href="#adicionarNotaModal" class="btn btn-s-md btn-success" data-animation="swell"-->
        <!--               data-plugin="custommodal"-->
        <!--               data-overlaySpeed="100" data-overlayColor="#36404a"><i class="fa fa-user-plus fa-2x"></i> Adicionar</a>-->
        <!--        </div>-->


        <div class="row" style="font-size: small">
            <div class="col-sm-6">
                <label class="form-inline">Show
                    <select id="demo-show-entries" class="form-control input-sm">
                        <option value="5">5</option>
                        <option value="10">10</option>
                        <option value="15">15</option>
                        <option value="50">50</option>
                    </select> entries </label>
            </div>
            <div class="col-sm-6">
                <div class="m-t-40">
                    <div class="d-flex">
                        <div class="ml-auto">
                            <div class="form-group">
                                <input id="demo-input-search1" type="text" placeholder="Procurar" class="form-control"
                                       autocomplete="off">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>


        <table id="demo-foo-pagination" class="table toggle-circle table-hover" data-page-size="50">
            <thead>
            <tr style="font-size: x-small">
                <!--                <th data-toggle="true">#</th>-->
                <th data-toggle="true">#</th>
                <th data-toggle="true">#</th>
                <th data-toggle="true">#</th>
                <th>Cliente</th>
                <th data-hide="phone">Data Emissão</th>
                <th data-hide="phone">Nº NotaFiscal</th>
                <th data-hide="phone">Data Vencimento</th>
                <th data-hide="phone">Valor</th>
                <th data-hide="phone">Status</th>
                <th data-hide="phone">Quitado</th>
                <th data-toggle="true">Ações</th>
            </tr>
            </thead>
            <tbody>
            <?php

            $totaVendas1 = 0;
            $totaVendasQuitadas = 0;
            $totaVendasEmAberto = 0;
            foreach ($notas as $r) {


                if (!is_null($r->DT_QUITACAO)) {
                    $totaVendasQuitadas += $r->VL_BOLETO;
                } else {
                    $totaVendasEmAberto += $r->VL_BOLETO;
                }

                $quandoVencido = '<img src="'.base_url().'assets/sistema/imagens/alerta.gif'.'"><span style="color: red" >Vencido</span>';

//                $vencido = date('Y-m-d') < $r->DT_VENCIMENTO?$quandoVencido:'';
                $vencido = date('Y-m-d') > $r->DT_VENCIMENTO?$quandoVencido:'';

                $totaVendas1 += $r->VL_BOLETO;

                $desabilitado = '';

                if ($r->DT_QUITACAO) {
                    $desabilitado = 'isDisabled';
                }

                if ($r->PAGAMENTO_ID) {
                    $acaoPagamento = '<a data-toggle="tooltip" data-placement="top" title="Ir para Pagamento" href="' . base_url() . 'manutencao/visualizarPagamento/' . $r->PAGAMENTO_ID . ' "> <i class="fa fa-list"></i></a>';
                }


                ?>

                <?php $statusPreventiva = is_null($r->DT_QUITACAO) ? 'red' : 'green'; ?>


                <tr>
                    <td>
                        <?php echo $acaoPagamento; ?>
                    </td>
                    <td>
                        <?php echo $vencido ?>
                    </td>
                    <td><?php echo $r->ID_BOLETO ?></td>
                    <td><?php echo $r->NM_CLIENTE ?></td>
                    <td><?php echo date('d/m/Y', strtotime($r->DT_EMISSAO)) ?></td>
                    <td><?php echo $r->NU_NOTAFISCAL ?></td>
                    <td><?php echo date('d/m/Y', strtotime($r->DT_VENCIMENTO)) ?></td>
                    <td><?php echo number_format($r->VL_BOLETO, 2, ',', '.') ?></td>

                    <td>
                        <span class="label label-table label-<?php echo is_null($r->DT_QUITACAO) ? 'warning' : 'success' ?>"><?php echo is_null($r->DT_QUITACAO) ? 'Pedente' : 'Quitado' ?></span>
                    </td>
                    <td style="color: <?php echo $statusPreventiva ?>"><?php
                        echo is_null($r->DT_QUITACAO) ?'': date('d/m/Y', strtotime($r->DT_QUITACAO))
                        ?>
                    </td>

                    <td>

                        <div class="btn-group" role="group" aria-label="Button group with nested dropdown">
                            <div class="btn-group" role="group">
                                <button id="btnGroupDrop1" type="button" class="btn btn-secondary dropdown-toggle"
                                        data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                    Ações
                                </button>
                                <div class="dropdown-menu" aria-labelledby="btnGroupDrop1">

                                    <a href="#" class="dropdown-item <?php echo is_null($r->DT_QUITACAO) ? '' : 'isDisabled'; ?>"
                                       data-toggle="modal" data-target="#quitarPagamentoModal"

                                       BoletoAQuitarNaLista="<?php echo $r->ID_BOLETO ?>"
                                       BoletoAQuitarNoPag="<?php echo $r->PAGAMENTO_ID ?>"
                                    >
                                        <i class="fa fa-money"></i> Quitar
                                    </a>
<!--                                    <a href="#"-->
<!--                                       class="dropdown-item --><?php //echo is_null($r->DT_QUITACAO) ? '' : 'isDisabled'; ?><!--"-->
<!--                                       servicoItem="--><?php //echo $r->PAGAMENTO_ID ?><!--"-->
<!---->
<!--                                       data-toggle="modal" data-target="#modalExluirItem"-->
<!--                                    > <i class="fa fa-trash-o text-danger"> Excluir</i>-->
<!--                                    </a>-->
                                </div>
                            </div>
                        </div>


                    </td>
                </tr>


            <?php } ?>

            </tbody>
            <tfoot>
            <tr>
                <td colspan="5">
                    <div class="text-right">
                        <ul class="pagination pagination-split m-t-30"></ul>
                    </div>
                </td>
            </tr>
            </tfoot>
        </table>
    </div>

</div>

<?php

echo '<strong style="color: blue">' . 'TOTAL DE BOLETOS QUITADAS- R$: ' . number_format($totaVendasQuitadas, 2, ',', '.') . '</strong>';
echo '<strong style="color:red;">' . '<br>TOTAL DE BOLETOS EM ABERTO- R$: ' . number_format($totaVendasEmAberto, 2, ',', '.') . '</strong>';
echo '<strong style="color:black;">' . '<br>TOTAL DE BOLETOS - R$: ' . number_format($totaVendas1, 2, ',', '.') . '</strong>';
echo '<strong>' . '<br>TOTAL DE BOLETOS: ' . count($notas) . '</strong>';

?>

<style>
    .isDisabled {
        color: currentColor;
        cursor: not-allowed;
        opacity: 0.5;
        text-decoration: none;
        pointer-events: none;
    }
</style>

<script type="text/javascript">
    $(document).on('click', 'a', function (event) {
        if ($(this).attr('BoletoAQuitarNaLista')) {
            var pagamento = $(this).attr('BoletoAQuitarNoPag')
            $('#idItemQuitar').val($(this).attr('BoletoAQuitarNaLista'));
            $("#formQuitarNota").attr("action", "<?php echo base_url() ?>manutencao/QuitarBoleto/"+pagamento);
        }
    });
</script>

<?php //$this->load->view("sistema/modais/notas/editarNotaModal"); ?>
<?php //$this->load->view("sistema/modais/notas/adicionarNotaModal"); ?>
<?php $this->load->view("sistema/modais/notas/quitarPagamentoModal"); ?>
<?php //$this->load->view("sistema/modais/exluirItemGerericosModal"); ?>


<script>
    $('a').on('click', function (e) {
        if ($(this).attr('imprimirNota')) {
            var preventiva = $(this).attr('imprimirNota')
            abreImpressaoPreventiva(preventiva);
            return false;
        }
    });


    function abreImpressaoPreventiva(preventiva) {
        var url = "<?php echo base_url() ?>relatorios/visualizarNotaPorID/" + preventiva;
        var win = window.open(url, target = "_blank", "left=400,top=300,width=700,height=500,location=no,menubar=no,toolbar=no");
        win.print();
    }
    ;
</script>

<script type="text/javascript">
    $(document).on('click', 'a', function (event) {
        if ($(this).attr('servicoItem')) {
            $('#idItemExcluir').val($(this).attr('servicoItem'));
            $("#excluirItem").attr("action", "<?php echo base_url() ?>manutencao/excluirNota");
        }
    });
</script>

