<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<section class="content-header">
    <h1><i class="fa fa-gavel"></i> Diligências Jurídicas</h1>
    <ol class="breadcrumb"><li class="active">Diligências</li></ol>
</section>
<section class="content">

<?php if($this->session->flashdata('message')): ?>
<div class="alert alert-success alert-dismissible"><button type="button" class="close" data-dismiss="alert">&times;</button><?php echo $this->session->flashdata('message')?></div>
<?php endif; ?>

<!-- Totais -->
<div class="row">
    <div class="col-md-3 col-sm-6">
        <div class="small-box bg-blue"><div class="inner"><h3><?php echo $totais->agendadas??0?></h3><p>Agendadas</p></div><div class="icon"><i class="fa fa-calendar"></i></div></div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="small-box bg-yellow"><div class="inner"><h3><?php echo $totais->em_andamento??0?></h3><p>Em Andamento</p></div><div class="icon"><i class="fa fa-spinner"></i></div></div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="small-box bg-green"><div class="inner"><h3><?php echo $totais->concluidas??0?></h3><p>Concluídas</p></div><div class="icon"><i class="fa fa-check"></i></div></div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="small-box bg-red"><div class="inner"><h3><?php echo $totais->vencidas??0?></h3><p>Prazo Vencido</p></div><div class="icon"><i class="fa fa-exclamation-triangle"></i></div></div>
    </div>
</div>

<!-- Filtros -->
<div class="box box-default">
    <div class="box-header"><h3 class="box-title"><i class="fa fa-filter"></i> Filtros</h3></div>
    <div class="box-body">
        <form method="get" class="form-inline">
            <div class="form-group" style="margin-right:8px">
                <select name="status" class="form-control input-sm">
                    <option value="">Todos os status</option>
                    <option value="agendada" <?php echo $filtros['status']==='agendada'?'selected':''?>>Agendada</option>
                    <option value="em_andamento" <?php echo $filtros['status']==='em_andamento'?'selected':''?>>Em Andamento</option>
                    <option value="concluida" <?php echo $filtros['status']==='concluida'?'selected':''?>>Concluída</option>
                    <option value="cancelada" <?php echo $filtros['status']==='cancelada'?'selected':''?>>Cancelada</option>
                </select>
            </div>
            <div class="form-group" style="margin-right:8px">
                <select name="tipo" class="form-control input-sm">
                    <option value="">Todos os tipos</option>
                    <?php foreach(['audiencia'=>'Audiência','pericia'=>'Perícia','despacho'=>'Despacho','protocolo'=>'Protocolo','citacao'=>'Citação','intimacao'=>'Intimação','sustentacao'=>'Sustentação Oral','outro'=>'Outro'] as $v=>$l): ?>
                    <option value="<?php echo $v?>" <?php echo $filtros['tipo']===$v?'selected':''?>><?php echo $l?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group" style="margin-right:8px">
                <select name="client_id" class="form-control input-sm">
                    <option value="">Todos os clientes</option>
                    <?php foreach($clientes as $c): ?>
                    <option value="<?php echo $c->id?>" <?php echo $filtros['client_id']==$c->id?'selected':''?>><?php echo htmlspecialchars($c->name)?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <input type="date" name="data_ini" class="form-control input-sm" value="<?php echo $filtros['data_ini']?>" style="margin-right:4px">
            <input type="date" name="data_fim" class="form-control input-sm" value="<?php echo $filtros['data_fim']?>" style="margin-right:8px">
            <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-search"></i> Filtrar</button>
            <a href="<?php echo site_url('admin/diligencias')?>" class="btn btn-default btn-sm">Limpar</a>
        </form>
    </div>
</div>

<!-- Lista -->
<div class="box box-primary">
    <div class="box-header with-border">
        <h3 class="box-title"><?php echo count($diligencias)?> diligência(s)</h3>
        <div class="box-tools pull-right">
            <a href="<?php echo site_url('admin/diligencias/add')?>" class="btn btn-success btn-sm"><i class="fa fa-plus"></i> Nova Diligência</a>
        </div>
    </div>
    <div class="box-body no-padding">
        <table class="table table-striped table-hover" id="tbl-dilig">
            <thead><tr><th>Data/Hora</th><th>Tipo</th><th>Título</th><th>Processo</th><th>Local</th><th>Cliente</th><th>Responsável</th><th>Prazo</th><th>Status</th><th>Ações</th></tr></thead>
            <tbody>
            <?php foreach($diligencias as $d):
                $vencida = $d->data_prazo && strtotime($d->data_prazo)<time() && !in_array($d->status,['concluida','cancelada']);
                $hoje = $d->data_hora && date('Y-m-d',strtotime($d->data_hora))===date('Y-m-d');
                $colors = ['agendada'=>'primary','em_andamento'=>'warning','concluida'=>'success','cancelada'=>'default'];
            ?>
            <tr class="<?php echo $vencida?'danger':($hoje?'warning':'')?>">
                <td><?php echo $d->data_hora?date('d/m/Y H:i',strtotime($d->data_hora)):'-'?> <?php if($hoje): ?><span class="label label-warning">Hoje</span><?php endif; ?></td>
                <td><span class="label label-info"><?php echo ucfirst($d->tipo??'-')?></span></td>
                <td><strong><?php echo htmlspecialchars($d->titulo)?></strong></td>
                <td><?php echo htmlspecialchars($d->case_no??'-')?></td>
                <td><?php echo htmlspecialchars($d->local??'-')?></td>
                <td><?php echo htmlspecialchars($d->client_nome??'-')?></td>
                <td><?php echo htmlspecialchars($d->responsavel_nome??'-')?></td>
                                <td><?php if($d->data_prazo): ?><span class="<?php echo $vencida?'text-danger':''?>"><?php echo date('d/m/Y',strtotime($d->data_prazo))?><?php if($vencida): ?> <i class="fa fa-exclamation-circle"></i><?php endif; ?></span><?php else: ?>-<?php endif; ?></td>
                <td><span class="label label-<?php echo $colors[$d->status]??'default'?>"><?php echo ucfirst(str_replace('_',' ',$d->status??''))?></span></td>
                <td style="white-space:nowrap">
                    <a href="<?php echo site_url('admin/diligencias/view/'.$d->id)?>" class="btn btn-info btn-xs"><i class="fa fa-eye"></i></a>
                    <a href="<?php echo site_url('admin/diligencias/add/'.$d->id)?>" class="btn btn-warning btn-xs"><i class="fa fa-pencil"></i></a>
                    <?php if($d->status!=='concluida'): ?><button class="btn btn-success btn-xs btn-concluir" data-id="<?php echo $d->id?>"><i class="fa fa-check"></i></button><?php endif; ?>
                    <a href="<?php echo site_url('admin/diligencias/delete/'.$d->id)?>" class="btn btn-danger btn-xs" onclick="return confirm('Excluir?')"><i class="fa fa-trash"></i></a>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
</section>

<div class="modal fade" id="modal-concluir">
    <div class="modal-dialog"><div class="modal-content">
        <div class="modal-header" style="background:#1D6B3B;color:#fff"><h4 class="modal-title"><i class="fa fa-check"></i> Concluir Diligência</h4></div>
        <form id="form-concluir" method="post">
        <div class="modal-body">
            <div class="form-group"><label>Resultado / Observações</label>
            <textarea name="resultado" class="form-control" rows="4" placeholder="Descreva o resultado..."></textarea></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"><i class="fa fa-check"></i> Concluir</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-dilig').dataTable({"order":[[0,'asc']],"pageLength":25,"language":{"url":"//cdn.datatables.net/plug-ins/1.10.21/i18n/Portuguese-Brasil.json"}});
    $('.btn-concluir').on('click',function(){
        $('#form-concluir').attr('action','<?php echo site_url('admin/diligencias/concluir/')?>'+$(this).data('id'));
        $('#modal-concluir').modal('show');
    });
});
</script>
