<div class="row mb-4">
    <div class="col-12">
        <a href="/dashboard.php?page=casas" class="btn btn-outline-secondary mb-2">
            <i class="bi bi-arrow-left"></i> Volver a Casas
        </a>
        <h2><i class="bi bi-building"></i> Casa <?= $house['number'] ?></h2>
        <p class="text-muted">Vista unificada de pagos y finanzas</p>
    </div>
</div>

<div class="row mb-4">
    <div class="col-md-3">
        <div class="card">
            <div class="card-header">Información</div>
            <div class="card-body">
                <p><strong>Número:</strong> <?= $house['number'] ?></p>
                <p><strong>Estado:</strong> 
                    <span class="badge <?= $house['status'] == 'activa' ? 'bg-success' : 'bg-secondary' ?>">
                        <?= $house['status'] == 'activa' ? 'Activa' : 'Deshabitada' ?>
                    </span>
                </p>
                <p><strong>Consumo Only:</strong> 
                    <?= $house['consumption_only'] ? '<span class="badge bg-warning">Sí</span>' : 'No' ?>
                </p>
                <p><strong>Propietario:</strong> <?= htmlspecialchars($house['owner_name'] ?? '-') ?></p>
                <p><strong>Email:</strong> <?= htmlspecialchars($house['owner_email'] ?? '-') ?></p>
                <p><strong>Teléfono:</strong> <?= htmlspecialchars($house['owner_phone'] ?? '-') ?></p>
            </div>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card border-success">
            <div class="card-body text-center">
                <h6 class="text-muted">Total Pagado Agua (Histórico)</h6>
                <h3 class="text-success">$<?= number_format($totalWaterPayments, 2) ?></h3>
            </div>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card border-info">
            <div class="card-body text-center">
                <h6 class="text-muted">Total Conceptos</h6>
                <h3 class="text-info">$<?= number_format($totalConceptPayments, 2) ?></h3>
            </div>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card border-primary">
            <div class="card-body text-center">
                <h6 class="text-muted">Total General</h6>
                <h3 class="text-primary">$<?= number_format($totalWaterPayments + $totalConceptPayments, 2) ?></h3>
            </div>
        </div>
    </div>
</div>

<ul class="nav nav-tabs mb-4" id="houseTabs">
    <li class="nav-item">
        <button class="nav-link active" data-bs-toggle="tab" data-bs-target="#waterPayments">
            <i class="bi bi-droplet-fill"></i> Pagos de Agua
        </button>
    </li>
    <li class="nav-item">
        <button class="nav-link" data-bs-toggle="tab" data-bs-target="#conceptPayments">
            <i class="bi bi-collection"></i> Conceptos Especiales
        </button>
    </li>
    <li class="nav-item">
        <button class="nav-link" data-bs-toggle="tab" data-bs-target="#activityLog">
            <i class="bi bi-clock-history"></i> Historial
        </button>
    </li>
</ul>

<div class="tab-content">
    <div class="tab-pane fade show active" id="waterPayments">
        <div class="card">
            <div class="card-body">
                <table class="table table-sm">
                    <thead>
                        <tr>
                            <th>Mes</th>
                            <th>Año</th>
                            <th>Monto</th>
                            <th>Fecha de Pago</th>
                            <th>Método</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($waterPayments as $payment): 
                            $expected = Payment::getExpectedAmount($house, $payment['year'], $payment['month']);
                            $statusClass = $payment['amount'] >= $expected ? 'paid' : 'pending';
                        ?>
                            <tr>
                                <td><?= $payment['month'] ?></td>
                                <td><?= $payment['year'] ?></td>
                                <td class="<?= $statusClass ?>">$<?= number_format($payment['amount'], 2) ?></td>
                                <td><?= $payment['payment_date'] ? date('d/m/Y', strtotime($payment['payment_date'])) : '-' ?></td>
                                <td><?= htmlspecialchars($payment['payment_method'] ?? '-') ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="tab-pane fade" id="conceptPayments">
        <div class="card">
            <div class="card-body">
                <table class="table table-sm">
                    <thead>
                        <tr>
                            <th>Concepto</th>
                            <th>Monto</th>
                            <th>Fecha de Pago</th>
                            <th>Notas</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($conceptPayments as $payment): ?>
                            <tr>
                                <td><?= htmlspecialchars($payment['concept_name']) ?></td>
                                <td>$<?= number_format($payment['amount'], 2) ?></td>
                                <td><?= $payment['payment_date'] ? date('d/m/Y', strtotime($payment['payment_date'])) : '-' ?></td>
                                <td><?= htmlspecialchars($payment['notes'] ?? '-') ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="tab-pane fade" id="activityLog">
        <div class="card">
            <div class="card-body">
                <table class="table table-sm">
                    <thead>
                        <tr>
                            <th>Fecha</th>
                            <th>Acción</th>
                            <th>Detalles</th>
                            <th>Usuario</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($activityLogs as $log): ?>
                            <tr>
                                <td><?= date('d/m/Y H:i', strtotime($log['created_at'])) ?></td>
                                <td><?= htmlspecialchars($log['action']) ?></td>
                                <td><?= htmlspecialchars($log['details'] ?? '-') ?></td>
                                <td><?= htmlspecialchars($log['username'] ?? '-') ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
