<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>{% block title %}STRATA (PVT) LTD{% endblock %}</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <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=Inter:wght@400;600;700;800;900&display=swap" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" href="{{ url_for('static', filename='css/premium.css') }}">
  <link rel="manifest" href="{{ url_for('manifest') }}">
</head>
<body class="premium-ui">
<div class="premium-shell">
  <aside class="premium-sidebar" id="premiumSidebar">
    <a class="brand-card" href="{{ url_for('dashboard') }}">
      <div class="brand-mark">◈</div>
      <div>
        <div class="brand-title">360 EDGE SOLUTIONS</div>
        <div class="brand-sub">Business Control Panel</div>
      </div>
    </a>

    <div class="nav-section-title">Main</div>
    <nav class="premium-nav">
      <a href="{{ url_for('dashboard') }}"><span class="nav-icon">📊</span> Dashboard</a>
      <a href="{{ url_for('new_invoice') }}"><span class="nav-icon">➕</span> New Invoice</a>
      <a href="{{ url_for('index') }}"><span class="nav-icon">🧾</span> Order History</a>
      <a href="{{ url_for('stock_manage') }}"><span class="nav-icon">📦</span> Stock</a>
      <a href="{{ url_for('global_search') }}"><span class="nav-icon">🔎</span> Global Search</a>
      <a href="{{ url_for('manage_customers') }}"><span class="nav-icon">👥</span> Customers</a>
      <a href="{{ url_for('manage_imei') }}"><span class="nav-icon">📱</span> IMEI & Warranty</a>
      <a href="{{ url_for('manage_returns') }}"><span class="nav-icon">↩️</span> Returns</a>
      <a href="{{ url_for('delivery_tracking') }}"><span class="nav-icon">🚚</span> Delivery</a>
    </nav>

    <div class="nav-section-title">Finance</div>
    <nav class="premium-nav">
      <a href="{{ url_for('manage_expenses') }}"><span class="nav-icon">💰</span> Expenses</a>
      <a href="{{ url_for('manage_cashbook') }}"><span class="nav-icon">📒</span> Cash Book</a>
      <a href="{{ url_for('cash_summary') }}"><span class="nav-icon">💵</span> Balance Summary</a>
      <a href="{{ url_for('calculate_profit') }}"><span class="nav-icon">📈</span> Profit</a>
      <a href="{{ url_for('platform_report') }}"><span class="nav-icon">🏪</span> Platform Report</a>
      <a href="{{ url_for('tax_report') }}"><span class="nav-icon">🧾</span> Tax Report</a>
    </nav>

    <div class="nav-section-title">Operations</div>
    <nav class="premium-nav">
      <a href="{{ url_for('bulk_import') }}"><span class="nav-icon">📥</span> Bulk Import</a>
      <a href="{{ url_for('stock_locations') }}"><span class="nav-icon">🏬</span> Locations</a>
      <a href="{{ url_for('import_cost_calculator') }}"><span class="nav-icon">🧮</span> Cost Calculator</a>
      <a href="{{ url_for('suppliers') }}"><span class="nav-icon">🏭</span> Suppliers</a>
      <a href="{{ url_for('manage_tasks') }}"><span class="nav-icon">✅</span> Tasks</a>
      <a href="{{ url_for('manage_assets') }}"><span class="nav-icon">🏢</span> Assets</a>
      <a href="{{ url_for('reminders_page') }}"><span class="nav-icon">⏰</span> Reminders</a>
      <a href="{{ url_for('sales_insights') }}"><span class="nav-icon">🧠</span> Sales Insights</a>
    </nav>

    {% if session.get('role') == 'admin' %}
    <div class="nav-section-title">Admin</div>
    <nav class="premium-nav">
      <a href="{{ url_for('manage_users') }}"><span class="nav-icon">👥</span> Users</a>
      <a href="{{ url_for('backups') }}" class="accent"><span class="nav-icon">🛡️</span> Backups</a>
      <a href="{{ url_for('activity_log') }}"><span class="nav-icon">🧾</span> Activity Log</a>
      <a href="{{ url_for('trash_page') }}"><span class="nav-icon">🗑️</span> Trash</a>
      <a href="{{ url_for('error_logs') }}"><span class="nav-icon">🐞</span> Error Logs</a>
      <a href="{{ url_for('download_db') }}" class="accent"><span class="nav-icon">⬇️</span> Download DB</a>
    </nav>
    {% endif %}

    <div class="nav-section-title">Account</div>
    <nav class="premium-nav">
      <button type="button" onclick="toggleDarkMode()"><span class="nav-icon">🌙</span> Dark Mode</button>
      <a href="{{ url_for('user_profile') }}"><span class="nav-icon">👤</span> Profile</a>
      <a href="{{ url_for('logout') }}" class="danger"><span class="nav-icon">🚪</span> Logout</a>
    </nav>
  </aside>

  <main class="premium-main">
    <header class="premium-topbar">
      <div class="d-flex align-items-center gap-2">
        <button class="ghost-btn mobile-menu-btn" type="button" onclick="toggleSidebar()">☰</button>
        <div>
          <div class="topbar-title">{% block page_heading %}Business Management System{% endblock %}</div>
          <div class="topbar-sub">Orders • Stock • Finance • Warranty • Reports</div>
        </div>
      </div>
      <div class="premium-actions">
        <a class="ghost-btn d-none d-md-inline-flex" href="{{ url_for('global_search') }}">🔎 Search</a>
        <a class="premium-btn" href="{{ url_for('new_invoice') }}">+ New Invoice</a>
        <a class="premium-btn" href="{{ url_for('stock_manage') }}">+ Stock</a>
      </div>
    </header>

    <div class="premium-container">
      {% with messages = get_flashed_messages() %}
        {% if messages %}
          {% for message in messages %}
            <div class="alert alert-success alert-dismissible fade show" role="alert">
              <strong>✓</strong> {{ message }}
              <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
          {% endfor %}
        {% endif %}
      {% endwith %}

      <div class="quick-grid d-lg-none">
        <a class="quick-tile" href="{{ url_for('new_invoice') }}"><span>➕</span> New Invoice</a>
        <a class="quick-tile" href="{{ url_for('stock_manage') }}"><span>📦</span> Stock Manager</a>
        <a class="quick-tile" href="{{ url_for('global_search') }}"><span>🔎</span> Search Customer</a>
      </div>

      {% block content %}{% endblock %}
    </div>

    <div class="premium-footer">© 360 EDGE SOLUTIONS (PVT) LTD — Premium business control system</div>
  </main>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
function toggleSidebar(){ document.getElementById('premiumSidebar').classList.toggle('open'); }
function togglePassword(inputId, btn){
  const input = document.getElementById(inputId);
  if(!input) return;
  input.type = input.type === 'password' ? 'text' : 'password';
  if(btn) btn.textContent = input.type === 'password' ? '👁️' : '🙈';
}
function toggleDarkMode(){
  document.body.classList.toggle('dark-mode');
  localStorage.setItem('darkMode', document.body.classList.contains('dark-mode') ? '1' : '0');
}
if(localStorage.getItem('darkMode') === '1'){ document.body.classList.add('dark-mode'); }
document.addEventListener('click', function(e){
  const sidebar = document.getElementById('premiumSidebar');
  if(window.innerWidth <= 1100 && sidebar.classList.contains('open') && !sidebar.contains(e.target) && !e.target.classList.contains('mobile-menu-btn')){ sidebar.classList.remove('open'); }
});
document.addEventListener('DOMContentLoaded', function(){
  document.querySelectorAll('form[method="post"], form[method="POST"]').forEach(function(form){
    if(!form.querySelector('input[name="csrf_token"]')){
      const input=document.createElement('input'); input.type='hidden'; input.name='csrf_token'; input.value='{{ csrf_token() }}'; form.appendChild(input);
    }
  });
  const path=window.location.pathname;
  document.querySelectorAll('.premium-nav a').forEach(function(link){ if(link.getAttribute('href')===path){ link.classList.add('active'); } });
  document.querySelectorAll('.alert').forEach(function(alert){ setTimeout(function(){ try{ new bootstrap.Alert(alert).close(); }catch(e){} }, 5000); });
});
if('serviceWorker' in navigator){ navigator.serviceWorker.register('{{ url_for('service_worker') }}').catch(function(){}); }
</script>
</body>
</html>
