<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<section class="content-header">
    <h1><i class="fa fa-percent"></i> Comissões</h1>
    <ol class="breadcrumb">
        <li><a href="<?php echo site_url('admin/financeiro')?>"><i class="fa fa-money"></i> Financeiro</a></li>
        <li class="active">Comissões</li>
    </ol>
</section>
<section class="content">
<div class="box">
    <div class="box-header with-border">
        <h3 class="box-title"><i class="fa fa-list"></i> Comissões Registradas</h3>
        <div class="box-tools pull-right">
            <a href="<?php echo site_url('admin/financeiro/add_lancamento')?>" class="btn btn-success btn-sm">
                <i class="fa fa-plus"></i> Novo Lançamento (com comissão)
            </a>
        </div>
    </div>
    <div class="box-body">
        <?php
        $total_pend=0; $total_pago=0;
        foreach($comissoes as $c){
            if($c->status==='pago') $total_pago+=(float)$c->valor_comissao;
            else $total_pend+=(float)$c->valor_comissao;
        }
        ?>
        <div class="row" style="margin-bottom:15px">
            <div class="col-md-4">
                <div class="small-box bg-yellow">
                    <div class="inner"><h3>R$ <?php echo number_format($total_pend,2,',','.')?></h3><p>Pendente</p></div>
                    <div class="icon"><i class="fa fa-clock-o"></i></div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="small-box bg-green">
                    <div class="inner"><h3>R$ <?php echo number_format($total_pago,2,',','.')?></h3><p>Pago</p></div>
                    <div class="icon"><i class="fa fa-check"></i></div>
                </div>
            </div>
        </div>

        <table class="table table-bordered table-striped table-hover" id="tbl-com">
            <thead>
                <tr><th>Advogado</th><th>Lançamento Ref.</th><th>Valor Base</th><th>%</th><th>Comissão</th><th>Status</th><th>Pgto.</th><th>Ação</th></tr>
            </thead>
            <tbody>
            <?php foreach($comissoes as $c): ?>
            <tr class="<?php echo $c->status==='pago'?'success':''?>">
                <td><i class="fa fa-user"></i> <?php echo htmlspecialchars($c->advogado_nome??'-')?></td>
                <td><?php echo htmlspecialchars($c->lanc_desc??'-')?></td>
                <td>R$ <?php echo number_format(isset($c->lanc_valor)?$c->lanc_valor:(isset($c->valor_lancamento)?$c->valor_lancamento:0),2,',','.')?></td>
                <td><?php echo number_format($c->percentual,2,',','.')?>%</td>
                <td><strong>R$ <?php echo number_format($c->valor_comissao,2,',','.')?></strong></td>
                <td><span class="label label-<?php echo $c->status==='pago'?'success':'warning'?>"><?php echo ucfirst($c->status??'pendente')?></span></td>
                <td><?php echo $c->data_pagamento?date('d/m/Y',strtotime($c->data_pagamento)):'-'?></td>
                <td>
                    <?php if(($c->status??'pendente')==='pendente'): ?>
                    <a href="<?php echo site_url('admin/financeiro/pagar_comissao/'.$c->id)?>"
                       class="btn btn-success btn-xs btn-confirmar">
                        <i class="fa fa-check"></i> Pagar
                    </a>
                    <?php else: ?>
                    <i class="fa fa-check-circle text-success"></i>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
</section>
<link href="<?php echo base_url('assets/css/datatables/dataTables.bootstrap.css')?>" rel="stylesheet">
<script src="<?php echo base_url('assets/js/plugins/datatables/jquery.dataTables.js')?>"></script>
<script src="<?php echo base_url('assets/js/plugins/datatables/dataTables.bootstrap.js')?>"></script>
<script>
$(function(){
    $('#tbl-com').dataTable({"order":[[0,'asc']],"language":{"url":"//cdn.datatables.net/plug-ins/1.10.21/i18n/Portuguese-Brasil.json"}});
    $('.btn-confirmar').on('click',function(e){
        if(!confirm('Confirmar pagamento desta comissão?')) e.preventDefault();
    });
});
</script>
