{% load static %}
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ project_name|default:"DN-WRS" }}</title>
    <!-- Bootstrap 5 -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Dynamic Google Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    {% if app_settings.font_family == 'Poppins' %}
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    {% elif app_settings.font_family == 'Roboto' %}
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap" rel="stylesheet">
    {% elif app_settings.font_family == 'Montserrat' %}
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    {% elif app_settings.font_family == 'Playfair Display' %}
    <link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@400;500;600;700&display=swap" rel="stylesheet">
    {% else %}
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    {% endif %}

    <!-- Lucide Icons -->
    <script src="https://unpkg.com/lucide@latest"></script>
    <!-- Chart.js -->
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <!-- Custom CSS -->
    <link rel="stylesheet" href="{% static 'css/custom.css' %}?v={% now 'U' %}">
    
    <style>
        :root {
            --primary-color: {{ app_settings.theme_hex|default:'#0d6efd' }};
            --primary-gradient: linear-gradient(135deg, {{ app_settings.theme_hex|default:'#0d6efd' }} 0%, {{ app_settings.theme_gradient_end|default:'#00d2ff' }} 100%);
            --sidebar-width: 280px;
            --glass-bg: rgba(255, 255, 255, 0.85);
            --glass-border: rgba(255, 255, 255, 0.4);
            --text-main: {{ app_settings.font_color|default:'#2d3748' }};
            --font-family: '{{ app_settings.font_family|default:"Inter" }}', sans-serif;
        }
        
        body {
            font-family: var(--font-family);
            background-color: #f8fafc;
            color: var(--text-main);
            min-height: 100vh;
        }

        .btn-primary {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
        }
        .btn-primary:hover {
            background-color: var(--primary-color);
            filter: brightness(0.9);
            border-color: var(--primary-color);
        }
        .text-primary {
            color: var(--primary-color) !important;
        }
        .bg-primary {
            background-color: var(--primary-color) !important;
        }

        /* Sidebar Styles */
        .sidebar {
            width: var(--sidebar-width);
            height: 100vh;
            position: fixed;
            left: 0;
            top: 0;
            display: flex;
            flex-direction: column;
            z-index: 1040;
            transition: transform 0.3s ease;
            
            {% if app_settings.sidebar_theme == 'dark' %}
            background: #1e293b;
            border-right: 1px solid #334155;
            color: #f1f5f9;
            {% elif app_settings.sidebar_theme == 'light' %}
            background: #ffffff;
            border-right: 1px solid #e2e8f0;
            color: #1e293b;
            {% else %}
            background: var(--glass-bg);
            backdrop-filter: blur(15px);
            border-right: 1px solid var(--glass-border);
            {% endif %}
        }

        .main-wrapper {
            margin-left: var(--sidebar-width);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            transition: margin-left 0.3s ease;
        }

        .sidebar-brand {
            padding: 2rem 1.5rem;
            text-decoration: none;
            display: flex;
            align-items: center;
        }

        .sidebar-brand span {
            background: var(--primary-gradient);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 800;
            font-size: 1.25rem;
        }

        .sidebar-nav {
            flex-grow: 1;
            padding: 0 1rem;
            overflow-y: auto;
        }

        .sidebar-section-title {
            padding: 1.5rem 1rem 0.5rem;
            font-size: 0.7rem;
            text-transform: uppercase;
            font-weight: 700;
            letter-spacing: 0.05rem;
            {% if app_settings.sidebar_theme == 'dark' %}
            color: #94a3b8;
            {% else %}
            color: #64748b;
            {% endif %}
        }

        .nav-item-link {
            display: flex;
            align-items: center;
            padding: 0.75rem 1rem;
            text-decoration: none;
            font-weight: 500;
            border-radius: 12px;
            margin-bottom: 0.25rem;
            transition: all 0.2s ease;
            {% if app_settings.sidebar_theme == 'dark' %}
            color: #cbd5e1;
            {% else %}
            color: #475569;
            {% endif %}
        }

        .nav-item-link:hover {
            {% if app_settings.sidebar_theme == 'dark' %}
            background: #334155;
            color: #fff;
            {% else %}
            background: #f1f5f9;
            color: var(--primary-color);
            {% endif %}
        }

        .nav-item-link.active {
            {% if app_settings.sidebar_theme == 'dark' %}
            background: var(--primary-color);
            color: #fff;
            {% else %}
            background: rgba(var(--primary-color-rgb, 13, 110, 253), 0.1);
            color: var(--primary-color);
            {% endif %}
        }
        
        /* Hack for active state color in light/glass theme */
        .nav-item-link.active {
            {% if app_settings.sidebar_theme != 'dark' %}
            background-color: var(--primary-color);
            background-image: var(--primary-gradient);
            color: white;
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
            {% endif %}
        }

        .nav-item-link i {
            margin-right: 12px;
            width: 20px;
            height: 20px;
        }

        /* Top Header */
        .top-header {
            height: 70px;
            background: rgba(248, 250, 252, 0.8);
            backdrop-filter: blur(10px);
            display: flex;
            align-items: center;
            padding: 0 2rem;
            position: sticky;
            top: 0;
            z-index: 1030;
        }

        .search-container input {
            padding-left: 40px;
            border-radius: 12px;
            background: #fff;
            border: 1px solid #e2e8f0;
            height: 42px;
        }
        
        .search-container input:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(var(--primary-color-rgb, 13, 110, 253), 0.15);
        }

        .user-profile-btn {
            background: #fff;
            border: 1px solid #e2e8f0;
            padding: 5px 15px 5px 5px;
            border-radius: 50px;
            display: flex;
            align-items: center;
            transition: all 0.2s;
        }

        .user-profile-btn:hover {
            border-color: var(--primary-color);
            background: #f8fafc;
        }

        @media (max-width: 991.98px) {
            .sidebar { transform: translateX(-100%); }
            .sidebar.show { transform: translateX(0); }
            .main-wrapper { margin-left: 0; }
            .sidebar-overlay {
                display: none;
                position: fixed;
                top: 0; left: 0; right: 0; bottom: 0;
                background: rgba(0,0,0,0.5);
                z-index: 1035;
                backdrop-filter: blur(4px);
            }
            .sidebar-overlay.show { display: block; }
        }

        .fade-in { animation: fadeIn 0.4s ease-out; }
        @keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
    </style>
    {% block extra_css %}{% endblock %}
</head>
<body>
    {% if user.is_authenticated %}
    <div class="sidebar-overlay" id="sidebarOverlay"></div>
    
    <aside class="sidebar" id="sidebar">
        <a class="sidebar-brand" href="{% url 'home' %}">
            <i data-lucide="package" class="me-2 text-primary" style="width: 32px; height: 32px;"></i>
            <span>{{ project_name|default:"DN-WRS" }}</span>
        </a>
        
        <div class="sidebar-nav">
            <div class="sidebar-section-title">Menu Utama</div>
            <a href="{% url 'home' %}" class="nav-item-link {% if request.resolver_match.url_name == 'home' %}active{% endif %}">
                <i data-lucide="layout-dashboard"></i> Dashboard
            </a>
            
            <div class="sidebar-section-title">Manajemen Data</div>
            <a href="{% url 'medicine_list' %}" class="nav-item-link {% if request.resolver_match.url_name == 'medicine_list' %}active{% endif %}">
                <i data-lucide="pill"></i> Daftar Barang
            </a>
            <a href="{% url 'category_list' %}" class="nav-item-link {% if request.resolver_match.url_name == 'category_list' %}active{% endif %}">
                <i data-lucide="tag"></i> Kategori
            </a>
            <a href="{% url 'supplier_list' %}" class="nav-item-link {% if request.resolver_match.url_name == 'supplier_list' %}active{% endif %}">
                <i data-lucide="truck"></i> Supplier
            </a>
            
            <div class="sidebar-section-title">Transaksi</div>
            <a href="{% url 'input_faktur' %}" class="nav-item-link {% if request.resolver_match.url_name == 'input_faktur' or request.resolver_match.url_name == 'faktur_detail' %}active{% endif %}">
                <i data-lucide="file-plus"></i> Inbound / Faktur
            </a>
            <a href="{% url 'barang_keluar' %}" class="nav-item-link {% if request.resolver_match.url_name == 'barang_keluar' %}active{% endif %}">
                <i data-lucide="file-minus"></i> Outbound / Keluar
            </a>
            <a href="{% url 'laporan_transaksi' %}" class="nav-item-link {% if request.resolver_match.url_name == 'laporan_transaksi' %}active{% endif %}">
                <i data-lucide="bar-chart-2"></i> Laporan Stok
            </a>
            
            <div class="sidebar-section-title">Sistem</div>
            <a href="{% url 'app_settings' %}" class="nav-item-link {% if request.resolver_match.url_name == 'app_settings' %}active{% endif %}">
                <i data-lucide="settings"></i> Pengaturan Aplikasi
            </a>
            <a href="/admin/password_change/" class="nav-item-link">
                <i data-lucide="key"></i> Keamanan
            </a>
        </div>
        
        <div class="p-3 border-top {% if app_settings.sidebar_theme == 'dark' %}border-secondary{% endif %}">
            <form method="post" action="{% url 'logout' %}">
                {% csrf_token %}
                <button type="submit" class="nav-item-link text-danger border-0 bg-transparent w-100 text-start">
                    <i data-lucide="log-out"></i> Keluar
                </button>
            </form>
        </div>
    </aside>
    {% endif %}

    <div class="{% if user.is_authenticated %}main-wrapper{% endif %}">
        {% if user.is_authenticated %}
        <header class="top-header">
            <button class="btn btn-link p-0 me-3 d-lg-none text-dark shadow-none" id="sidebarToggle">
                <i data-lucide="menu"></i>
            </button>
            
            <form action="{% url 'medicine_list' %}" method="get" class="search-container d-none d-md-block" style="position: relative; max-width: 400px; width: 100%;">
                <i data-lucide="search" style="position: absolute; left: 15px; top: 50%; transform: translateY(-50%); color: #adb5bd;"></i>
                <input type="text" name="q" class="form-control border-0 shadow-sm" placeholder="Cari barang atau faktur..." value="{{ query|default:'' }}">
            </form>
            
            <div class="ms-auto d-flex align-items-center">
                <div class="dropdown">
                    <button class="user-profile-btn shadow-none border-0" type="button" data-bs-toggle="dropdown">
                        <div class="bg-primary text-white rounded-circle d-flex align-items-center justify-content-center me-2" style="width: 32px; height: 32px; font-weight: 700; font-size: 0.8rem;">
                            {{ user.username|slice:":1"|upper }}
                        </div>
                        <span class="small fw-bold d-none d-sm-block">{{ user.username }}</span>
                        <i data-lucide="chevron-down" class="ms-1 text-muted" style="width: 14px; height: 14px;"></i>
                    </button>
                    <ul class="dropdown-menu dropdown-menu-end border-0 shadow-lg rounded-4 p-2 mt-2">
                        <li><a class="dropdown-item rounded-3 py-2" href="{% url 'app_settings' %}"><i data-lucide="settings" class="me-2 icon-sm"></i> Pengaturan</a></li>
                        <li><hr class="dropdown-divider opacity-50"></li>
                        <li>
                            <form method="post" action="{% url 'logout' %}">
                                {% csrf_token %}
                                <button type="submit" class="dropdown-item rounded-3 py-2 text-danger"><i data-lucide="log-out" class="me-2 icon-sm"></i> Keluar</button>
                            </form>
                        </li>
                    </ul>
                </div>
            </div>
        </header>
        {% endif %}
        
        <div class="{% if user.is_authenticated %}container-fluid px-4 py-4{% else %}container py-5{% endif %}">
            {% if user.is_authenticated and messages %}
                {% for message in messages %}
                    <div class="alert alert-{{ message.tags }} alert-dismissible fade show rounded-4 border-0 shadow-sm mb-4" role="alert">
                        <div class="d-flex align-items-center">
                            {% if message.tags == 'success' %}
                                <i data-lucide="check-circle" class="me-2 text-success"></i>
                            {% elif message.tags == 'error' %}
                                <i data-lucide="x-circle" class="me-2 text-danger"></i>
                            {% else %}
                                <i data-lucide="alert-circle" class="me-2 text-info"></i>
                            {% endif %}
                            <span class="fw-medium">{{ message }}</span>
                        </div>
                        <button type="button" class="btn-close shadow-none" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>
                {% endfor %}
            {% endif %}

            <main class="{% if user.is_authenticated %}fade-in{% endif %}">
                {% block content %}{% endblock %}
            </main>
        </div>

        {% if user.is_authenticated %}
        <footer class="mt-auto py-4 bg-white border-top">
            <div class="container-fluid px-4">
                <div class="row align-items-center">
                    <div class="col-md-6 text-center text-md-start">
                        <p class="mb-0 text-muted small">&copy; {% now "Y" %} {{ project_name|default:"DN-WRS" }}</p>
                    </div>
                    <div class="col-md-6 text-center text-md-end">
                        <div class="d-flex justify-content-center justify-content-md-end gap-3 text-muted" style="font-size: 0.75rem;">
                            <span>v2.0.0</span>
                        </div>
                    </div>
                </div>
            </div>
        </footer>
        {% endif %}
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        lucide.createIcons();
        
        const sidebar = document.getElementById('sidebar');
        const overlay = document.getElementById('sidebarOverlay');
        const toggle = document.getElementById('sidebarToggle');

        if (toggle) {
            toggle.addEventListener('click', () => {
                sidebar.classList.toggle('show');
                overlay.classList.toggle('show');
            });
        }

        if (overlay) {
            overlay.addEventListener('click', () => {
                sidebar.classList.remove('show');
                overlay.classList.remove('show');
            });
        }
    </script>
    {% block extra_js %}{% endblock %}
</body>
</html>