<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<style>
/* ── Reset e Base ─────────────────────────────── */
.content-wrapper { background:#f0f2f5; }
.content-header h1 { font-size:22px; font-weight:700; color:#1a1a2e; }
.content-header small { font-size:13px; color:#888; font-weight:400; }

/* ── Cards Financeiros ────────────────────────── */
.fin-card {
    border-radius:12px; padding:20px 22px; color:#fff;
    margin-bottom:16px; position:relative; overflow:hidden;
    box-shadow:0 4px 15px rgba(0,0,0,.15);
    transition:transform .2s, box-shadow .2s;
}
.fin-card:hover { transform:translateY(-2px); box-shadow:0 8px 25px rgba(0,0,0,.2); }
.fin-card .val { font-size:26px; font-weight:700; margin:8px 0 4px; letter-spacing:-.5px; }
.fin-card .lbl { font-size:11px; opacity:.85; text-transform:uppercase; letter-spacing:1px; }
.fin-card .ico { position:absolute; right:18px; top:50%; transform:translateY(-50%); font-size:48px; opacity:.15; }
.fin-card .fin-link { color:rgba(255,255,255,.75); font-size:11px; text-decoration:none; }
.fin-card .fin-link:hover { color:#fff; }
.fin-card.verde   { background:linear-gradient(135deg,#1D6B3B,#27ae60); }
.fin-card.azul    { background:linear-gradient(135deg,#1F4E79,#2980b9); }
.fin-card.laranja { background:linear-gradient(135deg,#C55A11,#e67e22); }
.fin-card.vermelho{ background:linear-gradient(135deg,#C00000,#e74c3c); }

/* ── Small Boxes modernizados ─────────────────── */
.small-box { border-radius:12px; box-shadow:0 4px 15px rgba(0,0,0,.1); overflow:hidden; transition:transform .2s; }
.small-box:hover { transform:translateY(-2px); }
.small-box .inner { padding:16px 18px 8px; }
.small-box .inner h3 { font-size:32px; font-weight:700; margin:0 0 4px; }
.small-box .inner p { font-size:13px; margin:0; opacity:.9; }
.small-box .small-box-footer { padding:8px 15px; font-size:12px; }

/* ── Boxes gerais modernizados ────────────────── */
.box {
    border-radius:12px !important;
    border:none !important;
    box-shadow:0 2px 12px rgba(0,0,0,.08) !important;
    margin-bottom:20px;
}
.box.box-primary { border-top:3px solid #1F4E79 !important; }
.box.box-success  { border-top:3px solid #1D6B3B !important; }
.box.box-danger   { border-top:3px solid #C00000 !important; }
.box.box-warning  { border-top:3px solid #C55A11 !important; }
.box-header { border-radius:12px 12px 0 0 !important; padding:14px 18px !important; }
.box-header.with-border { border-bottom:1px solid #f0f0f0 !important; }
.box-header .box-title { font-size:14px; font-weight:600; color:#333; }
.box-footer { border-radius:0 0 12px 12px !important; background:#fafafa !important; border-top:1px solid #f0f0f0 !important; }

/* ── Agenda / Diligências items ───────────────── */
.appt-item {
    padding:10px 14px; border-left:4px solid #2E75B6;
    margin-bottom:8px; background:#f8f9fa;
    border-radius:0 8px 8px 0;
    transition:background .15s;
}
.appt-item:hover { background:#eef3ff; }
.appt-item.hoje   { border-color:#1D6B3B; background:#f0fff4; }
.appt-item.passado { border-color:#e74c3c; background:#fff5f5; opacity:.8; }
.appt-item .appt-time  { font-size:11px; color:#888; margin-bottom:3px; }
.appt-item .appt-title { font-weight:600; color:#222; font-size:14px; line-height:1.3; }
.appt-item .appt-client { font-size:12px; color:#666; margin-top:2px; }

/* ── Todo list modernizado ────────────────────── */
.todo-list > li { border-radius:8px !important; margin-bottom:4px !important; padding:8px 12px !important; }
.todo-list > li:hover { background:#f5f7ff; }

/* ── Seção header modernizado ─────────────────── */
.content-header {
    background:linear-gradient(135deg,#1a1a2e,#0f3460);
    padding:18px 24px !important;
    margin-bottom:20px;
    border-radius:0;
}
.content-header h1 { color:#fff !important; margin:0; }
.content-header h1 small { color:rgba(255,255,255,.6) !important; }
.content-header .breadcrumb { background:transparent !important; margin:6px 0 0; padding:0; }
.content-header .breadcrumb li { color:rgba(255,255,255,.6); font-size:12px; }
.content-header .breadcrumb li.active { color:rgba(255,255,255,.9); }
.content-header .breadcrumb > li + li:before { color:rgba(255,255,255,.4); }

/* ── Gráfico box ──────────────────────────────── */
.box-fin { box-shadow:0 4px 20px rgba(0,0,0,.1) !important; }

/* ── Contas vencidas box ──────────────────────── */
.conta-vencida-item {
    padding:10px 14px; border-left:3px solid #C00000;
    margin-bottom:8px; background:#fff5f5;
    border-radius:0 8px 8px 0;
}
.conta-vencida-item .cv-data { font-size:11px; color:#C00000; font-weight:600; }
.conta-vencida-item .cv-nome { font-weight:600; color:#333; font-size:13px; }
.conta-vencida-item .cv-valor { font-size:13px; color:#C00000; font-weight:700; }

/* ── Section divider ──────────────────────────── */
.section-divider {
    display:flex; align-items:center; margin:20px 0 16px;
    color:#888; font-size:12px; font-weight:600; text-transform:uppercase; letter-spacing:1px;
}
.section-divider::after {
    content:''; flex:1; height:1px; background:#e0e0e0; margin-left:12px;
}
</style>

<section class="content-header">
    <h1><span style="color: #2b7de9;"><i class="fa fa-dashboard"></i> <?php echo lang('dashboard')?>
        <small><span style="color: #2b7de9;"><?php 
            $dias = ['Sunday' => 'Domingo', 'Monday' => 'Segunda-feira', 'Tuesday' => 'Terça-feira', 'Wednesday' => 'Quarta-feira', 'Thursday' => 'Quinta-feira', 'Friday' => 'Sexta-feira', 'Saturday' => 'Sábado'];
            $meses = ['January' => 'Janeiro', 'February' => 'Fevereiro', 'March' => 'Março', 'April' => 'Abril', 'May' => 'Maio', 'June' => 'Junho', 'July' => 'Julho', 'August' => 'Agosto', 'September' => 'Setembro', 'October' => 'Outubro', 'November' => 'Novembro', 'December' => 'Dezembro'];
            echo $dias[date('l')] . date(', d \d\e ') . $meses[date('F')] . date(' \d\e Y');
        ?></span></small>
    </h1>
    <ol class="breadcrumb">
        <li class="active"><i class="fa fa-home"></i> Dashboard</li>
    </ol>
</section>


<section class="content">
<?php
$admin  = $this->session->userdata('admin');
$access = $admin['user_role'];
$fin    = $financeiro_mes ?? null;
?>

<!-- ── CONTADORES ─────────────────────────────── -->
<?php if($access == 2): ?>
<div class="row">
    <div class="col-lg-3 col-xs-6">
        <div class="small-box bg-aqua">
            <div class="inner"><h3><?php echo is_array($my_cases)?count($my_cases):0?></h3><p><?php echo lang('my_cases')?></p></div>
            <div class="icon"><i class="fa fa-list"></i></div>
            <a href="<?php echo site_url('admin/my_cases')?>" class="small-box-footer"><?php echo lang('more_info')?> <i class="fa fa-arrow-circle-right"></i></a>
        </div>
    </div>
</div>
<?php else: ?>
<div class="row">
    <div class="col-lg-3 col-xs-6">
        <div class="small-box bg-aqua">
            <div class="inner"><h3><?php echo (is_array($clients)?count($clients):0)?></h3><p><?php echo lang('clients')?></p></div>
            <div class="icon"><i class="fa fa-users"></i></div>
            <a href="<?php echo site_url('admin/clients')?>" class="small-box-footer"><?php echo lang('more_info')?> <i class="fa fa-arrow-circle-right"></i></a>
        </div>
    </div>
    <div class="col-lg-3 col-xs-6">
        <div class="small-box bg-green">
            <div class="inner"><h3><?php echo (is_array($case_all)?count($case_all):0)?></h3><p><?php echo lang('cases')?></p></div>
            <div class="icon"><i class="fa fa-folder-open"></i></div>
            <a href="<?php echo site_url('admin/cases')?>" class="small-box-footer"><?php echo lang('more_info')?> <i class="fa fa-arrow-circle-right"></i></a>
        </div>
    </div>
    <div class="col-lg-3 col-xs-6">
        <div class="small-box bg-yellow">
            <div class="inner"><h3><?php echo (is_array($starred)?count($starred):0)?></h3><p><?php echo lang('starred_cases')?></p></div>
            <div class="icon"><i class="fa fa-star"></i></div>
            <a href="<?php echo site_url('admin/cases/starred_cases')?>" class="small-box-footer"><?php echo lang('more_info')?> <i class="fa fa-arrow-circle-right"></i></a>
        </div>
    </div>
    <div class="col-lg-3 col-xs-6">
        <div class="small-box bg-red">
            <div class="inner"><h3><?php echo (is_array($archived)?count($archived):0)?></h3><p><?php echo lang('archived_cases')?></p></div>
            <div class="icon"><i class="fa fa-archive"></i></div>
            <a href="<?php echo site_url('admin/cases/archived_cases')?>" class="small-box-footer"><?php echo lang('more_info')?> <i class="fa fa-arrow-circle-right"></i></a>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-lg-3 col-xs-6">
        <div class="small-box bg-blue">
            <div class="inner"><h3><?php echo (is_array($employees)?count($employees):0)?></h3><p><?php echo lang('employees')?></p></div>
            <div class="icon"><i class="fa fa-id-badge"></i></div>
            <a href="<?php echo site_url('admin/employees')?>" class="small-box-footer"><?php echo lang('more_info')?> <i class="fa fa-arrow-circle-right"></i></a>
        </div>
    </div>
    <div class="col-lg-3 col-xs-6">
        <div class="small-box bg-red">
            <div class="inner"><h3><?php echo (is_array($tasks)?count($tasks):0)?></h3><p><?php echo lang('tasks')?></p></div>
            <div class="icon"><i class="fa fa-tasks"></i></div>
            <a href="<?php echo site_url('admin/tasks')?>" class="small-box-footer"><?php echo lang('more_info')?> <i class="fa fa-arrow-circle-right"></i></a>
        </div>
    </div>
    <div class="col-lg-3 col-xs-6">
        <div class="small-box" style="background:#6c5ce7">
            <div class="inner"><h3><?php echo (is_array($to_do_list)?count($to_do_list):0)?></h3><p><?php echo lang('to_do_list')?></p></div>
            <div class="icon"><i class="fa fa-check-square-o"></i></div>
            <a href="<?php echo site_url('admin/to_do_list')?>" class="small-box-footer" style="background:rgba(0,0,0,.1)"><?php echo lang('more_info')?> <i class="fa fa-arrow-circle-right"></i></a>
        </div>
    </div>
    <div class="col-lg-3 col-xs-6">
        <div class="small-box bg-aqua">
            <div class="inner"><h3><?php echo (is_array($my_tasks)?count($my_tasks):0)?></h3><p><?php echo lang('my_tasks')?></p></div>
            <div class="icon"><i class="fa fa-inbox"></i></div>
            <a href="<?php echo site_url('admin/tasks/my_tasks')?>" class="small-box-footer"><?php echo lang('more_info')?> <i class="fa fa-arrow-circle-right"></i></a>
        </div>
    </div>
</div>
<?php endif; ?>

<!-- ── QUADROS OPERACIONAIS ───────────────────── -->
<div class="section-divider"><i class="fa fa-calendar-check-o" style="margin-right:6px"></i> Atividades do Dia</div>
<div class="row">
<?php if(check_user_role(105)==1): ?>
<div class="col-lg-4">
    <div class="box box-primary">
        <div class="box-header with-border">
            <h3 class="box-title"><i class="fa fa-folder text-blue" style="margin-right:6px"></i><?php echo lang('todays_cases')?></h3>
            <div class="box-tools pull-right"><a href="<?php echo site_url('admin/cases/view_all')?>" class="btn btn-xs btn-default">Ver todos</a></div>
        </div>
        <div class="box-body" style="padding:10px">
            <ul class="todo-list">
            <?php if(isset($cases) && count($cases)>0) foreach($cases as $new): ?>
            <li><span class="text"><a href="<?php echo site_url('admin/cases/dates/'.$new->case_id)?>"><?php echo $new->case_no." — ".$new->title?></a></span></li>
            <?php endforeach; ?>
            <?php if(empty($cases)): ?><li><span class="text text-muted"><i class="fa fa-check-circle text-success"></i> Nenhum processo para hoje</span></li><?php endif; ?>
            </ul>
        </div>
    </div>
</div>
<?php endif; ?>

<?php if(check_user_role(107)==1): ?>
<div class="col-lg-4">
    <div class="box box-success">
        <div class="box-header with-border">
            <h3 class="box-title"><i class="fa fa-tasks" style="color:#1D6B3B;margin-right:6px"></i><?php echo lang('todays_to_do')?></h3>
            <div class="box-tools pull-right"><a href="<?php echo site_url('admin/to_do_list/view_all')?>" class="btn btn-xs btn-default">Ver todos</a></div>
        </div>
        <div class="box-body" style="padding:10px">
            <ul class="todo-list">
            <?php if(isset($to_do) && count((array)$to_do)>0) foreach($to_do as $new): ?>
            <li><span class="text"><a href="<?php echo site_url('admin/to_do_list')?>"><?php echo $new->title?></a></span></li>
            <?php endforeach; ?>
            <?php if(empty($to_do)): ?><li><span class="text text-muted"><i class="fa fa-check-circle text-success"></i> Nenhuma tarefa para hoje</span></li><?php endif; ?>
            </ul>
        </div>
    </div>
</div>
<?php endif; ?>

<div class="col-lg-4">
    <div class="box box-warning">
        <div class="box-header with-border">
            <h3 class="box-title"><i class="fa fa-bullhorn" style="color:#C55A11;margin-right:6px"></i><?php echo lang('notice_board')?></h3>
            <div class="box-tools pull-right"><a href="<?php echo site_url('admin/notice/')?>" class="btn btn-xs btn-default">Ver todos</a></div>
        </div>
        <div class="box-body" style="padding:10px">
            <ul class="todo-list">
            <?php if(isset($notice)) foreach($notice as $new): ?>
            <li>
                <span class="label label-info" style="font-size:10px"><?php echo date_time_convert($new->date_time)?></span>
                <span class="text" style="margin-left:6px"><?php echo $new->title?></span>
                <div class="tools"><a href="<?php echo site_url('admin/notice/view/'.$new->id)?>"><i class="fa fa-eye"></i></a></div>
            </li>
            <?php endforeach; ?>
            </ul>
        </div>
    </div>
</div>
</div>

<!-- ── AGENDA + DILIGÊNCIAS + ATTENDANCE ──────── -->
<div class="section-divider"><i class="fa fa-calendar" style="margin-right:6px"></i> Agenda e Compromissos</div>
<div class="row">
    <!-- Agendamentos -->
    <div class="col-md-4">
        <div class="box box-fin" style="border-top:3px solid #1F4E79">
            <div class="box-header with-border" style="background:linear-gradient(135deg,#1F4E79,#2E75B6);border-radius:12px 12px 0 0">
                <h3 class="box-title" style="color:#fff"><i class="fa fa-calendar"></i> Agendamentos</h3>
                <div class="box-tools pull-right">
                    <a href="<?php echo site_url('admin/appointments')?>" class="btn btn-xs" style="background:rgba(255,255,255,.2);color:#fff">Ver todos</a>
                    <a href="<?php echo site_url('admin/appointments/add')?>" class="btn btn-xs" style="background:rgba(255,255,255,.15);color:#fff"><i class="fa fa-plus"></i></a>
                </div>
            </div>
            <div class="box-body" style="padding:12px;max-height:300px;overflow-y:auto">
                <?php if(empty($upcoming_appointments)): ?>
                <div class="text-center text-muted" style="padding:20px">
                    <i class="fa fa-calendar-o" style="font-size:28px;opacity:.3;display:block;margin-bottom:8px"></i>
                    Nenhum agendamento próximo
                </div>
                <?php else: ?>
                <?php foreach($upcoming_appointments as $a):
                    $dt=$strtotime=$hoje=null;
                    $dt=strtotime($a->date_time);
                    $hoje=date('Y-m-d')===date('Y-m-d',$dt);
                    $passado=$dt<time();
                ?>
                <div class="appt-item <?php echo $hoje?'hoje':($passado?'passado':'')?>">
                    <div class="appt-time"><i class="fa fa-clock-o"></i> <?php echo date('d/m',$dt)?> às <?php echo date('H:i',$dt)?><?php if($hoje): ?> <span class="label label-success" style="font-size:9px">Hoje</span><?php endif; ?></div>
                    <div class="appt-title"><?php echo htmlspecialchars($a->title)?></div>
                    <?php if(!empty($a->client_nome)): ?><div class="appt-client"><i class="fa fa-user"></i> <?php echo htmlspecialchars($a->client_nome)?></div><?php endif; ?>
                </div>
                <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Diligências -->
    <div class="col-md-4">
        <div class="box box-fin" style="border-top:3px solid #7F6000">
            <div class="box-header with-border" style="background:linear-gradient(135deg,#7F6000,#C55A11);border-radius:12px 12px 0 0">
                <h3 class="box-title" style="color:#fff"><i class="fa fa-gavel"></i> Diligências</h3>
                <div class="box-tools pull-right">
                    <a href="<?php echo site_url('admin/diligencias')?>" class="btn btn-xs" style="background:rgba(255,255,255,.2);color:#fff">Ver todas</a>
                    <a href="<?php echo site_url('admin/diligencias/add')?>" class="btn btn-xs" style="background:rgba(255,255,255,.15);color:#fff"><i class="fa fa-plus"></i></a>
                </div>
            </div>
            <div class="box-body" style="padding:12px;max-height:300px;overflow-y:auto">
                <?php if(empty($proximas_diligencias)): ?>
                <div class="text-center text-muted" style="padding:20px">
                    <i class="fa fa-gavel" style="font-size:28px;opacity:.3;display:block;margin-bottom:8px"></i>
                    Nenhuma diligência próxima
                </div>
                <?php else: ?>
                <?php foreach($proximas_diligencias as $d):
                    $dt=strtotime($d->data_hora);
                    $hoje=date('Y-m-d')===date('Y-m-d',$dt);
                ?>
                <div class="appt-item <?php echo $hoje?'hoje':''?>" style="border-left-color:#C55A11">
                    <div class="appt-time"><i class="fa fa-clock-o"></i> <?php echo date('d/m',$dt)?> às <?php echo date('H:i',$dt)?><?php if($hoje): ?> <span class="label label-warning" style="font-size:9px">Hoje</span><?php endif; ?> <span class="label label-info" style="font-size:9px"><?php echo ucfirst($d->tipo??'')?></span></div>
                    <div class="appt-title"><?php echo htmlspecialchars($d->titulo)?></div>
                    <?php if(!empty($d->case_no)): ?><div class="appt-client"><i class="fa fa-folder"></i> <?php echo htmlspecialchars($d->case_no)?></div><?php endif; ?>
                    <?php if(!empty($d->local)): ?><div class="appt-client"><i class="fa fa-map-marker"></i> <?php echo htmlspecialchars($d->local)?></div><?php endif; ?>
                </div>
                <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Attendance -->
    <?php if($access==1): ?>
    <div class="col-md-4">
        <div class="box box-fin" style="border-top:3px solid #2c3e50">
            <div class="box-header with-border" style="background:linear-gradient(135deg,#2c3e50,#3d5166);border-radius:12px 12px 0 0">
                <h3 class="box-title" style="color:#fff"><i class="fa fa-users"></i> Colaboradores Afastados</h3>
                <div class="box-tools pull-right"><a href="<?php echo site_url('admin/attendance')?>" class="btn btn-xs" style="background:rgba(255,255,255,.2);color:#fff">Ver todos</a></div>
            </div>
            <div class="box-body" style="padding:12px;max-height:300px;overflow-y:auto">
                <?php if(!empty($todays_leaves)): ?>
                <ul class="todo-list">
                    <?php foreach((array)$todays_leaves as $new): ?>
                    <li style="border-radius:8px;padding:8px 12px;margin-bottom:4px">
                        <span class="text"><i class="fa fa-user" style="color:#888;margin-right:6px"></i><?php echo $new->user?></span>
                        <small class="label label-warning pull-right"><?php echo $new->leave_type?></small>
                    </li>
                    <?php endforeach; ?>
                </ul>
                <?php else: ?>
                <div class="text-center text-muted" style="padding:20px">
                    <i class="fa fa-check-circle text-success" style="font-size:28px;display:block;margin-bottom:8px"></i>
                    Nenhum colaborador afastado hoje
                </div>
                <?php endif; ?>
            </div>
            <div class="box-footer">
                <a href="<?php echo site_url('admin/attendance/my_attendance')?>" class="btn btn-default btn-xs"><i class="fa fa-user"></i> Minha presença</a>
                <a href="<?php echo site_url('admin/attendance/my_leaves')?>" class="btn btn-primary btn-xs pull-right"><i class="fa fa-calendar"></i> Minhas faltas</a>
            </div>
        </div>
    </div>
    <?php elseif($access!=2): ?>
    <div class="col-md-4">
        <div class="box" style="border-top:3px solid #2c3e50">
            <div class="box-header with-border"><h3 class="box-title"><i class="fa fa-clock-o"></i> <?php echo lang('attendance')?></h3>
                <div class="box-tools pull-right"><a href="#apply_leave" class="btn btn-xs bg-purple" data-toggle="modal"><i class="fa fa-arrow-circle-up"></i> <?php echo lang('apply_leave')?></a></div>
            </div>
            <div class="box-body" style="padding:16px">
                <?php if(empty($check_today)): ?>
                    <?php if(!empty($att_status) && $att_status->current_status==1): ?>
                    <a href="#mark_out" class="btn btn-warning btn-block" data-toggle="modal"><i class="fa fa-sign-out"></i> <?php echo lang('mark_out')?></a>
                    <?php else: ?>
                    <a href="#mark_in" class="btn btn-success btn-block" data-toggle="modal"><i class="fa fa-sign-in"></i> <?php echo lang('mark_in')?></a>
                    <?php endif; ?>
                <?php else: ?>
                <button class="btn btn-danger btn-block">Hoje: <?php echo $check_today->leave_type?></button>
                <?php endif; ?>
            </div>
            <div class="box-footer">
                <a href="<?php echo site_url('admin/attendance/my_attendance')?>" class="btn btn-default btn-xs"><i class="fa fa-user"></i> Minha presença</a>
                <a href="<?php echo site_url('admin/attendance/my_leaves')?>" class="btn btn-primary btn-xs pull-right"><i class="fa fa-plus"></i> Minhas faltas</a>
            </div>
        </div>
    </div>
    <?php endif; ?>
</div>

<!-- ── FINANCEIRO (somente Admin) ────────────── -->
<?php if($access==1): ?>
<div class="section-divider"><i class="fa fa-money" style="margin-right:6px"></i> Financeiro do Mês</div>
<div class="row">
    <div class="col-md-3 col-sm-6"><div class="fin-card verde"><div class="lbl">Recebido</div><div class="val">R$ <?php echo number_format($fin->recebido??0,2,',','.')?></div><i class="fa fa-arrow-down ico"></i><a href="<?php echo site_url('admin/financeiro/lancamentos/receber?status=pago')?>" class="fin-link">Ver detalhes →</a></div></div>
    <div class="col-md-3 col-sm-6"><div class="fin-card azul"><div class="lbl">A Receber</div><div class="val">R$ <?php echo number_format($fin->a_receber??0,2,',','.')?></div><i class="fa fa-clock-o ico"></i><a href="<?php echo site_url('admin/financeiro/lancamentos/receber?status=pendente')?>" class="fin-link">Ver detalhes →</a></div></div>
    <div class="col-md-3 col-sm-6"><div class="fin-card laranja"><div class="lbl">Pago (Despesas)</div><div class="val">R$ <?php echo number_format($fin->pago??0,2,',','.')?></div><i class="fa fa-arrow-up ico"></i><a href="<?php echo site_url('admin/financeiro/lancamentos/pagar?status=pago')?>" class="fin-link">Ver detalhes →</a></div></div>
    <div class="col-md-3 col-sm-6"><div class="fin-card vermelho"><div class="lbl">Vencido sem Baixa</div><div class="val">R$ <?php echo number_format($fin->vencido??0,2,',','.')?></div><i class="fa fa-exclamation-triangle ico"></i><a href="<?php echo site_url('admin/financeiro')?>" class="fin-link">Ver todos →</a></div></div>
</div>

<div class="row">
    <div class="col-md-7">
        <div class="box box-fin" style="border-top:3px solid #1F4E79">
            <div class="box-header with-border">
                <h3 class="box-title"><i class="fa fa-bar-chart text-blue"></i> Receitas x Despesas — Últimos 6 Meses</h3>
                <div class="box-tools pull-right"><a href="<?php echo site_url('admin/financeiro/relatorio')?>" class="btn btn-xs btn-default"><i class="fa fa-external-link"></i> Relatório</a></div>
            </div>
            <div class="box-body"><canvas id="chart-financeiro" height="260"></canvas></div>
        </div>
    </div>
    <div class="col-md-5">
        <div class="box box-danger" style="border-top:3px solid #C00000">
            <div class="box-header with-border">
                <h3 class="box-title"><i class="fa fa-exclamation-circle text-red"></i> Contas Vencidas</h3>
                <div class="box-tools pull-right"><a href="<?php echo site_url('admin/financeiro')?>" class="btn btn-xs btn-default">Ver financeiro</a></div>
            </div>
            <div class="box-body" style="padding:12px;max-height:260px;overflow-y:auto">
                <?php if(empty($contas_vencidas)): ?>
                <div class="text-center text-muted" style="padding:20px">
                    <i class="fa fa-check-circle text-success" style="font-size:28px;display:block;margin-bottom:8px"></i>
                    Nenhuma conta vencida!
                </div>
                <?php else: ?>
                <?php foreach($contas_vencidas as $cv): ?>
                <div class="conta-vencida-item">
                    <div class="cv-data"><i class="fa fa-calendar-times-o"></i> Venceu: <?php echo date('d/m/Y',strtotime($cv->data_vencimento))?></div>
                    <div class="cv-nome"><?php echo htmlspecialchars($cv->client_nome??'-')?></div>
                    <div class="cv-valor">R$ <?php echo number_format($cv->valor,2,',','.')?></div>
                </div>
                <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
<?php endif; // fim financeiro admin ?>

<!-- ── GOOGLE CALENDAR ────────────────────────── -->
<div class="section-divider"><i class="fa fa-google" style="margin-right:6px"></i> Google Calendar</div>
<div class="row">
    <div class="col-md-8">
        <div class="box" style="border-top:3px solid #4285F4">
            <div class="box-body no-padding" style="border-radius:0 0 12px 12px;overflow:hidden">
                <iframe src="https://calendar.google.com/calendar/embed?src=agendaadvbp%40gmail.com&ctz=America%2FRecife" style="border:0;display:block" width="100%" height="480" frameborder="0" scrolling="no"></iframe>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="box" style="border-top:3px solid #4285F4">
            <div class="box-header with-border">
                <h3 class="box-title"><i class="fa fa-list" style="color:#4285F4"></i> Próximos Eventos</h3>
            </div>
            <div class="box-body no-padding" style="border-radius:0 0 12px 12px;overflow:hidden">
                <iframe src="https://calendar.google.com/calendar/embed?src=agendaadvbp%40gmail.com&ctz=America%2FRecife&mode=AGENDA&showTitle=0&showNav=0&showDate=0&showPrint=0&showTabs=0&showCalendars=0" style="border:0;display:block" width="100%" height="440" frameborder="0" scrolling="auto"></iframe>
            </div>
        </div>
    </div>
</div>

</section>

<script src="https://cdn.jsdelivr.net/npm/chart.js@3.9.1/dist/chart.min.js"></script>
<script>
<?php
$labels=$recebido=$a_receber=$pago=[];
foreach((array)$financeiro_chart as $row){
    $labels[]   =$row->mes_label;
    $recebido[] =(float)$row->recebido;
    $a_receber[]=(float)$row->a_receber;
    $pago[]     =(float)$row->pago;
}
?>
var ctx=document.getElementById('chart-financeiro');
if(ctx){new Chart(ctx,{type:'bar',data:{labels:<?php echo json_encode($labels)?>,datasets:[
    {label:'Recebido',data:<?php echo json_encode($recebido)?>,backgroundColor:'rgba(29,107,59,.85)',borderColor:'#1D6B3B',borderWidth:1,borderRadius:5},
    {label:'A Receber',data:<?php echo json_encode($a_receber)?>,backgroundColor:'rgba(46,117,182,.6)',borderColor:'#2E75B6',borderWidth:1,borderRadius:5},
    {label:'Despesas',data:<?php echo json_encode($pago)?>,backgroundColor:'rgba(197,90,17,.75)',borderColor:'#C55A11',borderWidth:1,borderRadius:5}
]},options:{responsive:true,maintainAspectRatio:false,plugins:{legend:{position:'top'},tooltip:{callbacks:{label:function(c){return c.dataset.label+': R$ '+c.parsed.y.toLocaleString('pt-BR',{minimumFractionDigits:2})}}}},scales:{y:{beginAtZero:true,ticks:{callback:function(v){return 'R$ '+v.toLocaleString('pt-BR',{minimumFractionDigits:0})}}}}}});}
</script>
