<?php
// Determine current page for active link
$current_page = basename($_SERVER['PHP_SELF']);
$user_role = isset($_SESSION['role']) ? $_SESSION['role'] : 'staff';
?>

<div class="sidebar">
    <div class="sidebar-brand">
        <h5><i class="fas fa-money-bill-wave"></i> Cash Lending</h5>
        <small><?php echo htmlspecialchars($company_name); ?></small>
    </div>

    <ul class="nav flex-column">

        <?php if ($user_role === 'admin'): ?>
        <li class="nav-item">
            <a class="nav-link <?php echo ($current_page === 'dashboard.php') ? 'active' : ''; ?>" 
               href="/cash_lending/dashboard.php">
                <i class="fas fa-chart-line"></i> Dashboard
            </a>
        </li>
        <?php endif; ?>

        <li class="nav-item">
            <a class="nav-link <?php echo ($current_page === 'borrowers.php' || $current_page === 'add-borrower.php' || $current_page === 'edit-borrower.php') ? 'active' : ''; ?>" 
               href="/cash_lending/borrowers.php">
                <i class="fas fa-users"></i> Borrowers
            </a>
        </li>

        <li class="nav-item">
            <a class="nav-link <?php echo ($current_page === 'loans.php' || $current_page === 'add-loan.php' || $current_page === 'edit-loan.php') ? 'active' : ''; ?>" 
               href="/cash_lending/loans.php">
                <i class="fas fa-file-contract"></i> Loans
            </a>
        </li>

        <?php if ($user_role === 'admin'): ?>
        <li class="nav-item">
            <a class="nav-link <?php echo ($current_page === 'payments.php' || $current_page === 'add-payment.php') ? 'active' : ''; ?>" 
               href="/cash_lending/payments.php">
                <i class="fas fa-credit-card"></i> Payments
            </a>
        </li>

        <li class="nav-item">
            <a class="nav-link <?php echo ($current_page === 'reports.php' || strpos($current_page, 'report') !== false) ? 'active' : ''; ?>" 
               href="/cash_lending/reports.php">
                <i class="fas fa-file-alt"></i> Reports
            </a>
        </li>

        <li class="nav-item">
            <a class="nav-link <?php echo ($current_page === 'users.php' || $current_page === 'add-user.php' || $current_page === 'edit-user.php') ? 'active' : ''; ?>" 
               href="/cash_lending/users.php">
                <i class="fas fa-cog"></i> Users
            </a>
        </li>

        <li class="nav-item">
            <a class="nav-link <?php echo ($current_page === 'settings.php') ? 'active' : ''; ?>" 
               href="/cash_lending/settings.php">
                <i class="fas fa-sliders-h"></i> Settings
            </a>
        </li>

        <li class="nav-item">
            <a class="nav-link <?php echo basename($_SERVER['PHP_SELF'])==='activity-log.php'?'active':''; ?>"
               href="/cash_lending/activity-log.php">
                <i class="fas fa-history"></i> Activity Log
            </a>
        </li>
        <?php endif; ?>

        <hr style="background-color: rgba(255, 255, 255, 0.2); margin: 15px 0;">

        <li class="nav-item">
            <a class="nav-link" href="/cash_lending/logout.php">
                <i class="fas fa-sign-out-alt"></i> Logout
            </a>
        </li>
    </ul>
</div>

<div class="topbar">
    <div class="d-flex align-items-center">
        <button class="btn btn-outline-secondary d-md-none me-3" id="sidebarToggle">
            <i class="fas fa-bars"></i>
        </button>
        <h4 style="margin: 0; color: #333;">
            <i class="fas fa-tachometer-alt"></i> 
            <?php 
            $page_titles = [
                'dashboard.php' => 'Dashboard',
                'borrowers.php' => 'Borrowers',
                'add-borrower.php' => 'Add Borrower',
                'edit-borrower.php' => 'Edit Borrower',
                'loans.php' => 'Loans',
                'add-loan.php' => 'Create Loan',
                'edit-loan.php' => 'Edit Loan',
                'payments.php' => 'Payments',
                'add-payment.php' => 'Record Payment',
                'reports.php' => 'Reports',
                'users.php' => 'Users',
                'add-user.php' => 'Add User',
                'edit-user.php' => 'Edit User',
                'settings.php' => 'Settings'
            ];
            echo $page_titles[$current_page] ?? 'Cash Lending System';
            ?>
        </h4>
    </div>

    <div class="user-menu">
        <div class="user-info">
            <strong><?php echo htmlspecialchars($_SESSION['full_name']); ?></strong>
            <small><?php echo ucfirst($_SESSION['role']); ?></small>
        </div>
        <div class="dropdown">
            <button class="btn btn-sm btn-outline-secondary dropdown-toggle" type="button" id="userDropdown" data-bs-toggle="dropdown">
                <i class="fas fa-user-circle"></i>
            </button>
            <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="userDropdown">
                <li><a class="dropdown-item" href="/cash_lending/profile.php"><i class="fas fa-user"></i> Profile</a></li>
                <li><hr class="dropdown-divider"></li>
                <li><a class="dropdown-item" href="/cash_lending/logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
            </ul>
        </div>
    </div>
</div>

<div class="content">
    <div class="main-content">
