<div class="card">
    <form action="<?php echo base_url(); ?>manutencao/listarTodosPedidosPorFiltro" 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-2" style="margin-left: 2%; ">
                    <input type="text" class="form-control" id="nomeCliente" name="nomeCliente"
                    ">
                    <span class="bar"></span>
                    <label for="input1">Cliente </span></label>
                </div>

                <div class="form-group col-lg-2">
                    <select class="form-control p-0" id="situacaoNota" name="situacaoNota" >
                        <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">
                    <select class="form-control p-0" id="nomeVendedor" name="nomeVendedor" >
                        <option value="" selected>Todos</option>
                        <option value="ESMAR" >ESMAR</option>
                        <option value="LUCIANA">LUCIANA</option>
                        <option value="PEDRO">PEDRO</option>
                        <option value="MARIANO">MARIANO</option>
                    </select><span class="bar"></span>
                    <label for="input6">Vendedor</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>
    <form action="<?php echo base_url(); ?>manutencao/pagamentosNotasSelecionados" method="post">


        <div class="btn-group float-right" role="group" aria-label="Basic mixed styles example">
            <a href="#adicionarNotaModal" class="btn btn-s-md btn-danger" data-animation="swell"
               data-plugin="custommodal"
               data-overlaySpeed="100" data-overlayColor="#36404a"><i class="fa fa-user-plus"></i> Pedido</a>
               
            
            
            
            <div class="btn-group" role="group">
                <button id="btnGroupDrop1" type="button" class="btn btn-s-md dropdown-toggle btn btn-success" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <i class="fa fa-money"></i>
                    Quitar
                </button>
                <div class="dropdown-menu" aria-labelledby="btnGroupDrop1">
                    <button type="submit" name="parcelado" data-toggle="tooltip" data-placement="top" title="Parcelar Pagemento de 1 ou Mais Pedidos" class="dropdown-item"> Parcelado</button>
                    <button type="submit" name="avista"  data-toggle="tooltip" data-placement="top" title="Quitar Pagementos de vários Pedidos" class="dropdown-item"> À Vista </button>
                </div>
            </div>
            
            
            
            

            <div class="btn-group" role="group">
                <button id="btnGroupDrop1" type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Pagos Com
                </button>
                <div class="dropdown-menu" aria-labelledby="btnGroupDrop1">
                    <a class="dropdown-item" href="<?php echo base_url().'manutencao/listarTodosChequesPorFiltro/' ?>">Cheques</a>
                    <a class="dropdown-item" href="<?php echo base_url().'manutencao/listarTodosBoletosPorFiltro/' ?>">Boletos</a>
                </div>
            </div>



            <!--                    <button type="button" class="btn btn-success">Right</button>-->
        </div>

        <h2 class="card-title">Listagem de Notas 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">N.&ordm; Pedido</th>
                <th>Cliente</th>
                <th data-hide="phone">Vendedor</th>
                <th data-hide="phone">Data</th>
                <th data-hide="phone">Valor</th>
                <th data-hide="phone">Produto</th>
                <th data-hide="phone">Status</th>
                <th data-hide="phone">Quitado</th>
                <th data-hide="phone">Previsão</th>
                <th data-toggle="true">Ações</th>
<!--                <th data-hide="all"></th>-->
            </tr>
            </thead>
            <tbody>
            <?php

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


                if(!is_null($r->DT_QUITADO)){
                    $totaVendasQuitadas += $r->VL_PEDIDO;
                }ELSE{
                    $totaVendasEmAberto+= $r->VL_PEDIDO;
                }


                $totaVendas1 += $r->VL_PEDIDO;

                $desabilitado = '';

                if($r->DT_QUITADO || $r->PAGAMENTO_ID){
                    $desabilitado = 'isDisabled';
                }

                if($r->PAGAMENTO_ID){
                    $acaoPagamento = '<a href="'.base_url().'manutencao/visualizarPagamento/'.$r->PAGAMENTO_ID.'"> <i class="fa fa-list"></i></a>';
                }else{
                    $acaoPagamento = '<input type="checkbox" class="'.$desabilitado.'" name="pedidos_id[]" value="'. $r->ID_NOTA.'" >';
                }

//                echo $totaVendas1;

                ?>

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



                <tr>
                    <td>
                        <?php echo $acaoPagamento; ?>
                    </td>
<!--                    <td style="font-size:x-small">--><?php //echo date('d/m/Y', strtotime($r->DT_PREVENTIVA)) ?><!--</td>-->
<!--                    <td style="width: 100px">--><?php //echo $r->ID_NOTA  ?><!--</td>-->
                    <td><?php echo $r->NU_PEDIDO  ?></td>
                    <td><?php echo $r->CLIENTE_ID ?></td>
                    <td><?php echo $r->NM_VENDEDOR ?></td>
                    <td ><?php echo  date('d/m/Y', strtotime($r->DT_NOTA))  ?></td>
                    <td><?php echo number_format($r->VL_PEDIDO, 2, ',', '.') ?></td>
                    <!--                    <td>--><?php //echo $r->DS_TIPO_MANUTENCAO=="P"?"Preventiva":"Corretiva" ?><!--</td>-->
                    <td><?php echo $r->NM_PRODUTO ?></td>

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

                    <td style="color: <?php echo $statusPreventiva ?>">
                        <?php echo is_null($r->DT_PREVISAO_PAGAMENTO)?'': date('d/m/Y', strtotime($r->DT_PREVISAO_PAGAMENTO))
                        ?>
                    </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 class="dropdown-item <?php echo is_null($r->DT_QUITADO)?'':'isDisabled';  ?>" href="#"
                                       notaAQuitar="<?php echo $r->ID_NOTA ?>"


                                       data-toggle="modal" data-target="#quitarNotaModal"
                                    >
                                        <i class="fa fa-money"></i> Quitar
                                    </a>
                                    <a class="dropdown-item <?php echo is_null($r->DT_QUITADO)?'':'isDisabled';  ?>" href="<?php echo base_url().'manutencao/editarNotasDetalhado/'.$r->ID_NOTA ?>"
                                    ><i class="fa fa-pencil"></i> Editar</a>

<!--                                    <a href="#"  class=""-->
<!---->
<!--                                    > Quitar nota-->
<!--                                    </a>-->


                                    <a href="#" class="dropdown-item <?php echo is_null($r->DT_QUITADO)?'':'isDisabled';  ?>"
                                       servicoItem="<?php echo $r->ID_NOTA ?>"

                                       data-toggle="modal" data-target="#modalExluirItem"
                                    > <i class="fa fa-trash-o text-danger"> Excluir</i>
                                    </a>


                                    <a class="dropdown-item <?php echo !is_null($r->DT_QUITADO)?'':'isDisabled';  ?>" href="#"
                                       notaADesQuitar="<?php echo $r->ID_NOTA ?>"


                                       data-toggle="modal" data-target="#quitarNotaModal"
                                    >
                                        <i class="fa fa-close text-danger"></i> DesQuitar
                                    </a>

                                    <a href="#" class="dropdown-item "
                                       imprimirNota="<?php echo $r->ID_NOTA ?>"

                                       data-toggle=""
                                       data-target=""
                                    > <i class="fa fa-print"> Imprimir</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>

    </form>
</div>

<?php

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

?>

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


<?php $this->load->view("sistema/modais/notas/editarNotaModal"); ?>
<?php $this->load->view("sistema/modais/notas/adicionarNotaModal"); ?>
<?php $this->load->view("sistema/modais/notas/quitarNotaModal"); ?>
<?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>

