<div class="row mb-4">
    <div class="col-12">
        <h4 class="mb-0">Bienvenue, <?= $this->auth_lib->full_name(); ?></h4>
        <p class="text-muted mb-0">Ligne Moroni — Mutsamudu | <?= date('d/m/Y'); ?></p>
    </div>
</div>

<!-- KPIs -->
<div class="row g-3 mb-4">
    <!-- Dépenses du mois -->
    <div class="col-md-3">
        <div class="card border-start border-primary border-4 shadow-sm h-100">
            <div class="card-body py-3">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <div class="text-muted small">Dépenses du mois</div>
                        <div class="h4 fw-bold mb-0"><?= number_format($monthly_expenses, 0, ',', ' '); ?></div>
                        <small class="text-muted">KMF</small>
                    </div>
                    <i class="bi bi-receipt fs-2 text-primary opacity-50"></i>
                </div>
            </div>
        </div>
    </div>

    <!-- En attente -->
    <div class="col-md-3">
        <div class="card border-start border-warning border-4 shadow-sm h-100">
            <div class="card-body py-3">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <div class="text-muted small">À valider</div>
                        <div class="h4 fw-bold mb-0">
                            <?= $pending_expenses; ?> <small class="fs-6 fw-normal text-muted">dépense(s)</small>
                        </div>
                        <div class="h6 mb-0">
                            <?= $pending_requests; ?> <small class="text-muted">demande(s)</small>
                        </div>
                    </div>
                    <i class="bi bi-hourglass-split fs-2 text-warning opacity-50"></i>
                </div>
            </div>
        </div>
    </div>

    <!-- Trésorerie -->
    <div class="col-md-3">
        <div class="card border-start border-success border-4 shadow-sm h-100">
            <div class="card-body py-3">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <div class="text-muted small">Trésorerie totale</div>
                        <div class="h4 fw-bold mb-0"><?= number_format($total_treasury, 0, ',', ' '); ?></div>
                        <small class="text-muted">KMF</small>
                    </div>
                    <i class="bi bi-bank fs-2 text-success opacity-50"></i>
                </div>
            </div>
        </div>
    </div>

    <!-- Compte Partenaire -->
    <?php if ($is_manager): ?>
    <div class="col-md-3">
        <div class="card border-start border-info border-4 shadow-sm h-100">
            <div class="card-body py-3">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <div class="text-muted small">Solde Partenaire</div>
                        <div class="h4 fw-bold mb-0 <?= $partner_balance < 0 ? 'text-danger' : ''; ?>">
                            <?= number_format($partner_balance, 0, ',', ' '); ?>
                        </div>
                        <small class="text-muted">KMF</small>
                    </div>
                    <i class="bi bi-journal-text fs-2 text-info opacity-50"></i>
                </div>
            </div>
        </div>
    </div>
    <?php else: ?>
    <div class="col-md-3">
        <div class="card border-start border-secondary border-4 shadow-sm h-100">
            <div class="card-body py-3 text-center">
                <i class="bi bi-geo-alt fs-2 text-secondary opacity-50"></i>
                <div class="fw-semibold mt-1"><?= $this->auth_lib->location_id() ? 'Ma localisation' : 'Toutes les localisations'; ?></div>
            </div>
        </div>
    </div>
    <?php endif; ?>
</div>

<!-- Position des caisses (mini) -->
<div class="row g-3 mb-4">
    <?php foreach ($cashboxes as $cb): ?>
    <?php
        $type_colors = ['bank' => 'primary', 'central' => 'success', 'petty' => 'warning'];
        $color = $type_colors[$cb->type] ?? 'secondary';
    ?>
    <div class="col-md-3">
        <a href="<?= site_url('cashboxes/view/' . $cb->id); ?>" class="card shadow-sm text-decoration-none h-100">
            <div class="card-body py-2">
                <div class="small text-muted"><?= htmlspecialchars($cb->name); ?></div>
                <div class="h5 fw-bold mb-0 text-<?= $color; ?> <?= $cb->current_balance < 0 ? 'text-danger' : ''; ?>">
                    <?= number_format($cb->current_balance, 0, ',', ' '); ?>
                    <small class="fs-6 fw-normal text-muted">KMF</small>
                </div>
            </div>
        </a>
    </div>
    <?php endforeach; ?>
</div>

<div class="row g-3">
    <!-- Colonne gauche : actions en attente -->
    <div class="col-lg-7">

        <!-- Dépenses en attente de validation -->
        <?php if ($is_manager && ! empty($pending_exp_list)): ?>
        <div class="card shadow-sm mb-4">
            <div class="card-header bg-white d-flex justify-content-between align-items-center py-2">
                <h6 class="mb-0"><i class="bi bi-hourglass-split text-warning"></i> Dépenses à valider</h6>
                <a href="<?= site_url('expenses?status=pending'); ?>" class="btn btn-sm btn-outline-primary">Voir tout</a>
            </div>
            <div class="table-responsive">
                <table class="table table-sm table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>ID</th>
                            <th>Fournisseur</th>
                            <th>Lieu</th>
                            <th class="text-end">Montant</th>
                            <th class="text-end"></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($pending_exp_list as $e): ?>
                        <tr>
                            <td><code class="small"><?= $e->expense_id; ?></code></td>
                            <td class="small"><?= htmlspecialchars($e->supplier); ?></td>
                            <td><span class="badge bg-light text-dark small"><?= $e->location_name; ?></span></td>
                            <td class="text-end fw-semibold"><?= number_format($e->amount, 0, ',', ' '); ?></td>
                            <td class="text-end">
                                <a href="<?= site_url('expenses/view/' . $e->id); ?>" class="btn btn-sm btn-outline-success">
                                    <i class="bi bi-check-lg"></i>
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php endif; ?>

        <!-- Demandes en attente d'approbation -->
        <?php if ($is_manager && ! empty($pending_req_list)): ?>
        <div class="card shadow-sm mb-4">
            <div class="card-header bg-white d-flex justify-content-between align-items-center py-2">
                <h6 class="mb-0"><i class="bi bi-file-earmark-text text-info"></i> Demandes à approuver</h6>
                <a href="<?= site_url('requests?status=pending'); ?>" class="btn btn-sm btn-outline-primary">Voir tout</a>
            </div>
            <div class="table-responsive">
                <table class="table table-sm table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>Réf.</th>
                            <th>Catégorie</th>
                            <th>Demandeur</th>
                            <th>Lieu</th>
                            <th class="text-end">Montant</th>
                            <th class="text-end"></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($pending_req_list as $r): ?>
                        <tr>
                            <td><code class="small"><?= $r->reference; ?></code></td>
                            <td class="small"><?= $r->category_label; ?></td>
                            <td class="small"><?= htmlspecialchars($r->requested_by_name); ?></td>
                            <td><span class="badge bg-light text-dark small"><?= $r->location_name; ?></span></td>
                            <td class="text-end fw-semibold"><?= number_format($r->estimated_amount, 0, ',', ' '); ?></td>
                            <td class="text-end">
                                <a href="<?= site_url('requests/view/' . $r->id); ?>" class="btn btn-sm btn-outline-info">
                                    <i class="bi bi-eye"></i>
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php endif; ?>

        <!-- Dernières dépenses -->
        <div class="card shadow-sm">
            <div class="card-header bg-white d-flex justify-content-between align-items-center py-2">
                <h6 class="mb-0"><i class="bi bi-clock-history"></i> Dernières dépenses</h6>
                <a href="<?= site_url('expenses'); ?>" class="btn btn-sm btn-outline-primary">Voir tout</a>
            </div>
            <div class="table-responsive">
                <table class="table table-sm table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>ID</th>
                            <th>Date</th>
                            <th>Fournisseur</th>
                            <th class="text-end">Montant</th>
                            <th>Statut</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($recent_expenses)): ?>
                        <tr><td colspan="5" class="text-center text-muted py-3">Aucune dépense enregistrée.</td></tr>
                        <?php else: ?>
                        <?php foreach ($recent_expenses as $e): ?>
                        <?php
                            $st = ['draft'=>'secondary','pending'=>'warning','validated'=>'success','rejected'=>'danger','batched'=>'info'];
                        ?>
                        <tr>
                            <td><a href="<?= site_url('expenses/view/' . $e->id); ?>" class="text-decoration-none"><code class="small"><?= $e->expense_id; ?></code></a></td>
                            <td class="small"><?= date('d/m', strtotime($e->expense_date)); ?></td>
                            <td class="small"><?= htmlspecialchars($e->supplier); ?></td>
                            <td class="text-end fw-semibold"><?= number_format($e->amount, 0, ',', ' '); ?></td>
                            <td><span class="badge bg-<?= $st[$e->status] ?? 'secondary'; ?> <?= $e->status=='pending'?'text-dark':''; ?>"><?= $e->status; ?></span></td>
                        </tr>
                        <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Colonne droite : répartitions + actions rapides -->
    <div class="col-lg-5">

        <!-- Dépenses par catégorie -->
        <?php if ( ! empty($expenses_by_category)): ?>
        <div class="card shadow-sm mb-4">
            <div class="card-header bg-white py-2">
                <h6 class="mb-0"><i class="bi bi-pie-chart"></i> Dépenses du mois par catégorie</h6>
            </div>
            <div class="card-body py-2">
                <?php 
                    $max_cat = ! empty($expenses_by_category) ? max(array_column((array) $expenses_by_category, 'total')) : 1;
                    $cat_colors = ['#0d6efd','#198754','#ffc107','#dc3545','#0dcaf0','#6f42c1','#fd7e14','#20c997','#6c757d'];
                ?>
                <?php foreach ($expenses_by_category as $i => $cat): ?>
                <div class="mb-2">
                    <div class="d-flex justify-content-between small">
                        <span><?= htmlspecialchars($cat->label); ?> <span class="text-muted">(<?= $cat->count; ?>)</span></span>
                        <span class="fw-semibold"><?= number_format($cat->total, 0, ',', ' '); ?></span>
                    </div>
                    <div class="progress" style="height: 6px;">
                        <div class="progress-bar" role="progressbar" 
                             style="width: <?= round(($cat->total / $max_cat) * 100); ?>%; background-color: <?= $cat_colors[$i % count($cat_colors)]; ?>">
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>

        <!-- Dépenses par localisation -->
        <?php if ($is_manager && ! empty($expenses_by_location)): ?>
        <div class="card shadow-sm mb-4">
            <div class="card-header bg-white py-2">
                <h6 class="mb-0"><i class="bi bi-geo-alt"></i> Dépenses du mois par localisation</h6>
            </div>
            <div class="card-body py-2">
                <?php foreach ($expenses_by_location as $loc): ?>
                <div class="d-flex justify-content-between align-items-center py-1 border-bottom">
                    <div>
                        <span class="fw-semibold"><?= htmlspecialchars($loc->name); ?></span>
                        <span class="text-muted small">(<?= $loc->count; ?> dépenses)</span>
                    </div>
                    <span class="fw-bold"><?= number_format($loc->total, 0, ',', ' '); ?> KMF</span>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>

        <!-- Actions rapides -->
        <div class="card shadow-sm">
            <div class="card-header bg-white py-2">
                <h6 class="mb-0"><i class="bi bi-lightning"></i> Actions rapides</h6>
            </div>
            <div class="card-body">
                <div class="d-grid gap-2">
                    <?php if ($this->auth_lib->has_permission('expense_create')): ?>
                    <a href="<?= site_url('expenses/create'); ?>" class="btn btn-outline-primary text-start">
                        <i class="bi bi-plus-circle"></i> Nouvelle dépense
                    </a>
                    <?php endif; ?>

                    <?php if ($this->auth_lib->has_permission('request_create')): ?>
                    <a href="<?= site_url('requests/create'); ?>" class="btn btn-outline-success text-start">
                        <i class="bi bi-file-earmark-plus"></i> Nouvelle demande
                    </a>
                    <?php endif; ?>

                    <?php if ($this->auth_lib->has_permission('transfer_create')): ?>
                    <a href="<?= site_url('transfers/create'); ?>" class="btn btn-outline-info text-start">
                        <i class="bi bi-arrow-left-right"></i> Nouveau transfert
                    </a>
                    <?php endif; ?>

                    <?php if ($this->auth_lib->has_permission('partner_manage')): ?>
                    <a href="<?= site_url('statements/cash_advance'); ?>" class="btn btn-outline-warning text-start">
                        <i class="bi bi-cash-stack"></i> Cash advance
                    </a>
                    <?php endif; ?>

                    <?php if ($this->auth_lib->has_permission('cashbox_view')): ?>
                    <a href="<?= site_url('cashboxes'); ?>" class="btn btn-outline-secondary text-start">
                        <i class="bi bi-safe2"></i> Position trésorerie
                    </a>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>
