<!DOCTYPE html>
<html lang="{{ current_language }}">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ _('dashboard') }} - iAccess Terminal Yönetimi</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #667eea;
            --secondary-color: #764ba2;
            --success-color: #28a745;
            --warning-color: #ffc107;
            --danger-color: #dc3545;
            --info-color: #17a2b8;
            --dark-color: #343a40;
            --light-color: #f8f9fa;
        }

        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        .navbar {
            background: rgba(255, 255, 255, 0.95) !important;
            backdrop-filter: blur(10px);
            box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
        }

        .navbar-brand {
            font-weight: 700;
            color: var(--primary-color) !important;
        }

        .nav-link {
            color: var(--dark-color) !important;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .nav-link:hover {
            color: var(--primary-color) !important;
            transform: translateY(-1px);
        }

        .main-container {
            margin-top: 80px;
            padding: 20px;
        }

        .dashboard-card {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
            transition: all 0.3s ease;
        }

        .dashboard-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
        }

        .stat-card {
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            color: white;
            border-radius: 12px;
            padding: 20px;
            text-align: center;
            transition: all 0.3s ease;
        }

        .stat-card:hover {
            transform: scale(1.03);
        }

        .stat-icon {
            font-size: 2.5rem;
            margin-bottom: 12px;
            opacity: 0.8;
        }

        .stat-number {
            font-size: 2rem;
            font-weight: 700;
            margin-bottom: 4px;
        }

        .stat-label {
            font-size: 1rem;
            opacity: 0.9;
        }
        
        .last-activity-info {
            padding: 0.5rem 0;
            border-top: 1px solid rgba(255, 255, 255, 0.2);
            margin-top: 0.5rem;
        }
        
        .last-activity-info .text-white {
            font-size: 0.9rem;
        }
        
        .last-activity-info .text-white-75 {
            font-size: 0.85rem;
        }
        
        .last-activity-info .text-white-50 {
            font-size: 0.8rem;
        }

        .table-container {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }

        .table {
            margin-bottom: 0;
        }

        .table thead th {
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            color: white;
            border: none;
            font-weight: 600;
            padding: 15px;
        }

        .table tbody tr {
            transition: all 0.3s ease;
        }

        .table tbody tr:hover {
            background-color: rgba(102, 126, 234, 0.1);
            transform: scale(1.01);
        }

        .btn-modern {
            border-radius: 10px;
            padding: 10px 20px;
            font-weight: 600;
            transition: all 0.3s ease;
            border: none;
        }

        .btn-modern:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }

        .search-box {
            background: rgba(255, 255, 255, 0.9);
            border-radius: 25px;
            border: 2px solid transparent;
            padding: 12px 20px;
            transition: all 0.3s ease;
        }

        .search-box:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.25);
        }

        .status-badge {
            padding: 5px 12px;
            border-radius: 20px;
            font-size: 0.85rem;
            font-weight: 600;
        }

        .status-success {
            background: rgba(40, 167, 69, 0.1);
            color: var(--success-color);
        }

        .status-warning {
            background: rgba(255, 193, 7, 0.1);
            color: var(--warning-color);
        }

        .status-danger {
            background: rgba(220, 53, 69, 0.1);
            color: var(--danger-color);
        }

        .welcome-section {
            background: linear-gradient(135deg, rgba(255, 255, 255, 0.9), rgba(255, 255, 255, 0.7));
            border-radius: 15px;
            padding: 20px;
            margin-bottom: 25px;
            text-align: center;
        }

        .welcome-title {
            font-size: 1.8rem;
            font-weight: 700;
            color: var(--dark-color);
            margin-bottom: 8px;
        }

        .welcome-company {
            font-size: 1.1rem;
            color: var(--primary-color);
            font-weight: 600;
            margin-bottom: 8px;
        }

        .welcome-subtitle {
            font-size: 1rem;
            color: #6c757d;
        }

        /* Access Logs Table Styles */
        .table-modern {
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
        }
        
        .table-modern thead th {
            background: linear-gradient(135deg, var(--primary-color), #357abd);
            color: white;
            border: none;
            padding: 1rem;
            font-weight: 600;
            text-transform: uppercase;
            font-size: 0.9rem;
            letter-spacing: 0.5px;
        }
        
        .table-modern tbody tr {
            transition: all 0.3s ease;
        }
        
        .table-modern tbody tr:hover {
            background-color: rgba(74, 144, 226, 0.1);
            transform: scale(1.01);
        }
        
        .table-modern tbody td {
            padding: 1rem;
            border: none;
            border-bottom: 1px solid #e9ecef;
            vertical-align: middle;
        }
        
        .badge {
            padding: 0.5rem 1rem;
            border-radius: 20px;
            font-weight: 600;
            font-size: 0.8rem;
        }
        
        .badge-success {
            background: linear-gradient(135deg, var(--success-color), #1e7e34);
            color: white;
        }
        
        .badge-danger {
            background: linear-gradient(135deg, var(--danger-color), #c82333);
            color: white;
        }
        
        .badge-warning {
            background: linear-gradient(135deg, var(--warning-color), #e0a800);
            color: #212529;
        }
        
        .badge-info {
            background: linear-gradient(135deg, var(--info-color), #138496);
            color: white;
        }
        
        .badge-secondary {
            background: linear-gradient(135deg, #6c757d, #5a6268);
            color: white;
        }
        
        .access-type {
            font-weight: 600;
            padding: 0.25rem 0.75rem;
            border-radius: 15px;
            font-size: 0.8rem;
            display: inline-block;
            vertical-align: middle;
        }
        
        .access-type-container {
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }
        
        .access-nfc { background-color: #e3f2fd; color: #1976d2; }
        .access-button { background-color: #f3e5f5; color: #7b1fa2; }
        .access-tcpserver { background-color: #e8f5e8; color: #388e3c; }
        .access-tcpclient { background-color: #fff3e0; color: #f57c00; }
        .access-ble { background-color: #fce4ec; color: #c2185b; }
        .access-rs485 { background-color: #f1f8e9; color: #689f38; }
        .access-unknown { background-color: #f5f5f5; color: #757575; }
        
        .timestamp {
            font-family: 'Courier New', monospace;
            font-size: 0.9rem;
            color: #6c757d;
        }
        
        .user-info {
            font-weight: 600;
            color: var(--primary-color);
        }
        
        .device-id {
            font-family: 'Courier New', monospace;
            font-weight: 600;
            color: #495057;
        }
        
        .device-info {
            font-weight: 600;
            color: var(--primary-color);
        }

        @media (max-width: 768px) {
            .main-container {
                margin-top: 60px;
                padding: 10px;
            }
            
            .welcome-title {
                font-size: 1.5rem;
            }
            
            .stat-number {
                font-size: 1.5rem;
            }
            
            .stat-icon {
                font-size: 2rem;
            }
        }
    </style>
</head>
<body>
    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg fixed-top">
        <div class="container">
            <a class="navbar-brand" href="https://monicont.com" target="_blank">
                <img src="{{ url_for('static', filename='img/monicont_logo.png') }}" alt="Logo" style="width: 120px; height: auto;" class="me-3">
            </a>
            
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('home') }}">
                            <i class="fas fa-home me-1"></i>{{ _('dashboard') }}
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('devices') }}">
                            <i class="fas fa-microchip me-1"></i>{{ _('devices') }}
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('users') }}">
                            <i class="fas fa-users me-1"></i>{{ _('users_title') }}
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('access_logs') }}">
                            <i class="fas fa-clipboard-list me-1"></i>{{ _('access_logs_title') }}
                        </a>
                    </li>
                </ul>
                
                <ul class="navbar-nav">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle d-flex align-items-center" href="#" id="userDropdown" role="button" data-bs-toggle="dropdown">
                            {% if user and user.profile_image %}
                                {% if user.profile_image.startswith('@') %}
                                  {% set imgfile = user.profile_image[1:] %}
                                  <img src="{{ url_for('static', filename='uploads/' + imgfile) }}" 
                                {% else %}
                                  <img src="{{ url_for('static', filename='uploads/' + user.profile_image) }}" 
                                {% endif %}
                                      alt="Profil Resmi" class="rounded-circle me-2" width="32" height="32" style="object-fit: cover;">
                            {% else %}
                                <img src="{{ url_for('static', filename='img/default-avatar.png') }}" 
                                    alt="Profil Resmi" class="rounded-circle me-2" width="32" height="32" style="object-fit: cover;">
                            {% endif %}
                            <div class="d-flex flex-column">
                                <span class="fw-bold">{{ user_name }}</span>
                                {% if user and user.company %}
                                    <small class="text-muted">{{ user.company }}</small>
                                {% endif %}
                            </div>
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="userDropdown">
                            <li><a class="dropdown-item" href="{{ url_for('profile') }}"><i class="fas fa-user me-2"></i>{{ _('profile') }}</a></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" href="{{ url_for('logout') }}"><i class="fas fa-sign-out-alt me-2"></i>{{ _('logout') }}</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- Main Content -->
    <div class="main-container">
        <div class="container-fluid">
            <!-- Welcome Section -->
            <div class="welcome-section">
                <h1 class="welcome-title">{{ _('welcome_message') }}, {{ user_name }}!</h1>
                {% if user and user.company %}
                    <p class="welcome-company">{{ user.company }}</p>
                {% endif %}
                <p class="welcome-subtitle">{{ _('dashboard') }} - {{ _('access_logs') }}</p>
                {% if last_login %}
                    <p class="welcome-subtitle"><i class="fas fa-user-clock me-2"></i>{{ _('last_access') }}: {{ last_login }}</p>
                {% else %}
                    <p class="welcome-subtitle"><i class="fas fa-user-clock me-2"></i>{{ _('last_access') }}: N/A</p>
                {% endif %}
            </div>

            <!-- Statistics Cards -->
            <div class="row mb-4">
                <div class="col-md-3 mb-3">
                    <div class="stat-card">
                        <div class="stat-icon">
                            <i class="fas fa-microchip"></i>
                        </div>
                        <div class="stat-number">{{ total_devices|default(0) }}</div>
                        <div class="stat-label">{{ _('total_devices') }}</div>
                    </div>
                </div>
                <div class="col-md-3 mb-3">
                    <div class="stat-card">
                        <div class="stat-icon">
                            <i class="fas fa-door-open"></i>
                        </div>
                        <div class="stat-number">{{ total_access|default(0) }}</div>
                        <div class="stat-label">{{ _('total_access') }}</div>
                        <div class="mt-2 small d-flex align-items-center gap-3 justify-content-center">
                            <span class="badge bg-success"><i class="bi bi-check-circle me-1"></i>{{ granted_count|default(0) }}</span>
                            <span class="text-white-75">{{ _('granted_label') }}</span>
                            <span class="badge bg-danger"><i class="bi bi-x-circle me-1"></i>{{ denied_count|default(0) }}</span>
                            <span class="text-white-75">{{ _('denied_label') }}</span>
                        </div>
                    </div>
                </div>
                <div class="col-md-3 mb-3">
                    <div class="stat-card">
                        <div class="stat-icon">
                            <i class="fas fa-clock"></i>
                        </div>
                        {% if gecisler and gecisler|length > 0 %}
                            {% set last_activity = gecisler[0] %}
                            <div class="stat-label">{{ _('recent_activity') }}</div>
                            <div class="last-activity-info">
                                <div class="d-flex align-items-center justify-content-center mb-1">
                                    {% set access_types = {
                                        1: ('NFC', 'access-nfc'),
                                        2: ('BUTTON', 'access-button'),
                                        3: ('TCP SERVER', 'access-tcpserver'),
                                        4: ('TCP CLIENT', 'access-tcpclient'),
                                        5: ('BLE', 'access-ble'),
                                        6: ('RS485', 'access-rs485')
                                    } %}
                                    {% set type_info = access_types.get(last_activity.access_type|int, ('UNKNOWN', 'access-unknown')) %}
                                    {% if last_activity.access_type == 1 or last_activity.access_type == '1' %}
                                        <img src="{{ url_for('static', filename='img/access_nfc.png') }}" alt="NFC" width="20" height="20" class="me-2">
                                    {% elif last_activity.access_type == 2 or last_activity.access_type == '2' %}
                                        <img src="{{ url_for('static', filename='img/access_button.png') }}" alt="BUTTON" width="20" height="20" class="me-2">
                                    {% elif last_activity.access_type == 3 or last_activity.access_type == '3' %}
                                        <img src="{{ url_for('static', filename='img/access_tcpserver.png') }}" alt="TCP SERVER" width="20" height="20" class="me-2">
                                    {% elif last_activity.access_type == 4 or last_activity.access_type == '4' %}
                                        <img src="{{ url_for('static', filename='img/access_tcpclient.png') }}" alt="TCP CLIENT" width="20" height="20" class="me-2">
                                    {% elif last_activity.access_type == 5 or last_activity.access_type == '5' %}
                                        <img src="{{ url_for('static', filename='img/access_ble.png') }}" alt="BLE" width="20" height="20" class="me-2">
                                    {% elif last_activity.access_type == 6 or last_activity.access_type == '6' %}
                                        <img src="{{ url_for('static', filename='img/access_rs485.png') }}" alt="RS485" width="20" height="20" class="me-2">
                                    {% else %}
                                        <i class="bi bi-question-circle me-2 text-white"></i>
                                    {% endif %}
                                    <span class="text-white fw-bold">{{ type_info[0] }}</span>
                                </div>
                                <div class="text-white-75 fw-semibold">{{ last_activity.user_name or last_activity.card_uuid }}</div>
                                <div class="text-white-50 small">{{ last_activity.notes or '' }}</div>
                                <div class="text-white-50 small">
                                    {% if last_activity.source == 'new' %}
                                        {{ last_activity.timestamp|datetime }}
                                    {% else %}
                                        {{ last_activity.timestamp }}
                                    {% endif %}
                                </div>
                            </div>
                        {% else %}
                            <div class="stat-label">{{ _('recent_activity') }}</div>
                            <div class="last-activity-info">
                                <div class="text-white-50">{{ _('no_transitions_yet') }}</div>
                            </div>
                        {% endif %}
                    </div>
                </div>
                <div class="col-md-3 mb-3">
                    <div class="stat-card">
                        <div class="stat-icon">
                            <i class="fas fa-chart-line"></i>
                        </div>
                        <div class="stat-number">{{ active_devices|default(0) }}</div>
                        <div class="stat-label">{{ _('active_devices') }}</div>
                    </div>
                </div>
            </div>

            <!-- Search and Actions -->
            <div class="row mb-4">
                <div class="col-md-8">
                    <form method="GET" class="d-flex">
                        <input type="text" name="adsoyad" class="form-control search-box me-2" 
                               placeholder="{{ _('filter_placeholder') }}" 
                               value="{{ request.args.get('adsoyad', '') }}">
                        <button type="submit" class="btn btn-modern btn-primary" formaction="{{ url_for('home') }}#logs">
                            <i class="fas fa-search me-1"></i>{{ _('filter') }}
                        </button>
                    </form>
                </div>
                <div class="col-md-4 text-end">
                    <a href="{{ url_for('download_pdf') }}?{{ request.query_string.decode() }}" 
                       class="btn btn-modern btn-success me-2">
                        <i class="fas fa-download me-1"></i>{{ _('download_pdf') }}
                    </a>
                    <a href="{{ url_for('cihaz_ekle') }}" class="btn btn-modern btn-primary">
                        <i class="fas fa-plus me-1"></i>{{ _('device_add') }}
                    </a>
                </div>
            </div>

            <!-- Data Table -->
            <div class="table-container" id="logs">
                {% set logs = gecisler %}
                {% include 'partials/access_logs_table.html' %}
            </div>
            
        </div>
    </div>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
    
    <!-- Custom Scripts -->
    <script>
        // Auto-hide alerts after 5 seconds
        setTimeout(function() {
            const alerts = document.querySelectorAll('.alert');
            alerts.forEach(function(alert) {
                const bsAlert = new bootstrap.Alert(alert);
                bsAlert.close();
            });
        }, 5000);

        // Smooth scrolling
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });

        // Real-time güncelleme
        let lastKnownLogId = 0;
        // İlk açılışta son id'yi çek
        fetch('/api/check-updates?last_id=0')
          .then(r=>r.json())
          .then(d=>{ lastKnownLogId = d.latest_id || 0; })
          .catch(()=>{});
        
        function checkForUpdates() {
            fetch('/api/check-updates?last_id=' + lastKnownLogId)
                .then(response => response.json())
                .then(data => {
                    if (data.has_updates) {
                        location.reload();
                    } else if (typeof data.latest_id !== 'undefined') {
                        lastKnownLogId = data.latest_id;
                    }
                })
                .catch(() => {});
        }

        // 5 sn'de bir hafif kontrol (yalnızca yeni kayıt varsa yenile)
        setInterval(checkForUpdates, 5000);
    </script>
</body>
</html> 