<?php
if (!defined('DB_HOST')) {
    require_once __DIR__ . '/../config.php';
}
require_once __DIR__ . '/Auth.php';
Auth::requireLogin();
?>
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $pageTitle ?? APP_NAME; ?></title>
    <link rel="stylesheet" href="<?php echo APP_URL; ?>/assets/css/style.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: #f5f7fa;
            color: #333;
        }

        .wrapper {
            display: flex;
            min-height: 100vh;
        }

        /* Sidebar */
        .sidebar {
            width: 260px;
            background: linear-gradient(180deg, #2c3e50 0%, #34495e 100%);
            color: white;
            position: fixed;
            height: 100vh;
            overflow-y: auto;
            z-index: 1000;
            box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, width 0.3s ease;
        }

        .sidebar.collapsed {
            transform: translateX(-260px);
        }

        .sidebar.collapsed.pinned {
            transform: translateX(0);
            width: 60px;
        }

        .sidebar.collapsed.pinned .sidebar-header h2,
        .sidebar.collapsed.pinned .sidebar-header p,
        .sidebar.collapsed.pinned .menu-section-title,
        .sidebar.collapsed.pinned .menu-item-text,
        .sidebar.collapsed.pinned .user-details {
            display: none;
        }

        .sidebar.collapsed.pinned .sidebar-controls {
            position: static;
            justify-content: center;
            margin-top: 10px;
        }

        .sidebar.collapsed.pinned .menu-item {
            justify-content: center;
            padding: 12px 0;
        }

        .sidebar.collapsed.pinned .menu-item-icon {
            margin-right: 0;
        }

        .sidebar.collapsed.pinned .user-info {
            padding: 12px 10px;
            justify-content: center;
        }

        .sidebar-header {
            padding: 30px 20px;
            background: rgba(0, 0, 0, 0.2);
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            position: relative;
        }

        .sidebar-header h2 {
            font-size: 20px;
            font-weight: 700;
            margin-bottom: 5px;
        }

        .sidebar-header p {
            font-size: 13px;
            opacity: 0.8;
        }

        .sidebar-controls {
            position: absolute;
            top: 15px;
            right: 15px;
            display: flex;
            gap: 8px;
        }

        .sidebar-control-btn {
            width: 32px;
            height: 32px;
            border: none;
            background: rgba(255, 255, 255, 0.1);
            color: white;
            border-radius: 6px;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 16px;
            transition: all 0.3s;
        }

        .sidebar-control-btn:hover {
            background: rgba(255, 255, 255, 0.2);
            transform: scale(1.1);
        }

        .sidebar-control-btn.active {
            background: rgba(52, 152, 219, 0.5);
        }

        .sidebar-menu {
            padding: 20px 0;
        }

        .menu-section {
            margin-bottom: 30px;
        }

        .menu-section-title {
            padding: 10px 20px;
            font-size: 11px;
            text-transform: uppercase;
            letter-spacing: 1px;
            opacity: 0.5;
            font-weight: 600;
        }

        .menu-item {
            display: flex;
            align-items: center;
            padding: 12px 20px;
            color: white;
            text-decoration: none;
            transition: all 0.3s;
            border-left: 3px solid transparent;
        }

        .menu-item:hover {
            background: rgba(255, 255, 255, 0.1);
            border-left-color: #3498db;
        }

        .menu-item.active {
            background: rgba(52, 152, 219, 0.2);
            border-left-color: #3498db;
        }

        .menu-item-icon {
            width: 24px;
            margin-right: 12px;
            font-size: 18px;
            text-align: center;
        }

        .menu-item-text {
            font-size: 14px;
            font-weight: 500;
        }

        .sidebar-footer {
            padding: 20px;
            border-top: 1px solid rgba(255, 255, 255, 0.1);
            margin-top: auto;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 12px;
            background: rgba(0, 0, 0, 0.2);
            border-radius: 10px;
        }

        .user-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 18px;
            font-weight: 600;
        }

        .user-details {
            flex: 1;
        }

        .user-name {
            font-size: 14px;
            font-weight: 600;
            margin-bottom: 2px;
        }

        .user-role {
            font-size: 12px;
            opacity: 0.7;
        }

        /* Main Content */
        .main-content {
            flex: 1;
            margin-left: 260px;
            min-height: 100vh;
            transition: margin-left 0.3s ease;
        }

        .sidebar.collapsed ~ .main-content {
            margin-left: 0;
        }

        .sidebar.collapsed.pinned ~ .main-content {
            margin-left: 60px;
        }

        /* Top Bar */
        .topbar {
            background: white;
            padding: 20px 30px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            display: flex;
            align-items: center;
            justify-content: space-between;
            position: sticky;
            top: 0;
            z-index: 100;
        }

        .topbar-left {
            display: flex;
            align-items: center;
            gap: 20px;
        }

        .topbar-left h1 {
            font-size: 24px;
            font-weight: 700;
            color: #2c3e50;
        }

        .sidebar-toggle-btn {
            width: 40px;
            height: 40px;
            border: none;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 8px;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 20px;
            transition: all 0.3s;
            box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
        }

        .sidebar-toggle-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
        }

        .topbar-right {
            display: flex;
            align-items: center;
            gap: 20px;
        }

        .btn {
            padding: 10px 20px;
            border-radius: 8px;
            text-decoration: none;
            font-size: 14px;
            font-weight: 600;
            transition: all 0.3s;
            border: none;
            cursor: pointer;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }

        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
        }

        .btn-danger {
            background: #e74c3c;
            color: white;
        }

        .btn-danger:hover {
            background: #c0392b;
        }

        .content-area {
            padding: 30px;
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <!-- Sidebar -->
        <aside class="sidebar" id="sidebar">
            <div class="sidebar-header">
                <h2>📞 Destek Sistemi</h2>
                <p>Performans Takip</p>
                <div class="sidebar-controls">
                    <button class="sidebar-control-btn" id="pinBtn" title="Sabitle/Sabitlemeyi Kaldır">
                        📌
                    </button>
                </div>
            </div>

            <nav class="sidebar-menu">
                <div class="menu-section">
                    <div class="menu-section-title">Ana Menü</div>
                    <a href="<?php echo APP_URL; ?>/index.php" class="menu-item <?php echo (basename($_SERVER['PHP_SELF']) == 'index.php') ? 'active' : ''; ?>">
                        <span class="menu-item-icon">📊</span>
                        <span class="menu-item-text">Dashboard</span>
                    </a>
                </div>

                <?php if (Auth::isAdmin()): ?>
                <div class="menu-section">
                    <div class="menu-section-title">Yönetim</div>
                    <a href="<?php echo APP_URL; ?>/settings.php" class="menu-item <?php echo (basename($_SERVER['PHP_SELF']) == 'settings.php') ? 'active' : ''; ?>">
                        <span class="menu-item-icon">⚙️</span>
                        <span class="menu-item-text">Ayarlar</span>
                    </a>
                </div>

                <div class="menu-section">
                    <div class="menu-section-title">Raporlar</div>
                    <a href="<?php echo APP_URL; ?>/incoming_calls.php" class="menu-item <?php echo (basename($_SERVER['PHP_SELF']) == 'incoming_calls.php') ? 'active' : ''; ?>">
                        <span class="menu-item-icon">📊</span>
                        <span class="menu-item-text">Gelen Arama Analizi</span>
                    </a>
                    <a href="<?php echo APP_URL; ?>/outgoing_calls.php" class="menu-item <?php echo (basename($_SERVER['PHP_SELF']) == 'outgoing_calls.php') ? 'active' : ''; ?>">
                        <span class="menu-item-icon">📈</span>
                        <span class="menu-item-text">Giden Arama Analizi</span>
                    </a>
                    <a href="<?php echo APP_URL; ?>/training_completion.php" class="menu-item <?php echo (basename($_SERVER['PHP_SELF']) == 'training_completion.php') ? 'active' : ''; ?>">
                        <span class="menu-item-icon">🎓</span>
                        <span class="menu-item-text">Eğitim ve Kurulum Analizi</span>
                    </a>
                    <a href="<?php echo APP_URL; ?>/bonus.php" class="menu-item <?php echo (basename($_SERVER['PHP_SELF']) == 'bonus.php') ? 'active' : ''; ?>">
                        <span class="menu-item-icon">💰</span>
                        <span class="menu-item-text">Prim Hesaplama</span>
                    </a>
                    <a href="<?php echo APP_URL; ?>/admin_reports.php" class="menu-item <?php echo (basename($_SERVER['PHP_SELF']) == 'admin_reports.php') ? 'active' : ''; ?>">
                        <span class="menu-item-icon">📋</span>
                        <span class="menu-item-text">Yönetici Raporları</span>
                    </a>
                    <a href="<?php echo APP_URL; ?>/support_messages.php" class="menu-item <?php echo (basename($_SERVER['PHP_SELF']) == 'support_messages.php') ? 'active' : ''; ?>">
                        <span class="menu-item-icon">💬</span>
                        <span class="menu-item-text">Destek Mesajı Raporları</span>
                    </a>
                </div>

                <div class="menu-section">
                    <div class="menu-section-title">Veri Yönetimi</div>
                    <a href="<?php echo APP_URL; ?>/upload_incoming.php" class="menu-item <?php echo (basename($_SERVER['PHP_SELF']) == 'upload_incoming.php') ? 'active' : ''; ?>">
                        <span class="menu-item-icon">📥</span>
                        <span class="menu-item-text">Gelen Arama Yükle</span>
                    </a>
                    <a href="<?php echo APP_URL; ?>/upload_outgoing.php" class="menu-item <?php echo (basename($_SERVER['PHP_SELF']) == 'upload_outgoing.php') ? 'active' : ''; ?>">
                        <span class="menu-item-icon">📤</span>
                        <span class="menu-item-text">Giden Arama Yükle</span>
                    </a>
                    <a href="<?php echo APP_URL; ?>/upload_training.php" class="menu-item <?php echo (basename($_SERVER['PHP_SELF']) == 'upload_training.php') ? 'active' : ''; ?>">
                        <span class="menu-item-icon">🎓</span>
                        <span class="menu-item-text">Eğitim Tamamlama Verisi Yükle</span>
                    </a>
                </div>
                <?php else: ?>
                <!-- Personel Menüsü -->
                <div class="menu-section">
                    <div class="menu-section-title">Hesabım</div>
                    <a href="<?php echo APP_URL; ?>/accounts.php" class="menu-item <?php echo (basename($_SERVER['PHP_SELF']) == 'accounts.php') ? 'active' : ''; ?>">
                        <span class="menu-item-icon">👤</span>
                        <span class="menu-item-text">Hesap Ayarlarım</span>
                    </a>
                </div>

                <div class="menu-section">
                    <div class="menu-section-title">Raporlar</div>
                    <a href="<?php echo APP_URL; ?>/incoming_calls.php" class="menu-item <?php echo (basename($_SERVER['PHP_SELF']) == 'incoming_calls.php') ? 'active' : ''; ?>">
                        <span class="menu-item-icon">📊</span>
                        <span class="menu-item-text">Gelen Arama Analizi</span>
                    </a>
                    <a href="<?php echo APP_URL; ?>/outgoing_calls.php" class="menu-item <?php echo (basename($_SERVER['PHP_SELF']) == 'outgoing_calls.php') ? 'active' : ''; ?>">
                        <span class="menu-item-icon">📈</span>
                        <span class="menu-item-text">Giden Arama Analizi</span>
                    </a>
                    <a href="<?php echo APP_URL; ?>/training_completion.php" class="menu-item <?php echo (basename($_SERVER['PHP_SELF']) == 'training_completion.php') ? 'active' : ''; ?>">
                        <span class="menu-item-icon">🎓</span>
                        <span class="menu-item-text">Eğitim ve Kurulum Analizi</span>
                    </a>
                    <a href="<?php echo APP_URL; ?>/bonus.php" class="menu-item <?php echo (basename($_SERVER['PHP_SELF']) == 'bonus.php') ? 'active' : ''; ?>">
                        <span class="menu-item-icon">💰</span>
                        <span class="menu-item-text">Prim Hesaplama</span>
                    </a>
                </div>
                <?php endif; ?>
            </nav>

            <div class="sidebar-footer">
                <div class="user-info">
                    <div class="user-avatar">
                        <?php echo strtoupper(substr(Auth::getFullName(), 0, 1)); ?>
                    </div>
                    <div class="user-details">
                        <div class="user-name"><?php echo htmlspecialchars(Auth::getFullName()); ?></div>
                        <div class="user-role"><?php echo Auth::isAdmin() ? 'Yönetici' : 'Personel'; ?></div>
                    </div>
                </div>
            </div>
        </aside>

        <!-- Main Content -->
        <main class="main-content">
            <div class="topbar">
                <div class="topbar-left">
                    <button class="sidebar-toggle-btn" id="sidebarToggle" title="Menüyü Aç/Kapat">
                        ☰
                    </button>
                    <h1><?php echo $pageTitle ?? 'Dashboard'; ?></h1>
                </div>
                <div class="topbar-right">
                    <a href="<?php echo APP_URL; ?>/logout.php" class="btn btn-danger">Çıkış Yap</a>
                </div>
            </div>

            <div class="content-area">
