<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<section class="content-header">
    <h1><i class="fa fa-user-circle"></i> Pró-labore, Lucros e Despesas Fixas</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">Pró-labore e Lucros</li>
    </ol>
</section>
<section class="content">

<!-- Cards resumo -->
<div class="row">
    <div class="col-md-4">
        <div class="info-box bg-light-blue">
            <span class="info-box-icon"><i class="fa fa-user"></i></span>
            <div class="info-box-content">
                <span class="info-box-text">Total Pró-labore</span>
                <span class="info-box-number">R$ <?php echo number_format($totais['prolabore']??0,2,',','.')?></span>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="info-box bg-green">
            <span class="info-box-icon"><i class="fa fa-pie-chart"></i></span>
            <div class="info-box-content">
                <span class="info-box-text">Total Lucros Distribuídos</span>
                <span class="info-box-number">R$ <?php echo number_format($totais['lucro']??0,2,',','.')?></span>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="info-box bg-orange">
            <span class="info-box-icon"><i class="fa fa-building"></i></span>
            <div class="info-box-content">
                <span class="info-box-text">Total Despesas Fixas</span>
                <span class="info-box-number">R$ <?php echo number_format($totais['despesa_fixa']??0,2,',','.')?></span>
            </div>
        </div>
    </div>
</div>

<!-- Formulário -->
<div class="row">
<div class="col-md-5">
<div class="box box-primary">
    <div class="box-header with-border"><h3 class="box-title"><i class="fa fa-plus"></i> Registrar</h3></div>
    <form method="post" action="<?php echo site_url('admin/financeiro/prolabore')?>">
    <div class="box-body">
        <div class="form-group">
            <label>Tipo <span class="text-danger">*</span></label>
            <select name="tipo_sub" class="form-control" required>
                <option value="">Selecione...</option>
                <option value="prolabore">Pró-labore</option>
                <option value="lucro">Distribuição de Lucros</option>
                <option value="despesa_fixa">Despesa Fixa</option>
            </select>
        </div>
        <div class="form-group">
            <label>Descrição <span class="text-danger">*</span></label>
            <input type="text" name="descricao" class="form-control" required placeholder="Ex: Pró-labore Dr. Breno – Julho/2025">
        </div>
        <div class="form-group">
            <label>Valor (R$) <span class="text-danger">*</span></label>
            <input type="text" name="valor" id="inp-valor" class="form-control" required placeholder="0,00">
        </div>
        <div class="form-group">
            <label>Vencimento <span class="text-danger">*</span></label>
            <input type="date" name="vencimento" class="form-control" required value="<?php echo date('Y-m-d')?>">
        </div>
        <div class="form-group">
            <label>Responsável</label>
            <select name="responsavel_id" class="form-control">
                <option value="">Selecione...</option>
                <?php foreach($advogados as $a): ?>
                <option value="<?php echo $a->id?>"><?php echo htmlspecialchars($a->name)?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="form-group">
            <label>Observação</label>
            <textarea name="observacao" class="form-control" rows="2"></textarea>
        </div>
        <input type="hidden" name="submitted" value="1">
    </div>
    <div class="box-footer">
        <button type="submit" class="btn btn-primary btn-block"><i class="fa fa-save"></i> Salvar</button>
    </div>
    </form>
</div>
</div>

<!-- Listagem -->
<div class="col-md-7">
<div class="box">
    <div class="box-header with-border"><h3 class="box-title"><i class="fa fa-list"></i> Registros</h3></div>
    <div class="box-body no-padding">
        <table class="table table-striped table-hover" id="tbl-pro">
            <thead><tr><th>Tipo</th><th>Descrição</th><th>Responsável</th><th>Venc.</th><th>Valor</th><th>Status</th></tr></thead>
            <tbody>
            <?php foreach($lancamentos as $l):
                $sub = $l->subcategoria ?? '';
                $tipo_label = ($sub==='prolabore') ? '<span class="label label-info">Pró-labore</span>' : (($sub==='lucro') ? '<span class="label label-success">Lucro</span>' : '<span class="label label-warning">Despesa Fixa</span>');
            ?>
            <tr>
                <td><?php echo $tipo_label?></td>
                <td><?php echo htmlspecialchars($l->descricao)?></td>
                <td><?php echo htmlspecialchars($l->responsavel_nome??'-')?></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':'warning'?>"><?php echo ucfirst($l->status??'pendente')?></span></td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
</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-pro').dataTable({"order":[[3,'desc']]});
    $('#inp-valor').on('input',function(){
        var v=$(this).val().replace(/\D/g,'');
        v=(parseInt(v||0)/100).toFixed(2).replace('.',',').replace(/(\d)(?=(\d{3})+(?!\d))/g,'$1.');
        $(this).val(v);
    });
});
</script>
