<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<section class="content-header">
    <h1><i class="fa fa-plus"></i> Registrar Honorário</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">Novo Honorário</li>
    </ol>
</section>
<section class="content">
<div class="row"><div class="col-md-9 col-md-offset-1">
<div class="box box-primary">
    <div class="box-header with-border"><h3 class="box-title">Dados do Honorário</h3></div>
    <form method="post" action="<?php echo site_url('admin/financeiro/add_honorario')?>">
    <div class="box-body">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label>Cliente <span class="text-danger">*</span></label>
                    <select name="client_id" class="form-control" required>
                        <option value="">Selecione...</option>
                        <?php foreach($clientes as $c): ?>
                        <option value="<?php echo $c->id?>" <?php echo $this->input->get('client_id')==$c->id?'selected':''?>>
                            <?php echo htmlspecialchars($c->name)?>
                        </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div class="col-md-3">
                <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>
            <div class="col-md-3">
                <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>
        </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: Honorários advocatícios – Ação de cobrança nº ...">
        </div>
        <div class="row">
            <div class="col-md-4">
                <div class="form-group">
                    <label>Forma de Pagamento</label>
                    <select name="forma_pagamento" class="form-control">
                        <option value="">Selecione...</option>
                        <option>PIX</option><option>Dinheiro</option>
                        <option>Transferência</option><option>Boleto</option>
                        <option>Cartão de Crédito</option><option>Depósito</option>
                    </select>
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label>Advogado 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>
            <div class="col-md-4">
                <div class="form-group">
                    <label>Advogado Comissionado</label>
                    <select name="advogado_comissao_id" class="form-control" id="sel-comissao-adv">
                        <option value="">Sem comissão</option>
                        <?php foreach($advogados as $a): ?>
                        <option value="<?php echo $a->id?>"><?php echo htmlspecialchars($a->name)?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
        </div>
        <div class="row" id="row-comissao" style="display:none">
            <div class="col-md-3">
                <div class="form-group">
                    <label>% Comissão</label>
                    <input type="number" name="percentual_comissao" id="inp-perc" class="form-control" min="0" max="100" step="0.01" value="0">
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label>Valor da Comissão</label>
                    <input type="text" id="val-comissao" class="form-control" readonly style="background:#eee">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label>Observação</label>
            <textarea name="observacao" class="form-control" rows="2" placeholder="Observações adicionais..."></textarea>
        </div>
        <input type="hidden" name="submitted" value="1">
    </div>
    <div class="box-footer">
        <button type="submit" class="btn btn-primary"><i class="fa fa-save"></i> Registrar Honorário</button>
        <a href="<?php echo site_url('admin/financeiro/bpo')?>" class="btn btn-default">Cancelar</a>
    </div>
    </form>
</div>
</div></div>
</section>
<script>
$(function(){
    $('#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); calcComissao();
    });
    $('#inp-perc').on('input',calcComissao);
    $('#sel-comissao-adv').on('change',function(){
        $(this).val()?$('#row-comissao').slideDown():$('#row-comissao').slideUp();
    });
    function calcComissao(){
        var v=parseFloat($('#inp-valor').val().replace(/\./g,'').replace(',','.'))||0;
        var p=parseFloat($('#inp-perc').val())||0;
        var c=(v*p/100).toFixed(2).replace('.',',').replace(/(\d)(?=(\d{3})+(?!\d))/g,'$1.');
        $('#val-comissao').val('R$ '+c);
    }
});
</script>
