<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<link href="<?php echo base_url('assets/css/chosen.css') ?>" rel="stylesheet">
<style>
.form-group label{font-size:11px;font-weight:700;color:#888;text-transform:uppercase;letter-spacing:.5px;margin-bottom:5px;display:block}
</style>

<section class="content-header">
    <h1><i class="fa fa-plus"></i> Novo Expediente de Busca Patrimonial</h1>
    <ol class="breadcrumb">
        <li><a href="<?php echo site_url('admin') ?>"><i class="fa fa-dashboard"></i> Dashboard</a></li>
        <li><a href="<?php echo site_url('admin/patrimonio') ?>">Busca Patrimonial</a></li>
        <li class="active">Novo</li>
    </ol>
</section>

<section class="content">
<div class="box box-primary">
    <div class="box-header with-border" style="background:linear-gradient(135deg,#1e3a5f,#2e75b6);border-radius:10px 10px 0 0">
        <h3 class="box-title" style="color:#fff"><i class="fa fa-plus"></i> Novo Expediente Patrimonial</h3>
    </div>
    <form method="post">
    <div class="box-body">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label>Processo Vinculado</label>
                    <select name="case_id" class="form-control chzn" required>
                        <option value="">-- Selecione o processo --</option>
                        <?php foreach($cases as $c): ?>
                        <option value="<?php echo $c->id ?>">#<?php echo $c->case_no ?> — <?php echo htmlspecialchars($c->title) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <label>Valor da Execução (R$)</label>
                    <input type="text" name="valor_execucao" class="form-control" placeholder="0,00" required>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label>Nome do Devedor</label>
                    <input type="text" name="devedor_nome" class="form-control" required placeholder="Nome completo ou Razão Social">
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label>CPF / CNPJ</label>
                    <input type="text" name="devedor_doc" id="devedor_doc" class="form-control" required placeholder="000.000.000-00 ou 00.000.000/0001-00">
                    <small class="text-muted">O tipo (PF/PJ) é detectado automaticamente</small>
                </div>
            </div>
            <div class="col-md-2">
                <div class="form-group">
                    <label>&nbsp;</label>
                    <button type="button" class="btn btn-default btn-block" id="btn-consultar-api">
                        <i class="fa fa-search"></i> Consultar APIs
                    </button>
                </div>
            </div>
        </div>
        <div id="api-resultado" style="display:none">
            <div class="alert alert-info alert-dismissible">
                <button class="close" data-dismiss="alert">&times;</button>
                <div id="api-conteudo"></div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <label>Observações</label>
                    <textarea name="obs" class="form-control" rows="3" placeholder="Informações relevantes sobre o devedor, patrimônio conhecido, etc."></textarea>
                </div>
            </div>
        </div>
    </div>
    <div class="box-footer">
        <button type="submit" name="ok" value="ok" class="btn btn-primary"><i class="fa fa-save"></i> Criar Expediente</button>
        <a href="<?php echo site_url('admin/patrimonio') ?>" class="btn btn-default"><i class="fa fa-arrow-left"></i> Cancelar</a>
    </div>
    </form>
</div>
</section>

<script src="<?php echo base_url('assets/js/chosen.jquery.min.js') ?>"></script>
<script>
$(function(){
    $('.chzn').chosen({width:'100%'});
    $('#btn-consultar-api').on('click', function(){
        var doc = $('#devedor_doc').val();
        if(!doc){alert('Informe o CPF ou CNPJ primeiro.');return;}
        $(this).html('<i class="fa fa-spin fa-spinner"></i> Consultando...').prop('disabled',true);
        $.ajax({
            url:'<?php echo site_url('admin/patrimonio/api_consultar') ?>',
            type:'POST',data:{documento:doc},
            success:function(r){
                var html='';
                if(r.cnpj&&!r.cnpj.erro){
                    html+='<strong>'+r.cnpj.razao_social+'</strong> — '+r.cnpj.situacao+' | '+r.cnpj.municipio;
                    if(r.cnpj.socios&&r.cnpj.socios.length){
                        html+='<br><small>Sócios: '+r.cnpj.socios.map(function(s){return s.nome+' ('+s.qual+')'}).join(', ')+'</small>';
                    }
                }
                if(r.datajud&&r.datajud.total>0){
                    html+='<br><i class="fa fa-exclamation-triangle text-warning"></i> <strong>DataJud:</strong> '+r.datajud.total+' processo(s) localizado(s) em outros tribunais';
                }
                if(!html)html='Nenhum dado encontrado nas APIs.';
                $('#api-conteudo').html(html);
                $('#api-resultado').show();
                $('#btn-consultar-api').html('<i class="fa fa-check"></i> Consultado').prop('disabled',false);
            },
            error:function(){$('#btn-consultar-api').html('<i class="fa fa-search"></i> Consultar APIs').prop('disabled',false);}
        });
    });
});
</script>
