<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>@yield('title', 'Dashboard') - ATARA LifeStyle | Brillez dehors, Encaissez dedans</title>
    <link rel="icon" type="image/svg+xml" href="{{ asset('favicon.svg') }}">
    <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.css">
    <style>
        :root {
            --sidebar-width: 270px;
            --primary: #6366f1;
            --secondary: #8b5cf6;
            --accent: #a855f7;
            --surface: #ffffff;
            --surface-strong: #f8f9fa;
            --border: rgba(0, 0, 0, 0.1);
            --text: #1e293b;
            --muted: #64748b;
            --success: #22c55e;
            --warning: #f59e0b;
            --danger: #ef4444;
        }

        * { box-sizing: border-box; }

        body { 
            font-family: 'Space Grotesk', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f1f5f9;
            color: var(--text);
            min-height: 100vh;
        }

        a { color: var(--primary); text-decoration: none; }
        a:hover { color: var(--secondary); }

        .sidebar {
            background: #ffffff;
            border-right: 1px solid var(--border);
            height: 100vh;
            max-height: 100vh;
            width: var(--sidebar-width);
            position: fixed;
            left: 0;
            top: 0;
            overflow-y: auto;
            overflow-x: hidden;
            z-index: 1000;
            box-shadow: 4px 0 20px rgba(0, 0, 0, 0.08);
        }

        .sidebar::-webkit-scrollbar { width: 6px; }
        .sidebar::-webkit-scrollbar-track { background: transparent; }
        .sidebar::-webkit-scrollbar-thumb { 
            background: rgba(99, 102, 241, 0.3);
            border-radius: 3px;
        }

        .sidebar .nav-link {
            color: var(--text);
            padding: 12px 20px;
            transition: all 0.3s ease;
            border-left: 3px solid transparent;
            font-size: 0.95rem;
            font-weight: 500;
            display: flex;
            align-items: center;
        }

        .sidebar .nav-link:hover {
            background: rgba(99, 102, 241, 0.1);
            color: var(--primary);
            border-left-color: var(--primary);
            padding-left: 25px;
        }

        .sidebar .nav-link.active {
            background: rgba(99, 102, 241, 0.15);
            color: var(--primary);
            border-left-color: var(--primary);
        }

        .sidebar .nav-section {
            padding: 15px 20px 8px;
            font-size: 0.7rem;
            text-transform: uppercase;
            letter-spacing: 1.5px;
            color: var(--muted);
            opacity: 0.8;
            margin-top: 15px;
            font-weight: 700;
        }

        .main-content {
            margin-left: var(--sidebar-width);
            padding: 25px;
            min-height: 100vh;
        }

        .topbar {
            background: #ffffff;
            padding: 18px 30px;
            margin: -25px -25px 30px -25px;
            border-bottom: 1px solid var(--border);
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
        }

        /* Notification Bell Styles */
        .notification-bell-wrapper {
            position: relative;
        }

        .notification-bell-btn {
            position: relative;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            border: 2px solid var(--border);
            background: #fff;
            transition: all 0.3s ease;
        }

        .notification-bell-btn:hover {
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            border-color: transparent;
            color: #fff;
            transform: scale(1.1);
            box-shadow: 0 4px 15px rgba(99, 102, 241, 0.4);
        }

        .notification-bell-btn i {
            font-size: 1.1rem;
        }

        .notification-badge {
            font-size: 0.65rem;
            padding: 0.25em 0.5em;
            min-width: 18px;
            animation: pulse-badge 2s infinite;
        }

        @keyframes pulse-badge {
            0%, 100% { transform: translate(-50%, -50%) scale(1); }
            50% { transform: translate(-50%, -50%) scale(1.15); }
        }

        .notification-pulse {
            position: absolute;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
            border-radius: 50%;
            background: var(--primary);
            opacity: 0;
            pointer-events: none;
        }

        .notification-bell-btn.has-unread .notification-pulse {
            animation: ripple 1.5s infinite;
        }

        @keyframes ripple {
            0% { transform: scale(1); opacity: 0.4; }
            100% { transform: scale(1.5); opacity: 0; }
        }

        .notification-dropdown {
            position: absolute;
            top: calc(100% + 10px);
            right: 0;
            width: 320px;
            background: #fff;
            border-radius: 16px;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
            opacity: 0;
            visibility: hidden;
            transform: translateY(-10px);
            transition: all 0.3s ease;
            z-index: 1000;
            border: 1px solid var(--border);
        }

        .notification-bell-wrapper:hover .notification-dropdown {
            opacity: 1;
            visibility: visible;
            transform: translateY(0);
        }

        .notification-dropdown-header {
            padding: 15px 20px;
            border-bottom: 1px solid var(--border);
            display: flex;
            justify-content: space-between;
            align-items: center;
            background: linear-gradient(135deg, rgba(99, 102, 241, 0.1), rgba(139, 92, 246, 0.1));
            border-radius: 16px 16px 0 0;
        }

        .notification-dropdown-body {
            max-height: 300px;
            overflow-y: auto;
        }

        .notification-dropdown-body::-webkit-scrollbar {
            width: 6px;
        }

        .notification-dropdown-body::-webkit-scrollbar-thumb {
            background: var(--primary);
            border-radius: 10px;
        }

        .notification-dropdown-footer {
            padding: 10px 15px;
            border-top: 1px solid var(--border);
            text-align: center;
            background: rgba(248, 250, 252, 0.8);
            border-radius: 0 0 16px 16px;
        }

        .notif-item {
            padding: 12px 20px;
            border-bottom: 1px solid var(--border);
            transition: all 0.2s ease;
            cursor: pointer;
            display: flex;
            gap: 12px;
        }

        .notif-item:hover {
            background: rgba(99, 102, 241, 0.05);
        }

        .notif-item.unread {
            background: rgba(99, 102, 241, 0.08);
            border-left: 3px solid var(--primary);
        }

        .notif-icon {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-shrink: 0;
        }

        .notif-icon.primary { background: rgba(99, 102, 241, 0.15); color: var(--primary); }
        .notif-icon.success { background: rgba(16, 185, 129, 0.15); color: var(--success); }
        .notif-icon.warning { background: rgba(245, 158, 11, 0.15); color: var(--warning); }
        .notif-icon.danger { background: rgba(239, 68, 68, 0.15); color: var(--danger); }
        .notif-icon.info { background: rgba(59, 130, 246, 0.15); color: #3b82f6; }

        .notif-content {
            flex: 1;
            min-width: 0;
        }

        .notif-title {
            font-weight: 600;
            font-size: 0.85rem;
            margin-bottom: 2px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .notif-message {
            font-size: 0.8rem;
            color: var(--muted);
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .notif-time {
            font-size: 0.7rem;
            color: var(--muted);
            margin-top: 3px;
        }

        .card {
            background: #ffffff;
            border: 1px solid var(--border);
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            border-radius: 12px;
            transition: transform 0.3s ease;
        }

        .card:hover { transform: translateY(-2px); }

        .card-header {
            background: rgba(99, 102, 241, 0.1);
            border-bottom: 1px solid var(--border);
            font-weight: 600;
        }

        .btn-primary {
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            border: none;
            font-weight: 600;
            padding: 10px 20px;
            transition: all 0.3s ease;
        }

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

        .table {
            color: var(--text);
            background: transparent;
        }

        .table thead {
            background: rgba(99, 102, 241, 0.1);
            border-bottom: 2px solid var(--border);
        }

        .table tbody tr {
            border-bottom: 1px solid var(--border);
            transition: background 0.2s ease;
        }

        .table tbody tr:hover {
            background: rgba(99, 102, 241, 0.05);
        }

        .badge {
            padding: 6px 12px;
            font-weight: 600;
            border-radius: 6px;
        }

        .form-control, .form-select {
            background: #ffffff;
            border: 1px solid var(--border);
            color: var(--text);
        }

        .form-control:focus, .form-select:focus {
            background: #ffffff;
            border-color: var(--primary);
            color: var(--text);
            box-shadow: 0 0 0 0.2rem rgba(99, 102, 241, 0.25);
        }

        .stat-card {
            background: #fff;
            border-radius: 12px;
            padding: 1.5rem;
            border: 1px solid var(--border);
            transition: all 0.3s ease;
        }
        .stat-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.1);
        }
        .stat-card.primary { border-left: 4px solid var(--primary); }
        .stat-card.success { border-left: 4px solid var(--success); }
        .stat-card.warning { border-left: 4px solid var(--warning); }
        .stat-card.danger { border-left: 4px solid var(--danger); }

        @media (max-width: 768px) {
            .sidebar {
                transform: translateX(-100%);
                transition: transform 0.3s ease;
            }
            .sidebar.show { transform: translateX(0); }
            .main-content { margin-left: 0; }
        }
    </style>
    @stack('styles')
</head>
<body>
    @include('components.impersonate-banner')
    
    <nav class="sidebar">
        <div class="p-3 text-center border-bottom">
            <h5 class="text-primary"><img src="{{ asset('images/logo.png') }}" alt="ATARA" height="30" class="me-2">ATARA</h5>
            <small class="text-muted">Espace Vendeur</small>
        </div>
        <ul class="nav flex-column px-2 py-2">
            <li class="nav-item">
                <a class="nav-link {{ request()->routeIs('seller.dashboard') ? 'active' : '' }}" href="{{ route('seller.dashboard') }}">
                    <i class="bi bi-speedometer2 me-2"></i>Tableau de bord
                </a>
            </li>

            <li class="nav-section">Gestion</li>
            <li class="nav-item">
                <a class="nav-link {{ request()->routeIs('seller.products.*') ? 'active' : '' }}" href="{{ route('seller.products.index') }}">
                    <i class="bi bi-box-seam me-2"></i>Mes Produits
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link {{ request()->routeIs('seller.orders.*') ? 'active' : '' }}" href="{{ route('seller.orders.index') }}">
                    <i class="bi bi-cart3 me-2"></i>Commandes
                    @php $pendingOrders = \App\Models\Order::where('seller_id', auth()->id())->where('status', 'pending')->count(); @endphp
                    @if($pendingOrders > 0)<span class="badge bg-danger ms-auto">{{ $pendingOrders }}</span>@endif
                </a>
            </li>

            <li class="nav-section">Finance</li>
            <li class="nav-item">
                <a class="nav-link {{ request()->routeIs('seller.wallet.*') ? 'active' : '' }}" href="{{ route('seller.wallet.index') }}">
                    <i class="bi bi-wallet2 me-2"></i>Portefeuille
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link {{ request()->routeIs('seller.coupons.*') ? 'active' : '' }}" href="{{ route('seller.coupons.index') }}">
                    <i class="bi bi-ticket-perforated me-2"></i>Coupons
                </a>
            </li>

            <li class="nav-section">Engagement</li>
            <li class="nav-item">
                <a class="nav-link {{ request()->routeIs('seller.notifications.*') ? 'active' : '' }}" href="{{ route('seller.notifications.index') }}">
                    <i class="bi bi-bell me-2"></i>Notifications
                    @php
                        $unreadNotifCount = \App\Models\Notification::where('user_id', auth()->id())->where('is_read', false)->count();
                    @endphp
                    @if($unreadNotifCount > 0)
                        <span class="badge bg-danger rounded-pill ms-auto">{{ $unreadNotifCount > 99 ? '99+' : $unreadNotifCount }}</span>
                    @endif
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link {{ request()->routeIs('seller.reviews.*') ? 'active' : '' }}" href="{{ route('seller.reviews.index') }}">
                    <i class="bi bi-star me-2"></i>Avis Clients
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link {{ request()->routeIs('seller.disputes.*') ? 'active' : '' }}" href="{{ route('seller.disputes.index') }}">
                    <i class="bi bi-exclamation-triangle me-2"></i>Litiges
                </a>
            </li>

            <li class="nav-section">Parametres</li>
            <li class="nav-item">
                <a class="nav-link {{ request()->routeIs('seller.profile.*') ? 'active' : '' }}" href="{{ route('seller.profile.index') }}">
                    <i class="bi bi-person-gear me-2"></i>Mon Profil
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link {{ request()->routeIs('seller.addresses.*') ? 'active' : '' }}" href="{{ route('seller.profile.index') }}#addresses-tab">
                    <i class="bi bi-geo-alt me-2"></i>Mes Adresses
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link {{ request()->routeIs('seller.kyc.*') ? 'active' : '' }}" href="{{ route('seller.kyc.index') }}">
                    <i class="bi bi-shield-check me-2"></i>Verification KYC
                </a>
            </li>
        </ul>
        <div class="p-3 border-top mt-auto">
            <div class="d-flex align-items-center mb-2">
                <i class="bi bi-person-circle fs-5 me-2 text-primary"></i>
                <div class="small flex-grow-1">
                    <div class="fw-bold">{{ Auth::user()->name ?? 'Vendeur' }}</div>
                    <div class="text-muted">{{ Auth::user()->seller->shop_name ?? 'Boutique' }}</div>
                </div>
            </div>
            <a href="{{ route('seller.profile.index') }}" class="btn btn-outline-primary w-100 btn-sm mb-2">
                <i class="bi bi-person-gear me-2"></i>Mon Profil
            </a>
            <form action="{{ route('logout') }}" method="POST">
                @csrf
                <button type="submit" class="btn btn-outline-danger w-100 btn-sm">
                    <i class="bi bi-box-arrow-right me-2"></i>Deconnexion
                </button>
            </form>
        </div>
    </nav>

    <main class="main-content">
        <div class="topbar d-flex justify-content-between align-items-center flex-wrap gap-2">
            <h2 class="h5 mb-0">@yield('page-title', 'Tableau de bord')</h2>
            <div class="d-flex align-items-center gap-3">
                <!-- Notification Bell -->
                <div class="notification-bell-wrapper position-relative" id="notificationBell">
                    <a href="{{ route('seller.notifications.index') }}" class="btn btn-outline-secondary btn-sm position-relative notification-bell-btn">
                        <i class="bi bi-bell-fill"></i>
                        <span class="notification-badge position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger" id="notifBadge" style="display: none;">
                            0
                        </span>
                        <span class="notification-pulse"></span>
                    </a>
                    <!-- Dropdown Preview -->
                    <div class="notification-dropdown" id="notificationDropdown">
                        <div class="notification-dropdown-header">
                            <span class="fw-semibold">Notifications</span>
                            <a href="{{ route('seller.notifications.index') }}" class="text-primary small">Voir tout</a>
                        </div>
                        <div class="notification-dropdown-body" id="notificationList">
                            <div class="text-center py-3 text-muted small">Chargement...</div>
                        </div>
                        <div class="notification-dropdown-footer">
                            <button class="btn btn-sm btn-link text-primary" onclick="markAllAsRead()">Tout marquer comme lu</button>
                        </div>
                    </div>
                </div>
                <a href="{{ route('seller.products.create') }}" class="btn btn-primary btn-sm">
                    <i class="bi bi-plus-lg me-1"></i>Nouveau Produit
                </a>
                <span class="text-muted small d-none d-md-inline">{{ now()->format('d/m/Y H:i') }}</span>
            </div>
        </div>

        @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show">
            <i class="bi bi-check-circle me-2"></i>{{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
        @endif
        
        @if(session('error'))
        <div class="alert alert-danger alert-dismissible fade show">
            <i class="bi bi-exclamation-circle me-2"></i>{{ session('error') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
        @endif
        
        @if(isset($errors) && $errors->any())
        <div class="alert alert-danger alert-dismissible fade show">
            <ul class="mb-0">@foreach($errors->all() as $error)<li>{{ $error }}</li>@endforeach</ul>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
        @endif

        @yield('content')
    </main>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    
    <!-- SweetAlert2 Ultra Modern Theme -->
    @include('components.swal-modern-theme')
    
    <script>
        document.querySelectorAll('.alert').forEach(function(alert) {
            setTimeout(function() {
                let bsAlert = new bootstrap.Alert(alert);
                bsAlert.close();
            }, 5000);
        });

        @if(session('success'))
            Swal.fire({
                icon: 'success',
                title: 'Succes !',
                text: '{{ session('success') }}',
                toast: true,
                position: 'top-end',
                showConfirmButton: false,
                timer: 3000,
                timerProgressBar: true
            });
        @endif

        @if(session('error'))
            Swal.fire({
                icon: 'error',
                title: 'Erreur !',
                text: '{{ session('error') }}',
                toast: true,
                position: 'top-end',
                showConfirmButton: false,
                timer: 3000,
                timerProgressBar: true
            });
        @endif

        // Notification Bell System
        function loadNotifications() {
            fetch('{{ route('seller.notifications.latest') }}')
                .then(response => response.json())
                .then(data => {
                    updateNotificationBadge(data.unread_count);
                    renderNotificationList(data.notifications);
                })
                .catch(error => console.log('Error loading notifications:', error));
        }

        function updateNotificationBadge(count) {
            const badge = document.getElementById('notifBadge');
            const bellBtn = document.querySelector('.notification-bell-btn');
            
            if (count > 0) {
                badge.textContent = count > 99 ? '99+' : count;
                badge.style.display = 'flex';
                bellBtn.classList.add('has-unread');
            } else {
                badge.style.display = 'none';
                bellBtn.classList.remove('has-unread');
            }
        }

        function renderNotificationList(notifications) {
            const container = document.getElementById('notificationList');
            
            if (notifications.length === 0) {
                container.innerHTML = '<div class="text-center py-4 text-muted"><i class="bi bi-bell-slash fs-3 mb-2 d-block"></i><span class="small">Aucune notification</span></div>';
                return;
            }

            let html = '';
            notifications.forEach(notif => {
                const colorClass = notif.color || 'primary';
                html += `
                    <div class="notif-item ${notif.is_read ? '' : 'unread'}" onclick="handleNotificationClick('${notif.id}', '${notif.action_url || ''}')">
                        <div class="notif-icon ${colorClass}">
                            <i class="bi ${notif.icon || 'bi-bell'}"></i>
                        </div>
                        <div class="notif-content">
                            <div class="notif-title">${notif.title}</div>
                            <div class="notif-message">${notif.message}</div>
                            <div class="notif-time"><i class="bi bi-clock me-1"></i>${notif.created_at}</div>
                        </div>
                    </div>
                `;
            });
            container.innerHTML = html;
        }

        function handleNotificationClick(id, url) {
            fetch(`{{ url('seller/notifications') }}/${id}/read`, {
                method: 'POST',
                headers: {
                    'X-CSRF-TOKEN': '{{ csrf_token() }}',
                    'Content-Type': 'application/json'
                }
            }).then(() => {
                if (url) {
                    window.location.href = url;
                } else {
                    loadNotifications();
                }
            });
        }

        function markAllAsRead() {
            fetch('{{ route('seller.notifications.read-all') }}', {
                method: 'POST',
                headers: {
                    'X-CSRF-TOKEN': '{{ csrf_token() }}',
                    'Content-Type': 'application/json'
                }
            })
            .then(response => response.json())
            .then(data => {
                loadNotifications();
                Swal.fire({
                    icon: 'success',
                    title: 'Termine !',
                    text: data.message,
                    toast: true,
                    position: 'top-end',
                    showConfirmButton: false,
                    timer: 2000
                });
            });
        }

        // Load notifications on page load
        document.addEventListener('DOMContentLoaded', function() {
            loadNotifications();
            // Refresh notifications every 30 seconds
            setInterval(loadNotifications, 30000);
        });
    </script>
    
    @stack('scripts')
</body>
</html>
