<!DOCTYPE html>
<html lang="{{ current_language }}">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <title>Cihazlarım - iAccess Mobile</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;
            --danger-color: #dc3545;
            --warning-color: #ffc107;
            --info-color: #17a2b8;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
            min-height: 100vh;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            overflow-x: hidden;
        }

        .mobile-header {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            padding: 15px 20px;
            box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
            position: sticky;
            top: 0;
            z-index: 100;
        }

        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .user-info h5 {
            color: var(--primary-color);
            font-weight: 700;
            margin: 0;
            font-size: 1.1rem;
        }

        .user-info small {
            color: #666;
            font-size: 0.8rem;
        }

        .logout-btn {
            background: linear-gradient(135deg, var(--danger-color), #c82333);
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 10px;
            font-size: 0.9rem;
            transition: all 0.3s ease;
        }

        .logout-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(220, 53, 69, 0.4);
        }

        .devices-container {
            padding: 20px;
            min-height: calc(100vh - 80px);
        }

        .devices-grid {
            display: grid;
            grid-template-columns: 1fr;
            gap: 20px;
            max-width: 500px;
            margin: 0 auto;
        }

        .device-card {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            padding: 25px;
            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;
            position: relative;
            overflow: hidden;
        }

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

        .device-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        .device-info h6 {
            color: var(--primary-color);
            font-weight: 700;
            margin: 0;
            font-size: 1.1rem;
        }

        .device-info small {
            color: #666;
            font-size: 0.8rem;
        }

        .status-indicator {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            display: inline-block;
        }

        .status-connected {
            background-color: var(--success-color);
            animation: pulse 2s infinite;
        }

        .status-disconnected {
            background-color: var(--danger-color);
        }

        @keyframes pulse {
            0% { opacity: 1; }
            50% { opacity: 0.7; }
            100% { opacity: 1; }
        }

        .device-actions {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 12px;
        }

        .action-btn {
            flex: 1;
            padding: 16px;
            min-height: 100px;
            border: none;
            border-radius: 14px;
            font-weight: 600;
            font-size: 0.95rem;
            transition: all 0.3s ease;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            gap: 6px;
            text-align: center;
        }

        .action-btn .action-icon {
            line-height: 1;
        }

        .action-btn .action-icon i {
            font-size: 28px;
        }

        .action-btn .action-label {
            font-size: 0.95rem;
            font-weight: 700;
        }

        .btn-open {
            background: linear-gradient(135deg, var(--success-color), #20c997);
            color: white;
        }

        .btn-open:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 15px rgba(40, 167, 69, 0.4);
        }

        .btn-logs {
            background: linear-gradient(135deg, var(--info-color), #138496);
            color: white;
        }

        .btn-logs:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 15px rgba(23, 162, 184, 0.4);
        }

        .device-details {
            background: rgba(108, 117, 125, 0.1);
            border-radius: 12px;
            padding: 15px;
            margin-bottom: 20px;
        }

        .detail-row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 8px;
        }

        .detail-row:last-child {
            margin-bottom: 0;
        }

        .detail-label {
            color: #666;
            font-size: 0.85rem;
            font-weight: 600;
        }

        .detail-value {
            color: #333;
            font-size: 0.85rem;
            font-family: monospace;
        }

        .no-devices {
            text-align: center;
            color: rgba(255, 255, 255, 0.8);
            padding: 40px 20px;
        }

        .no-devices i {
            font-size: 4rem;
            margin-bottom: 20px;
            opacity: 0.5;
        }

        .loading-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.7);
            display: none;
            justify-content: center;
            align-items: center;
            z-index: 9999;
        }

        .loading-overlay.active {
            display: flex;
        }

        .loading-content {
            background: white;
            border-radius: 15px;
            padding: 30px;
            text-align: center;
            max-width: 300px;
        }

        .spinner-large {
            border: 4px solid rgba(102, 126, 234, 0.3);
            border-top: 4px solid var(--primary-color);
            border-radius: 50%;
            width: 40px;
            height: 40px;
            animation: spin 1s linear infinite;
            margin: 0 auto 15px;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        /* Swipe indicators */
        .swipe-hint {
            position: absolute;
            bottom: 10px;
            right: 15px;
            color: rgba(102, 126, 234, 0.3);
            font-size: 0.7rem;
            animation: fadeInOut 3s infinite;
        }

        @keyframes fadeInOut {
            0%, 50%, 100% { opacity: 0.3; }
            25%, 75% { opacity: 0.8; }
        }

        /* Touch feedback */
        .device-card.touching {
            transform: scale(0.98);
        }

        @media (min-width: 768px) {
            .devices-grid {
                grid-template-columns: repeat(2, 1fr);
                max-width: 800px;
            }
        }

        @media (min-width: 1200px) {
            .devices-grid {
                grid-template-columns: repeat(3, 1fr);
                max-width: 1200px;
            }
        }
    </style>
</head>
<body>
    <!-- Header -->
    <div class="mobile-header">
        <div class="header-content">
            <div class="user-info">
                <div class="d-flex align-items-center gap-2">
                    {% if user_profile_image_url %}
                        <img src="{{ user_profile_image_url }}" alt="Avatar" class="rounded-circle" width="28" height="28" style="object-fit:cover;">
                    {% endif %}
                    <h5 class="mb-0">{{ user_name }}</h5>
                </div>
                <small><i class="fas fa-mobile-alt me-1"></i>{{ _('mobile_access_title') }}</small>
            </div>
            <button class="logout-btn" onclick="logout()">
                <i class="fas fa-sign-out-alt me-1"></i>{{ _('logout') }}
            </button>
        </div>
    </div>

    <!-- Devices Container -->
    <div class="devices-container">
        {% if devices and devices|length > 0 %}
            <div class="devices-grid">
                {% for device in devices %}
                <div class="device-card" data-device-id="{{ device.serial_number }}">
                    <div class="device-header">
                        <div class="device-info">
                            <h6>{{ device.device_name }}</h6>
                            <small>{{ device.serial_number }}</small>
                        </div>
                        <div class="status-indicator {{ 'status-connected' if device.is_connected else 'status-disconnected' }}"></div>
                    </div>

                    <div class="device-details">
                        <div class="detail-row">
                            <span class="detail-label">{{ _('ip_address_label') }}:</span>
                            <span class="detail-value">{{ device.ip_address or 'N/A' }}</span>
                        </div>
                        <div class="detail-row">
                            <span class="detail-label">{{ _('status') }}:</span>
                            <span class="detail-value">
                                {% if device.is_connected %}
                                    <span class="text-success">{{ _('online') }}</span>
                                {% else %}
                                    <span class="text-danger">{{ _('offline') }}</span>
                                {% endif %}
                            </span>
                        </div>
                        <div class="detail-row">
                            <span class="detail-label">{{ _('type_short') }}:</span>
                            <span class="detail-value">
                                {% set dtype = (device.device_type or '')|lower %}
                                {% if 'client' in dtype %}
                                    TCP Client
                                {% elif 'server' in dtype %}
                                    TCP Server
                                {% else %}
                                    {{ device.device_type or '-' }}
                                {% endif %}
                            </span>
                        </div>
                    </div>

                    <div class="device-actions">
                        {% if (user_type or '') == 'personnel' %}
                            <button class="action-btn btn-open" onclick="openDevice('{{ device.serial_number }}','shift_in')">
                                <div class="action-icon"><i class="fas fa-sign-in-alt"></i></div>
                                <div class="action-label">{{ _('shift_in_label') }}</div>
                            </button>
                            <button class="action-btn btn-open" onclick="openDevice('{{ device.serial_number }}','shift_out')">
                                <div class="action-icon"><i class="fas fa-sign-out-alt"></i></div>
                                <div class="action-label">{{ _('shift_out_label') }}</div>
                            </button>
                            <button class="action-btn btn-open" onclick="openDevice('{{ device.serial_number }}','break_in')">
                                <div class="action-icon"><i class="fas fa-coffee"></i></div>
                                <div class="action-label">{{ _('break_in_label') }}</div>
                            </button>
                            <button class="action-btn btn-open" onclick="openDevice('{{ device.serial_number }}','break_out')">
                                <div class="action-icon"><i class="fas fa-mug-hot"></i></div>
                                <div class="action-label">{{ _('break_out_label') }}</div>
                            </button>
                        {% elif (user_type or '') == 'guest' %}
                            <button class="action-btn btn-open" onclick="openDevice('{{ device.serial_number }}','guest_in')">
                                <div class="action-icon"><i class="fas fa-user-plus"></i></div>
                                <div class="action-label">{{ _('guest_in_label') }}</div>
                            </button>
                            <button class="action-btn btn-open" onclick="openDevice('{{ device.serial_number }}','guest_out')">
                                <div class="action-icon"><i class="fas fa-user-minus"></i></div>
                                <div class="action-label">{{ _('guest_out_label') }}</div>
                            </button>
                        {% else %}
                            <button class="action-btn btn-open" onclick="openDevice('{{ device.serial_number }}','standard_open')">
                                <div class="action-icon"><i class="fas fa-door-open"></i></div>
                                <div class="action-label">{{ _('open_label') }}</div>
                            </button>
                        {% endif %}
                    </div>

                    <div class="swipe-hint">
                        <i class="fas fa-hand-pointer"></i> {{ _('tap') }}
                    </div>
                </div>
                {% endfor %}
            </div>
        {% else %}
            <div class="no-devices">
                <i class="fas fa-microchip"></i>
                <h5>{{ _('no_devices_yet') }}</h5>
                <p>{{ _('no_assigned_devices') }}</p>
            </div>
        {% endif %}
    </div>

    <!-- Loading Overlay -->
    <div class="loading-overlay" id="loadingOverlay">
        <div class="loading-content">
            <div class="spinner-large"></div>
            <h6>{{ _('processing_action') }}</h6>
            <small class="text-muted">{{ _('please_wait_small') }}</small>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // Touch feedback
        document.querySelectorAll('.device-card').forEach(card => {
            card.addEventListener('touchstart', function() {
                this.classList.add('touching');
            });
            
            card.addEventListener('touchend', function() {
                this.classList.remove('touching');
            });
        });

        // Device actions
        function openDevice(deviceId, action) {
            showLoading();
            
            fetch('/mobile/api/open-device', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ device_id: deviceId, action: action })
            })
            .then(response => response.json())
            .then(data => {
                hideLoading();
                
                if (data.success) {
                    showSuccess({{ _('device_opened')|tojson }});
                    // Add visual feedback
                    const card = document.querySelector(`[data-device-id="${deviceId}"]`);
                    card.style.background = 'linear-gradient(135deg, rgba(40, 167, 69, 0.1), rgba(32, 201, 151, 0.1))';
                    setTimeout(() => {
                        card.style.background = '';
                    }, 2000);
                } else {
                    showError(data.message || {{ _('device_open_failed')|tojson }});
                }
            })
            .catch(error => {
                hideLoading();
                console.error('Error:', error);
                showError({{ _('connection_error')|tojson }});
            });
        }

        function viewLogs(deviceId) {
            showLoading();
            // Redirect to logs page
            window.location.href = `/mobile/device-logs/${deviceId}`;
        }

        function logout() {
            if (confirm({{ _('logout_confirm')|tojson }})) {
                window.location.href = '/mobile/logout';
            }
        }

        // Utility functions
        function showLoading() {
            document.getElementById('loadingOverlay').classList.add('active');
        }

        function hideLoading() {
            document.getElementById('loadingOverlay').classList.remove('active');
        }

        function showSuccess(message) {
            // Create toast notification
            const toast = document.createElement('div');
            toast.className = 'position-fixed top-0 start-50 translate-middle-x mt-3 alert alert-success';
            toast.style.zIndex = '10000';
            toast.innerHTML = `<i class="fas fa-check-circle me-2"></i>${message}`;
            
            document.body.appendChild(toast);
            
            setTimeout(() => {
                toast.remove();
            }, 3000);
        }

        function showError(message) {
            // Create toast notification
            const toast = document.createElement('div');
            toast.className = 'position-fixed top-0 start-50 translate-middle-x mt-3 alert alert-danger';
            toast.style.zIndex = '10000';
            toast.innerHTML = `<i class="fas fa-exclamation-triangle me-2"></i>${message}`;
            
            document.body.appendChild(toast);
            
            setTimeout(() => {
                toast.remove();
            }, 4000);
        }

        // Auto-refresh device status every 30 seconds
        setInterval(() => {
            // Update device status without full page reload
            fetch('/mobile/api/device-status')
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        data.devices.forEach(device => {
                            const card = document.querySelector(`[data-device-id="${device.id}"]`);
                            if (card) {
                                const indicator = card.querySelector('.status-indicator');
                                if (device.connected) {
                                    indicator.className = 'status-indicator status-connected';
                                } else {
                                    indicator.className = 'status-indicator status-disconnected';
                                }
                            }
                        });
                    }
                })
                .catch(error => {
                    console.log('Status update failed:', error);
                });
            // Kullanıcı türünü de güncelle
            fetch('/mobile/api/user-type')
                .then(r => r.json())
                .then(d => {
                    if (d.success) {
                        // sayfayı tam yenilemeden buton grubunu güncellemek için basit bir reload tercih ediyoruz
                        // fark edilirse gereksiz reload engellenir
                        const current = '{{ user_type or "standard" }}';
                        if (d.user_type && d.user_type !== current) {
                            location.reload();
                        }
                    }
                })
                .catch(() => {});
        }, 30000);

        // Prevent zoom on double tap
        let lastTouchEnd = 0;
        document.addEventListener('touchend', function (event) {
            const now = (new Date()).getTime();
            if (now - lastTouchEnd <= 300) {
                event.preventDefault();
            }
            lastTouchEnd = now;
        }, false);
    </script>
</body>
</html> 