<?php
/**
 * Seller Center Master Layout (Modern Business Dashboard)
 */
$user = Auth::user();
$store = (new Store())->findByUserId(Auth::id());
?>
<!DOCTYPE html>
<html lang="<?= Language::getLocale() ?>">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="csrf-token" content="<?= Security::csrfToken() ?>">
  <title><?= $pageTitle ?? 'Seller Center - CARGOO' ?></title>
  
  <!-- Bootstrap 5 CSS & Bootstrap Icons -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
  
  <!-- CARGOO Design System CSS -->
  <link rel="stylesheet" href="<?= asset('/assets/css/app.css') ?>">
</head>
<body class="bg-light">
  <!-- Seller Center Master Topbar -->
  <header class="navbar navbar-expand-lg bg-dark navbar-dark sticky-top py-2 px-3 shadow-sm">
    <div class="container-fluid">
      <!-- Left: Mobile Offcanvas Toggler & Official Brand Logo -->
      <div class="d-flex align-items-center gap-2">
        <button class="btn btn-outline-light btn-sm d-md-none border-0 px-2" type="button" data-bs-toggle="offcanvas" data-bs-target="#sellerSidebarOffcanvas" aria-controls="sellerSidebarOffcanvas">
          <i class="bi bi-list fs-5"></i>
        </button>

        <a class="navbar-brand d-flex align-items-center gap-2 me-3" href="<?= url('/seller') ?>">
          <img src="<?= asset('/assets/images/brand/logo2-white.png') ?>" alt="CARGOO Online Shopping" height="32" class="d-inline-block align-text-top">
          <span class="badge bg-primary px-2 py-1 small rounded-pill">Seller Center</span>
        </a>
      </div>

      <!-- Right: Actions & User Navigation -->
      <div class="d-flex align-items-center gap-2 ms-auto">
        <?php if ($store): ?>
          <a href="<?= url('/shop/' . $store['store_slug']) ?>" target="_blank" class="btn btn-outline-light btn-sm px-3 d-none d-sm-flex align-items-center gap-1" title="ดูหน้าร้านค้าของคุณในมุมมองผู้ซื้อ">
            <i class="bi bi-box-arrow-up-right"></i> <span class="d-none d-lg-inline">ดูหน้าร้านค้า (Store Preview)</span>
          </a>
        <?php endif; ?>

        <a href="<?= url('/seller/notifications') ?>" class="btn btn-outline-light btn-sm px-2 position-relative d-flex align-items-center" title="การแจ้งเตือนร้านค้า">
          <i class="bi bi-bell"></i>
          <?php 
            require_once __DIR__ . '/../../app/Models/Notification.php';
            $sellerNotifCount = (new Notification())->getUnreadCount((int)Auth::id(), 'seller');
          ?>
          <?php if ($sellerNotifCount > 0): ?>
            <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger" style="font-size: 0.6rem;">
              <?= $sellerNotifCount ?>
            </span>
          <?php endif; ?>
        </a>

        <a href="<?= url('/') ?>" class="btn btn-outline-light btn-sm px-3 d-flex align-items-center gap-1">
          <i class="bi bi-shop"></i> <span class="d-none d-md-inline">สลับเป็นโหมดผู้ซื้อ</span>
        </a>

        <!-- Language Switcher -->
        <div class="dropdown">
          <button class="btn btn-outline-light btn-sm dropdown-toggle rounded-pill px-2 d-flex align-items-center gap-1" type="button" data-bs-toggle="dropdown" aria-expanded="false">
            <i class="bi bi-globe2 text-primary"></i> <span class="small fw-semibold"><?= strtoupper(Language::getLocale()) ?></span>
          </button>
          <ul class="dropdown-menu dropdown-menu-end shadow-sm border-0 rounded-3 small">
            <li><a class="dropdown-item <?= Language::getLocale() === 'th' ? 'active fw-bold' : '' ?>" href="<?= Language::switchUrl('th') ?>"><span class="badge bg-secondary-subtle text-secondary small border me-1">TH</span> ภาษาไทย</a></li>
            <li><a class="dropdown-item <?= Language::getLocale() === 'en' ? 'active fw-bold' : '' ?>" href="<?= Language::switchUrl('en') ?>"><span class="badge bg-secondary-subtle text-secondary small border me-1">EN</span> English</a></li>
          </ul>
        </div>

        <!-- User Profile Dropdown -->
        <div class="dropdown border-start ps-2 ms-1">
          <button class="btn btn-dark btn-sm dropdown-toggle d-flex align-items-center gap-2 text-white" type="button" data-bs-toggle="dropdown">
            <i class="bi bi-person-circle fs-6 text-primary"></i>
            <span class="d-none d-sm-inline fw-semibold"><?= e($user['first_name'] ?? 'Seller') ?></span>
          </button>
          <ul class="dropdown-menu dropdown-menu-end shadow-sm border-0 rounded-3 small">
            <li><h6 class="dropdown-header text-muted">เข้าสู่ระบบในชื่อ <?= e($user['username'] ?? '') ?></h6></li>
            <li><a class="dropdown-item" href="<?= url('/seller/store') ?>"><i class="bi bi-gear me-2 text-muted"></i> ตั้งค่าร้านค้า</a></li>
            <li><a class="dropdown-item" href="<?= url('/account/profile') ?>"><i class="bi bi-person me-2 text-muted"></i> ข้อมูลบัญชีผู้ใช้</a></li>
            <li><hr class="dropdown-divider"></li>
            <li>
              <form action="<?= url('/logout') ?>" method="POST" class="m-0">
                <?= Security::csrfField() ?>
                <button type="submit" class="dropdown-item text-danger">
                  <i class="bi bi-box-arrow-right me-2"></i> ออกจากระบบ
                </button>
              </form>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </header>

  <!-- Mobile Offcanvas Sidebar Drawer -->
  <div class="offcanvas offcanvas-start bg-white" tabindex="-1" id="sellerSidebarOffcanvas" aria-labelledby="sellerSidebarOffcanvasLabel" style="width: 280px;">
    <div class="offcanvas-header bg-dark text-white py-3">
      <h6 class="offcanvas-title fw-bold" id="sellerSidebarOffcanvasLabel">
        <i class="bi bi-speedometer2 text-primary me-2"></i>เมนู Seller Center
      </h6>
      <button type="button" class="btn-close btn-close-white" data-bs-dismiss="offcanvas" aria-label="Close"></button>
    </div>
    <div class="offcanvas-body p-3">
      <?php if ($store): ?>
        <div class="p-3 bg-light rounded-3 border mb-3 text-center">
          <h6 class="fw-bold text-dark mb-1 text-truncate"><?= e($store['store_name']) ?></h6>
          <span class="badge bg-success-subtle text-success border border-success-subtle small">
            <?= ucfirst($store['status']) ?>
          </span>
        </div>
      <?php endif; ?>

      <div class="nav flex-column account-sidebar-nav gap-1">
        <a href="<?= url('/seller') ?>" class="nav-link <?= ($activeNav ?? '') === 'dashboard' ? 'active' : '' ?>">
          <i class="bi bi-speedometer2"></i> แดชบอร์ด (Dashboard)
        </a>
        <a href="<?= url('/seller/orders') ?>" class="nav-link <?= ($activeNav ?? '') === 'orders' ? 'active' : '' ?>">
          <i class="bi bi-bag-check"></i> จัดการคำสั่งซื้อ (Orders)
        </a>
        <a href="<?= url('/seller/products') ?>" class="nav-link <?= ($activeNav ?? '') === 'products' ? 'active' : '' ?>">
          <i class="bi bi-box-seam"></i> จัดการสินค้า (Products)
        </a>
        <a href="<?= url('/seller/inventory') ?>" class="nav-link <?= ($activeNav ?? '') === 'inventory' ? 'active' : '' ?>">
          <i class="bi bi-boxes"></i> สต็อกสินค้า (Inventory)
        </a>
        <a href="<?= url('/seller/reviews') ?>" class="nav-link <?= ($activeNav ?? '') === 'reviews' ? 'active' : '' ?>">
          <i class="bi bi-star"></i> รีวิวสินค้า (Reviews)
        </a>
        <a href="<?= url('/seller/finance') ?>" class="nav-link <?= ($activeNav ?? '') === 'finance' ? 'active' : '' ?>">
          <i class="bi bi-cash-stack"></i> การเงินและรายได้ (Finance)
        </a>
        <a href="<?= url('/seller/store') ?>" class="nav-link <?= ($activeNav ?? '') === 'store' ? 'active' : '' ?>">
          <i class="bi bi-gear"></i> ตั้งค่าร้านค้า (Store Setup)
        </a>
      </div>
    </div>
  </div>

  <!-- Seller Center Main Workspace -->
  <div class="container-fluid my-4">
    <div class="row g-4">
      <!-- Desktop Left Sidebar Navigation -->
      <div class="col-lg-2 col-md-3 d-none d-md-block">
        <div class="cargoo-card p-3 position-sticky" style="top: 80px;">
          <!-- Store Identity in Sidebar -->
          <?php if ($store): ?>
            <div class="text-center pb-3 mb-3 border-bottom">
              <?php if (!empty($store['logo'])): ?>
                <img src="<?= asset('/uploads/stores/' . e($store['logo'])) ?>" alt="Store Logo" class="rounded-circle border mb-2" style="width: 54px; height: 54px; object-fit: cover;">
              <?php else: ?>
                <div class="rounded-circle bg-primary-subtle text-primary d-flex align-items-center justify-content-center fw-bold mx-auto mb-2" style="width: 54px; height: 54px;">
                  <i class="bi bi-shop fs-4"></i>
                </div>
              <?php endif; ?>
              <h6 class="fw-bold text-dark mb-0 text-truncate"><?= e($store['store_name']) ?></h6>
              <span class="badge bg-success-subtle text-success border border-success-subtle small mt-1" style="font-size: 0.65rem;">
                <?= ucfirst($store['status']) ?>
              </span>
            </div>
          <?php endif; ?>

          <div class="nav flex-column account-sidebar-nav gap-1">
            <a href="<?= url('/seller') ?>" class="nav-link <?= ($activeNav ?? '') === 'dashboard' ? 'active' : '' ?>">
              <i class="bi bi-speedometer2"></i> แดชบอร์ด (Dashboard)
            </a>
            <a href="<?= url('/seller/orders') ?>" class="nav-link <?= ($activeNav ?? '') === 'orders' ? 'active' : '' ?>">
              <i class="bi bi-bag-check"></i> จัดการคำสั่งซื้อ (Orders)
            </a>
            <a href="<?= url('/seller/products') ?>" class="nav-link <?= ($activeNav ?? '') === 'products' ? 'active' : '' ?>">
              <i class="bi bi-box-seam"></i> จัดการสินค้า (Products)
            </a>
            <a href="<?= url('/seller/inventory') ?>" class="nav-link <?= ($activeNav ?? '') === 'inventory' ? 'active' : '' ?>">
              <i class="bi bi-boxes"></i> สต็อกสินค้า (Inventory)
            </a>
            <a href="<?= url('/seller/reviews') ?>" class="nav-link <?= ($activeNav ?? '') === 'reviews' ? 'active' : '' ?>">
              <i class="bi bi-star"></i> รีวิวสินค้า (Reviews)
            </a>
            <a href="<?= url('/seller/notifications') ?>" class="nav-link <?= ($activeNav ?? '') === 'notifications' ? 'active' : '' ?>">
              <i class="bi bi-bell"></i> การแจ้งเตือน (Notifications)
            </a>
            <a href="<?= url('/seller/finance') ?>" class="nav-link <?= ($activeNav ?? '') === 'finance' ? 'active' : '' ?>">
              <i class="bi bi-cash-stack"></i> การเงินและรายได้ (Finance)
            </a>
            <a href="<?= url('/seller/store') ?>" class="nav-link <?= ($activeNav ?? '') === 'store' ? 'active' : '' ?>">
              <i class="bi bi-gear"></i> ตั้งค่าร้านค้า (Store Setup)
            </a>
            <hr class="my-2">
            <a href="<?= url('/') ?>" class="nav-link text-muted small">
              <i class="bi bi-arrow-left"></i> กลับสู่หน้าหลัก
            </a>
          </div>
        </div>
      </div>

      <!-- Main Workspace Area -->
      <div class="col-lg-10 col-md-9">
        <!-- Flash Alert Messages -->
        <?php require __DIR__ . '/../components/alerts.php'; ?>

        <!-- Seller Dashboard Announcements -->
        <?php 
          $placementLocation = 'seller_dashboard';
          require __DIR__ . '/../components/announcement_banner.php';
        ?>

        <!-- View Content Injection -->
        <?= $content ?>
      </div>
    </div>
  </div>

  <!-- Bootstrap 5 Bundle JS -->
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  <!-- CARGOO App JS -->
  <script src="<?= asset('/assets/js/app.js') ?>"></script>
</body>
</html>
