<?php
// includes/layout.php
// Shared dark/gold layout. auth.php must already be included.

if (!isset($page_title)) $page_title = 'Arti Atolye ERP';
if (!isset($active_menu)) $active_menu = '';

$user       = current_user();
$role       = $user['role']       ?? '';
$store_id   = $user['store_id']   ?? null;
$store_code = $user['store_code'] ?? '';
$full_name  = $user['full_name']  ?? 'Kullanıcı';

$parts    = explode(' ', $full_name);
$initials = '';
foreach ($parts as $p) {
    if ($p !== '') {
        $initials .= mb_strtoupper(mb_substr($p, 0, 1, 'UTF-8'), 'UTF-8');
    }
    if (mb_strlen($initials, 'UTF-8') >= 2) break;
}

$role_labels = [
    'admin'              => 'Admin',
    'production_manager' => 'Üretim Yöneticisi',
    'workshop_staff'     => 'Atölye',
    'designer'           => 'Çizimci',
    'store_user'         => 'Mağaza',
    'viewer'             => 'İzleyici',
];
$role_label = $role_labels[$role] ?? $role;

$pending_admin = 0;
$pending_store = 0;
try {
    if (in_array($role, ['admin', 'production_manager'], true)) {
        $pending_admin = (int) db()->query(
            "SELECT COUNT(*) FROM csv_import_rows WHERE row_status = 'admin_pending'"
        )->fetchColumn();
    }
    if ($role === 'store_user' && $store_id) {
        $st = db()->prepare(
            "SELECT COUNT(*) FROM csv_import_rows WHERE store_id = ? AND row_status IN ('store_pending','admin_rejected','store_skipped')"
        );
        $st->execute([$store_id]);
        $pending_store = (int)$st->fetchColumn();
    }
} catch (Throwable $e) {
    // csv_import_rows may not exist before the SQL migration. Hide badges silently.
}
?>
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title><?= htmlspecialchars($page_title) ?> — Arti Atolye ERP</title>
<link rel="stylesheet" href="assets/css/erp.css">
</head>
<body>
<div class="layout">

<aside class="sidebar">
  <div class="sb-brand">
    <div class="sb-brand-name">Arti <span>Atolye</span></div>
    <div class="sb-brand-sub">Üretim Takip · MVP-1</div>
  </div>

  <div class="sb-user">
    <div class="sb-avatar"><?= htmlspecialchars($initials) ?></div>
    <div>
      <div class="sb-user-name"><?= htmlspecialchars($full_name) ?></div>
      <div class="sb-user-role"><?= htmlspecialchars($role_label) ?></div>
    </div>
  </div>

  <?php if ($store_code): ?>
  <div class="sb-store-tag"><span class="dot dot-gld"></span><?= htmlspecialchars($store_code) ?></div>
  <?php endif; ?>

  <nav class="sb-nav">
    <a href="dashboard.php" class="<?= $active_menu === 'dashboard' ? 'active' : '' ?>">
      <span class="ico">⊞</span> Dashboard
    </a>

    <?php if ($role === 'store_user'): ?>
      <div class="sb-section">Sipariş</div>
      <a href="csv_upload.php" class="<?= $active_menu === 'csv' ? 'active' : '' ?>">
        <span class="ico">↑</span> CSV Yükle
        <?php if ($pending_store > 0): ?><span class="nav-badge yl"><?= $pending_store ?></span><?php endif; ?>
      </a>
      <a href="my_orders.php" class="<?= $active_menu === 'my_orders' ? 'active' : '' ?>">
        <span class="ico">≡</span> Siparişlerim
      </a>

      <div class="sb-section">Tasarım</div>
      <a href="design_requests.php" class="<?= $active_menu === 'design_requests' ? 'active' : '' ?>">
        <span class="ico">✦</span> Tasarım Taleplerim
      </a>

    <?php elseif (in_array($role, ['admin', 'production_manager'], true)): ?>
      <div class="sb-section">Siparişler</div>
      <a href="admin_import_review.php" class="<?= $active_menu === 'review' ? 'active' : '' ?>">
        <span class="ico">✓</span> CSV Onayları
        <?php if ($pending_admin > 0): ?><span class="nav-badge"><?= $pending_admin ?></span><?php endif; ?>
      </a>
      <a href="orders.php" class="<?= $active_menu === 'orders' ? 'active' : '' ?>">
        <span class="ico">≡</span> Tüm Siparişler
      </a>
      <a href="csv_upload.php" class="<?= $active_menu === 'csv' ? 'active' : '' ?>">
        <span class="ico">↑</span> CSV Yükle
      </a>

      <div class="sb-section">Taş / Stok</div>
      <a href="stones.php" class="<?= $active_menu === 'stones' ? 'active' : '' ?>">
        <span class="ico">◆</span> Taş Kartları
      </a>
      <a href="stone_purchase.php" class="<?= $active_menu === 'stone_purchase' ? 'active' : '' ?>">
        <span class="ico">＋</span> Taş Alımı
      </a>
      <a href="stone_transactions.php" class="<?= $active_menu === 'stone_transactions' ? 'active' : '' ?>">
        <span class="ico">≡</span> Taş İşlemleri
      </a>
      <?php if ($role === 'admin'): ?>
      <a href="stone_corrections.php" class="<?= $active_menu === 'stone_corrections' ? 'active' : '' ?>">
        <span class="ico">↔</span> Taş Düzeltmeleri
      </a>
      <?php endif; ?>
      <a href="suppliers.php" class="<?= $active_menu === 'suppliers' ? 'active' : '' ?>">
        <span class="ico">◉</span> Tedarikçiler
      </a>
      <?php if ($role === 'admin'): ?>
      <a href="stone_requests.php" class="<?= $active_menu === 'stone_requests' ? 'active' : '' ?>">
        <span class="ico">!</span> Taş Açma Talepleri
      </a>
      <a href="stone_sizes_admin.php" class="<?= $active_menu === 'stone_sizes' ? 'active' : '' ?>">
        <span class="ico">◇</span> Taş Ölçüleri
      </a>
      <?php endif; ?>

      <div class="sb-section">Tasarım</div>
      <a href="design_queue.php?view=waiting" class="<?= $active_menu === 'design_queue' ? 'active' : '' ?>">
        <span class="ico">✦</span> Tasarımcı Kuyruğu
      </a>
      <a href="design_admin.php" class="<?= $active_menu === 'design_admin' ? 'active' : '' ?>">
        <span class="ico">★</span> Tasarım Admin Takibi
      </a>
      <a href="design_requests.php" class="<?= $active_menu === 'design_requests' ? 'active' : '' ?>">
        <span class="ico">≡</span> Tüm Tasarım Talepleri
      </a>

      <div class="sb-section">Üretim</div>
      <a href="production.php" class="<?= $active_menu === 'production' ? 'active' : '' ?>">
        <span class="ico">◈</span> Üretim Listesi
      </a>

      <div class="sb-section">Yönetim</div>
      <a href="stores.php" class="<?= $active_menu === 'stores' ? 'active' : '' ?>">
        <span class="ico">◆</span> Mağazalar
      </a>
      <a href="products.php" class="<?= $active_menu === 'products' ? 'active' : '' ?>">
        <span class="ico">◻</span> Ürün Kartları
      </a>

      <?php if ($role === 'admin'): ?>
      <a href="flagged_customers.php" class="<?= $active_menu === 'flagged' ? 'active' : '' ?>">
        <span class="ico">⚑</span> Sorunlu Müşteriler
      </a>
      <a href="users.php" class="<?= $active_menu === 'users' ? 'active' : '' ?>">
        <span class="ico">⊙</span> Kullanıcılar
      </a>
      <?php endif; ?>

    <?php elseif (in_array($role, ['workshop_staff','designer'], true)): ?>
      <div class="sb-section">Tasarım</div>
      <a href="design_queue.php?view=waiting" class="<?= $active_menu === 'design_queue' ? 'active' : '' ?>">
        <span class="ico">✦</span> Bekleyen / Üzerimdeki
      </a>
      <a href="design_queue.php?view=sent" class="<?= $active_menu === 'design_sent' ? 'active' : '' ?>">
        <span class="ico">⇢</span> Admine Gönderilenler
      </a>
      <a href="design_queue.php?view=revision" class="<?= $active_menu === 'design_revision' ? 'active' : '' ?>">
        <span class="ico">↩</span> Revize Dönenler
      </a>
      <a href="design_requests.php" class="<?= $active_menu === 'design_requests' ? 'active' : '' ?>">
        <span class="ico">≡</span> Tüm Tasarım Talepleri
      </a>
      <div class="sb-section">Üretim</div>
      <a href="production.php" class="<?= $active_menu === 'production' ? 'active' : '' ?>">
        <span class="ico">◈</span> Üretim Listesi
      </a>
    <?php endif; ?>
  </nav>

  <div class="sb-foot">
    <a href="logout.php"><span>⏻</span> Çıkış</a>
  </div>
</aside>

<main class="main">
  <div class="topbar">
    <span class="topbar-title"><?= htmlspecialchars($page_title) ?></span>
  </div>
  <div class="page-content">
<?php
if (!empty($_SESSION['flash'])) {
    $f = $_SESSION['flash'];
    $type = $f['type'] ?? 'info';
    $class = match ($type) {
        'error' => 'flash-error',
        'success' => 'flash-success',
        default => 'flash-info',
    };
    echo '<div class="flash '.$class.'">'.htmlspecialchars((string)($f['msg'] ?? '')).'</div>';
    unset($_SESSION['flash']);
}
?>
