<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<section class="content-header">
    <h1><i class="fa fa-user"></i> BPO – Ficha Financeira do Cliente</h1>
    <ol class="breadcrumb">
        <li><a href="<?php echo site_url('admin/financeiro/bpo')?>"><i class="fa fa-university"></i> BPO</a></li>
        <li class="active"><?php echo htmlspecialchars($cliente->name??'Cliente')?></li>
    </ol>
</section>
<section class="content">

<div class="row">
    <!-- Info cliente -->
    <div class="col-md-4">
        <div class="box box-primary">
            <div class="box-header with-border"><h3 class="box-title"><i class="fa fa-user"></i> Dados do Cliente</h3></div>
            <div class="box-body">
                <dl class="dl-horizontal">
                    <dt>Nome</dt><dd><strong><?php echo htmlspecialchars($cliente->name??'-')?></strong></dd>
                    <dt>E-mail</dt><dd><?php echo htmlspecialchars($cliente->email??'-')?></dd>
                    <dt>Contato</dt><dd><?php echo htmlspecialchars($cliente->contact??'-')?></dd>
                </dl>
                <hr>
                <a href="<?php echo site_url('admin/financeiro/add_honorario?client_id='.($cliente->id??''))?>" class="btn btn-success btn-block">
                    <i class="fa fa-plus"></i> Novo Honorário
                </a>
                <a href="<?php echo site_url('admin/financeiro/bpo_pdf/'.($cliente->id??''))?>" class="btn btn-danger btn-block" style="margin-top:5px">
                    <i class="fa fa-file-pdf-o"></i> Exportar PDF
                </a>
            </div>
        </div>
    </div>

    <!-- Totais -->
    <div class="col-md-8">
        <?php
        $total_r = $total_p = $vencido = 0;
        foreach($lancamentos as $l) {
            if($l->tipo==='receber') { $total_r+=(float)$l->valor; if($l->status==='pendente'&&strtotime($l->vencimento)<time()) $vencido+=(float)$l->valor; }
            else $total_p+=(float)$l->valor;
        }
        ?>
        <div class="row">
            <div class="col-md-6">
                <div class="info-box bg-green">
                    <span class="info-box-icon"><i class="fa fa-arrow-down"></i></span>
                    <div class="info-box-content">
                        <span class="info-box-text">Honorários Total</span>
                        <span class="info-box-number">R$ <?php echo number_format($total_r,2,',','.')?></span>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="info-box <?php echo $vencido>0?'bg-red':'bg-yellow'?>">
                    <span class="info-box-icon"><i class="fa fa-clock-o"></i></span>
                    <div class="info-box-content">
                        <span class="info-box-text"><?php echo $vencido>0?'Vencido':'Pendente'?></span>
                        <span class="info-box-number">R$ <?php echo number_format($vencido>0?$vencido:($total_r-array_sum(array_column(array_filter((array)$lancamentos,fn($x)=>$x->tipo==='receber'&&$x->status==='pago'),'valor')??[0])),2,',','.')?></span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Tabs: Honorários / Recibos -->
<div class="box">
    <div class="box-header with-border">
        <ul class="nav nav-tabs" style="border:none;margin-bottom:-1px">
            <li class="active"><a href="#tab-honorarios" data-toggle="tab"><i class="fa fa-money"></i> Honorários</a></li>
            <li><a href="#tab-recibos" data-toggle="tab"><i class="fa fa-file-text"></i> Recibos (<?php echo count((array)$recibos)?>)</a></li>
        </ul>
    </div>
    <div class="box-body">
        <div class="tab-content">

            <!-- Honorários -->
            <div class="tab-pane active" id="tab-honorarios">
                <table class="table table-striped table-hover" id="tbl-hon">
                    <thead>
                        <tr><th>Descrição</th><th>Processo</th><th>Vencimento</th><th>Valor</th><th>Status</th><th>Ações</th></tr>
                    </thead>
                    <tbody>
                    <?php foreach($lancamentos as $l): ?>
                    <tr class="<?php echo $l->status==='pago'?'success':($l->status==='pendente'&&strtotime($l->vencimento)<time()?'danger':'')?>">
                        <td><?php echo htmlspecialchars($l->descricao)?></td>
                        <td><?php echo htmlspecialchars($l->case_no??'-')?></td>
                        <td><?php echo date('d/m/Y',strtotime($l->vencimento))?></td>
                        <td><strong>R$ <?php echo number_format($l->valor,2,',','.')?></strong></td>
                        <td>
                            <span class="label label-<?php echo $l->status==='pago'?'success':($l->status==='cancelado'?'default':'warning')?>">
                                <?php echo ucfirst($l->status)?>
                            </span>
                        </td>
                        <td>
                            <?php if($l->status==='pendente'): ?>
                            <button class="btn btn-success btn-xs btn-baixar" data-id="<?php echo $l->id?>"><i class="fa fa-check"></i></button>
                            <?php endif; ?>
                            <a href="<?php echo site_url('admin/financeiro/gerar_recibo/'.$l->id)?>" class="btn btn-info btn-xs"><i class="fa fa-file-text"></i></a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>

            <!-- Recibos -->
            <div class="tab-pane" id="tab-recibos">
                <table class="table table-striped table-hover" id="tbl-rec">
                    <thead><tr><th>Número</th><th>Referente</th><th>Data</th><th>Valor</th><th>Ações</th></tr></thead>
                    <tbody>
                    <?php foreach($recibos as $r): ?>
                    <tr>
                        <td><strong><?php echo htmlspecialchars($r->numero)?></strong></td>
                        <td><?php echo htmlspecialchars(mb_substr($r->referente,0,50))?></td>
                        <td><?php echo date('d/m/Y',strtotime($r->data_emissao))?></td>
                        <td>R$ <?php echo number_format($r->valor,2,',','.')?></td>
                        <td>
                            <a href="<?php echo site_url('admin/financeiro/imprimir_recibo/'.$r->id)?>" class="btn btn-info btn-xs" target="_blank"><i class="fa fa-print"></i></a>
                            <a href="<?php echo site_url('admin/financeiro/pdf_recibo/'.$r->id)?>" class="btn btn-danger btn-xs"><i class="fa fa-file-pdf-o"></i></a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>

        </div>
    </div>
</div>

</section>

<!-- Modal Baixar -->
<div class="modal fade" id="modal-baixar">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header" style="background:#00a65a;color:#fff">
                <h4 class="modal-title"><i class="fa fa-check"></i> Dar Baixa</h4>
            </div>
            <form id="form-baixar" method="post">
                <div class="modal-body">
                    <div class="form-group">
                        <label>Data do Pagamento</label>
                        <input type="date" name="data_pagamento" class="form-control" value="<?php echo date('Y-m-d')?>">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                    <button type="submit" class="btn btn-success">Confirmar</button>
                </div>
            </form>
        </div>
    </div>
</div>

<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-hon, #tbl-rec').dataTable({"language":{"url":"//cdn.datatables.net/plug-ins/1.10.21/i18n/Portuguese-Brasil.json"}});
    $('.btn-baixar').on('click',function(){
        $('#form-baixar').attr('action','<?php echo site_url('admin/financeiro/baixar/')?>'+$(this).data('id'));
        $('#modal-baixar').modal('show');
    });
});
</script>
