<?php
$currentPage = basename($_SERVER['PHP_SELF']);
?>

<aside class="z-20 hidden w-64 overflow-y-auto bg-white dark:bg-gray-800 md:block flex-shrink-0">
  <div class="py-4 text-gray-500 dark:text-gray-400">
    <a class="ml-6 text-lg font-bold text-gray-800 dark:text-gray-200" href="#">PANGLEJAR</a>

    <!-- Dashboard -->
    <ul class="mt-6">
      <li class="relative px-6 py-3">
        <?php if ($currentPage == 'index.php'): ?>
          <span class="absolute inset-y-0 left-0 w-1 bg-purple-600 rounded-tr-lg rounded-br-lg" aria-hidden="true"></span>
        <?php endif; ?>
        <a
          href="index.php"
          class="inline-flex items-center w-full text-sm font-semibold transition-colors duration-150 hover:text-gray-800 dark:hover:text-gray-200 <?= $currentPage == 'index.php' ? 'text-gray-800 dark:text-gray-100' : '' ?>"
        >
          <svg class="w-5 h-5" fill="none" stroke="currentColor" stroke-width="2"
               viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round">
            <path d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"/>
          </svg>
          <span class="ml-4">Dashboard</span>
        </a>
      </li>
    </ul>

    <!-- Data Master Dropdown -->
    <ul>
      <li class="relative px-6 py-3" x-data="{ open: <?= in_array($currentPage, [
        'penerimaan_pucuk.php', 'pelayuan.php', 'penggilingan.php', 'oksidasi.php',
        'pengeringan.php', 'sortasi.php', 'pengepakan.php', 'data_user.php', 'data_teh.php', 'data_lelang.php'
      ]) ? 'true' : 'false' ?> }">
        <button
          class="inline-flex items-center justify-between w-full text-sm font-semibold transition-colors duration-150 hover:text-gray-800 dark:hover:text-gray-200"
          @click="open = !open"
        >
          <span class="inline-flex items-center">
            <svg class="w-5 h-5" fill="none" stroke="currentColor" stroke-width="2"
                 viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round">
              <path d="M4 6h16M4 10h16M4 14h16"/>
            </svg>
            <span class="ml-4">Produksi</span>
          </span>
          <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
            <path fill-rule="evenodd"
                  d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
                  clip-rule="evenodd"/>
          </svg>
        </button>
        <ul x-show="open" class="p-2 mt-2 space-y-2 overflow-hidden text-sm font-medium text-gray-500 rounded-md shadow-inner bg-gray-50 dark:text-gray-400 dark:bg-gray-900">
          <?php
            $dataMasterMenus = [
              ['name' => 'Penerimaan Pucuk', 'file' => 'penerimaan_pucuk.php'],
              ['name' => 'Pelayuan', 'file' => 'pelayuan.php'],
              ['name' => 'Penggilingan', 'file' => 'penggilingan.php'],
              ['name' => 'Oksidasi', 'file' => 'oksidasi.php'],
              ['name' => 'Pengeringan', 'file' => 'pengeringan.php'],
              ['name' => 'Sortasi', 'file' => 'sortasi.php'],
              ['name' => 'Pengepakan', 'file' => 'pengepakan.php'],
              ['name' => 'Manajemen User', 'file' => 'data_user.php'],
              ['name' => 'Data Teh', 'file' => 'data_teh.php'],
              ['name' => 'Data Lelang', 'file' => 'data_lelang.php'],
            ];
            foreach ($dataMasterMenus as $menu):
          ?>
            <li>
              <a
                href="<?= $menu['file'] ?>"
                class="block px-4 py-2 transition-colors duration-150 hover:text-gray-800 dark:hover:text-gray-200 <?= $currentPage == $menu['file'] ? 'text-gray-800 dark:text-gray-100 font-semibold' : '' ?>"
              >
                <?= $menu['name'] ?>
              </a>
            </li>
          <?php endforeach; ?>
        </ul>
      </li>
    </ul>

    <!-- Laporan -->
    <ul>
      <li class="relative px-6 py-3">
        <?php if ($currentPage == 'laporan.php'): ?>
          <span class="absolute inset-y-0 left-0 w-1 bg-purple-600 rounded-tr-lg rounded-br-lg" aria-hidden="true"></span>
        <?php endif; ?>
        <a
          href="laporan.php"
          class="inline-flex items-center w-full text-sm font-semibold transition-colors duration-150 hover:text-gray-800 dark:hover:text-gray-200 <?= $currentPage == 'laporan.php' ? 'text-gray-800 dark:text-gray-100' : '' ?>"
        >
          <svg class="w-5 h-5" fill="none" stroke="currentColor" stroke-width="2"
               viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round">
            <path d="M9 17v-6h13M3 17h3a3 3 0 003-3V4"/>
          </svg>
          <span class="ml-4">Laporan Produksi</span>
        </a>
      </li>
    </ul>

    <!-- Monitoring Produksi -->
    <ul>
      <li class="relative px-6 py-3">
        <?php if ($currentPage == 'monitoring.php'): ?>
          <span class="absolute inset-y-0 left-0 w-1 bg-purple-600 rounded-tr-lg rounded-br-lg" aria-hidden="true"></span>
        <?php endif; ?>
        <a
          href="monitoring.php"
          class="inline-flex items-center w-full text-sm font-semibold transition-colors duration-150 hover:text-gray-800 dark:hover:text-gray-200 <?= $currentPage == 'monitoring.php' ? 'text-gray-800 dark:text-gray-100' : '' ?>"
        >
          <svg class="w-5 h-5" fill="none" stroke="currentColor" stroke-width="2"
               viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round">
            <line x1="12" y1="20" x2="12" y2="10" />
            <line x1="18" y1="20" x2="18" y2="4" />
            <line x1="6" y1="20" x2="6" y2="16" />
          </svg>
          <span class="ml-4">Monitoring Produksi</span>
        </a>
      </li>
    </ul>

    <!-- Penjualan Dropdown -->
    <ul>
      <li class="relative px-6 py-3" x-data="{ open: <?= in_array($currentPage, [
        'stok_gudang.php', 'data_penjualan.php', 'laporan_penjualan.php'
      ]) ? 'true' : 'false' ?> }">
        <button
          class="inline-flex items-center justify-between w-full text-sm font-semibold transition-colors duration-150 hover:text-gray-800 dark:hover:text-gray-200"
          @click="open = !open"
        >
          <span class="inline-flex items-center">
            <svg class="w-5 h-5" fill="none" stroke="currentColor" stroke-width="2"
                 viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round">
              <path d="M16 11V7a4 4 0 00-8 0v4M5 9h14l1 12H4L5 9z"/>
            </svg>
            <span class="ml-4">Penjualan</span>
          </span>
          <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
            <path fill-rule="evenodd"
                  d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
                  clip-rule="evenodd"/>
          </svg>
        </button>
        <ul x-show="open" class="p-2 mt-2 space-y-2 overflow-hidden text-sm font-medium text-gray-500 rounded-md shadow-inner bg-gray-50 dark:text-gray-400 dark:bg-gray-900">
          <?php
            $penjualanMenus = [
              ['name' => 'Stok Gudang', 'file' => 'stok_gudang.php'],
              ['name' => 'Data Penjualan', 'file' => 'data_penjualan.php'],
              ['name' => 'Laporan Penjualan', 'file' => 'laporan_penjualan.php'],
            ];
            foreach ($penjualanMenus as $menu):
          ?>
            <li>
              <a
                href="<?= $menu['file'] ?>"
                class="block px-4 py-2 transition-colors duration-150 hover:text-gray-800 dark:hover:text-gray-200 <?= $currentPage == $menu['file'] ? 'text-gray-800 dark:text-gray-100 font-semibold' : '' ?>"
              >
                <?= $menu['name'] ?>
              </a>
            </li>
          <?php endforeach; ?>
        </ul>
      </li>
    </ul>

    <!-- CTA Button -->
    <div class="px-6 my-6">
      <form method="post" action="produksi_baru.php">
        <button type="submit"
          class="flex items-center justify-between w-full px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded hover:bg-purple-700">
          PRODUKSI BARU
          <span class="ml-2">+</span>
        </button>
      </form>
    </div>
  </div>
</aside>

<?php
$currentPage = basename($_SERVER['PHP_SELF']);
?>

<!-- Sidebar -->
<aside
  class="fixed inset-y-0 z-20 flex-shrink-0 w-64 mt-16 overflow-y-auto bg-white dark:bg-gray-800 md:hidden transform transition-transform duration-300 ease-in-out"
  x-show="isSideMenuOpen"
  x-transition:enter="transform transition ease-in-out duration-300"
  x-transition:enter-start="-translate-x-full opacity-0"
  x-transition:enter-end="translate-x-0 opacity-100"
  x-transition:leave="transform transition ease-in-out duration-300"
  x-transition:leave-start="translate-x-0 opacity-100"
  x-transition:leave-end="-translate-x-full opacity-0"
  @click.away="isSideMenuOpen = false"
>
  <div class="py-4 text-gray-500 dark:text-gray-400">
    <a class="ml-6 text-lg font-bold text-gray-800 dark:text-gray-200" href="#">PANGLEJAR</a>

    <!-- Dashboard -->
    <ul class="mt-6">
      <li class="relative px-6 py-3">
        <?php if ($currentPage == 'index.php'): ?>
          <span class="absolute inset-y-0 left-0 w-1 bg-purple-600 rounded-tr-lg rounded-br-lg"></span>
        <?php endif; ?>
        <a href="index.php"
          class="inline-flex items-center w-full text-sm font-semibold transition-colors duration-150 hover:text-gray-800 dark:hover:text-gray-200 <?= $currentPage == 'index.php' ? 'text-gray-800 dark:text-gray-100' : '' ?>">
          <svg class="w-5 h-5" fill="none" stroke="currentColor" stroke-width="2"
            viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round">
            <path d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"/>
          </svg>
          <span class="ml-4">Dashboard</span>
        </a>
      </li>
    </ul>

    <!-- === PRODUKSI DROPDOWN === -->
    <ul>
      <li class="relative px-6 py-3" 
          x-data="{ open: <?= in_array($currentPage, [
            'penerimaan_pucuk.php','pelayuan.php','penggilingan.php','oksidasi.php',
            'pengeringan.php','sortasi.php','pengepakan.php','data_user.php','data_teh.php','data_lelang.php'
          ]) ? 'true' : 'false' ?> }">
        <button
          class="inline-flex items-center justify-between w-full text-sm font-semibold transition-colors duration-150 hover:text-gray-800 dark:hover:text-gray-200"
          @click="open = !open">
          <span class="inline-flex items-center">
            <svg class="w-5 h-5" fill="none" stroke="currentColor" stroke-width="2"
              viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round">
              <path d="M4 6h16M4 10h16M4 14h16"/>
            </svg>
            <span class="ml-4">Produksi</span>
          </span>
          <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
            <path fill-rule="evenodd"
              d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
              clip-rule="evenodd"/>
          </svg>
        </button>
        <ul x-show="open" x-transition class="p-2 mt-2 space-y-2 overflow-hidden text-sm font-medium text-gray-500 rounded-md shadow-inner bg-gray-50 dark:text-gray-400 dark:bg-gray-900">
          <?php
            $dataMasterMenus = [
              ['name' => 'Penerimaan Pucuk','file' => 'penerimaan_pucuk.php'],
              ['name' => 'Pelayuan','file' => 'pelayuan.php'],
              ['name' => 'Penggilingan','file' => 'penggilingan.php'],
              ['name' => 'Oksidasi','file' => 'oksidasi.php'],
              ['name' => 'Pengeringan','file' => 'pengeringan.php'],
              ['name' => 'Sortasi','file' => 'sortasi.php'],
              ['name' => 'Pengepakan','file' => 'pengepakan.php'],
              ['name' => 'Manajemen User','file' => 'data_user.php'],
              ['name' => 'Data Teh','file' => 'data_teh.php'],
              ['name' => 'Data Lelang','file' => 'data_lelang.php'],
            ];
            foreach ($dataMasterMenus as $menu):
          ?>
            <li>
              <a href="<?= $menu['file'] ?>"
                class="block px-4 py-2 transition-colors duration-150 hover:text-gray-800 dark:hover:text-gray-200 <?= $currentPage == $menu['file'] ? 'text-gray-800 dark:text-gray-100 font-semibold' : '' ?>">
                <?= $menu['name'] ?>
              </a>
            </li>
          <?php endforeach; ?>
        </ul>
      </li>
    </ul>

    <!-- === Laporan === -->
    <ul>
      <li class="relative px-6 py-3">
        <?php if ($currentPage == 'laporan.php'): ?>
          <span class="absolute inset-y-0 left-0 w-1 bg-purple-600 rounded-tr-lg rounded-br-lg"></span>
        <?php endif; ?>
        <a href="laporan.php"
          class="inline-flex items-center w-full text-sm font-semibold transition-colors duration-150 hover:text-gray-800 dark:hover:text-gray-200 <?= $currentPage == 'laporan.php' ? 'text-gray-800 dark:text-gray-100' : '' ?>">
          <svg class="w-5 h-5" fill="none" stroke="currentColor" stroke-width="2"
            viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round">
            <path d="M9 17v-6h13M3 17h3a3 3 0 003-3V4"/>
          </svg>
          <span class="ml-4">Laporan Produksi</span>
        </a>
      </li>
    </ul>

    <!-- === Monitoring === -->
    <ul>
      <li class="relative px-6 py-3">
        <?php if ($currentPage == 'monitoring.php'): ?>
          <span class="absolute inset-y-0 left-0 w-1 bg-purple-600 rounded-tr-lg rounded-br-lg"></span>
        <?php endif; ?>
        <a href="monitoring.php"
          class="inline-flex items-center w-full text-sm font-semibold transition-colors duration-150 hover:text-gray-800 dark:hover:text-gray-200 <?= $currentPage == 'monitoring.php' ? 'text-gray-800 dark:text-gray-100' : '' ?>">
          <svg class="w-5 h-5" fill="none" stroke="currentColor" stroke-width="2"
            viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round">
            <line x1="12" y1="20" x2="12" y2="10"/>
            <line x1="18" y1="20" x2="18" y2="4"/>
            <line x1="6" y1="20" x2="6" y2="16"/>
          </svg>
          <span class="ml-4">Monitoring Produksi</span>
        </a>
      </li>
    </ul>

    <!-- === Penjualan DROPDOWN === -->
    <ul>
      <li class="relative px-6 py-3"
        x-data="{ open: <?= in_array($currentPage, ['stok_gudang.php','data_penjualan.php','laporan_penjualan.php']) ? 'true':'false' ?> }">
        <button
          class="inline-flex items-center justify-between w-full text-sm font-semibold transition-colors duration-150 hover:text-gray-800 dark:hover:text-gray-200"
          @click="open = !open">
          <span class="inline-flex items-center">
            <svg class="w-5 h-5" fill="none" stroke="currentColor" stroke-width="2"
              viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round">
              <path d="M16 11V7a4 4 0 00-8 0v4M5 9h14l1 12H4L5 9z"/>
            </svg>
            <span class="ml-4">Penjualan</span>
          </span>
          <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
            <path fill-rule="evenodd"
              d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
              clip-rule="evenodd"/>
          </svg>
        </button>
        <ul x-show="open" x-transition class="p-2 mt-2 space-y-2 overflow-hidden text-sm font-medium text-gray-500 rounded-md shadow-inner bg-gray-50 dark:text-gray-400 dark:bg-gray-900">
          <?php
            $penjualanMenus = [
              ['name'=>'Stok Gudang','file'=>'stok_gudang.php'],
              ['name'=>'Data Penjualan','file'=>'data_penjualan.php'],
              ['name'=>'Laporan Penjualan','file'=>'laporan_penjualan.php'],
            ];
            foreach ($penjualanMenus as $menu):
          ?>
            <li>
              <a href="<?= $menu['file'] ?>"
                class="block px-4 py-2 transition-colors duration-150 hover:text-gray-800 dark:hover:text-gray-200 <?= $currentPage == $menu['file'] ? 'text-gray-800 dark:text-gray-100 font-semibold' : '' ?>">
                <?= $menu['name'] ?>
              </a>
            </li>
          <?php endforeach; ?>
        </ul>
      </li>
    </ul>

    <!-- CTA -->
    <div class="px-6 my-6">
      <form method="post" action="produksi_baru.php">
        <button type="submit"
          class="flex items-center justify-between w-full px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded hover:bg-purple-700">
          PRODUKSI BARU
          <span class="ml-2">+</span>
        </button>
      </form>
    </div>
  </div>
</aside>
