<?php
session_start();
include 'koneksi.php';

$id_user = $_SESSION['id_user'] ?? 1;

// Ambil data lelang
// Tampilkan baris lelang berbasis data sortasi dengan kondisi rusak.
// Ambil data utama dari sortasi (rusak) lalu LEFT JOIN ke lelang (jika sudah pernah dibuat recordnya)
$query = mysqli_query($conn, "
SELECT 
    s.id_sortasi,
    s.berat_sortasi,
    s.berat_rusak,
    s.kondisi,
    s.kelas_mutu,
    s.mutu,
    s.grade,
    pm.kode_produksi,
    pm.tanggal_mulai,
    l.id_lelang,
    l.harga_awal,
    l.pembeli,
    l.status,
    l.tanggal_lelang,
    /* kode lelang hanya untuk tampilan, tidak diinput manual */
    COALESCE(l.kode_lelang, CONCAT('LEL-', pm.kode_produksi, '-', s.id_sortasi)) AS kode_lelang_display,
    COALESCE(l.tanggal_lelang, pm.tanggal_mulai) AS tanggal_lelang_display
FROM sortasi s
JOIN produksi_master pm ON s.id_produksi = pm.id_produksi
LEFT JOIN lelang l ON l.id_sortasi = s.id_sortasi
WHERE s.kondisi = 'Rusak' AND (s.berat_rusak IS NOT NULL AND s.berat_rusak > 0)
ORDER BY pm.id_produksi DESC, s.id_sortasi ASC
");

$no = 1;
// Dropdown sumber data sortasi (rusak) untuk form tambah
$query_sortasi = mysqli_query($conn, "
SELECT s.id_sortasi, pm.kode_produksi, s.kelas_mutu, s.mutu, s.grade, s.berat_rusak
FROM sortasi s
JOIN produksi_master pm ON s.id_produksi = pm.id_produksi
LEFT JOIN lelang l ON l.id_sortasi = s.id_sortasi
WHERE s.kondisi='Rusak' AND (s.berat_rusak IS NOT NULL AND s.berat_rusak > 0)
ORDER BY pm.id_produksi DESC, s.id_sortasi ASC
");
?>

<!DOCTYPE html>
<html :class="{ 'theme-dark': dark }" x-data="data()" lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Data Lelang</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet" />
    <link rel="stylesheet" href="./assets/css/tailwind.output.css" />
    <script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js" defer></script>
    <script src="./assets/js/init-alpine.js"></script>
</head>
<body>
    <div class="flex h-screen bg-gray-50 dark:bg-gray-900" :class="{ 'overflow-hidden': isSideMenuOpen}">
        <!-- Desktop sidebar -->
        <?php
        $currentPage = basename($_SERVER['PHP_SELF']);
        include 'sidebar.php';
        ?>
        
        <div class="flex flex-col flex-1 overflow-y-auto">
            <!-- Header -->
            <?php include 'header.php'; ?>
            
            <main class="h-full pb-16 overflow-y-auto">
          <div class="container grid px-6 mx-auto">
            <h2
              class="my-6 text-2xl font-semibold text-gray-700 dark:text-gray-200"
            >
              Data Lelang
            </h2>
          <div x-data="{ open: false }">
  <!-- Tombol Tambah Data -->
  <div class="flex justify-start w-auto mb-4">
    <button @click="open = true"
      class="inline-block px-4 py-2 text-sm font-medium text-white bg-purple-600 hover:bg-purple-700 rounded-lg">
      + Tambah Data
    </button>
  </div>

                    <!-- Tabel Data Lelang -->
                    <div class="w-full overflow-hidden rounded-lg shadow-xs">
                        <div class="w-full overflow-x-auto">
                            <table class="table-auto w-full">
                                <thead>
                                    <tr class="text-xs font-semibold tracking-wide text-left text-gray-500 uppercase border-b dark:border-gray-700 bg-gray-50 dark:text-gray-400 dark:bg-gray-800">
                                        <th class="px-4 py-3">No</th>
                                        <th class="px-4 py-3">Kode Lelang</th>
                                        <th class="px-4 py-3">Tanggal Lelang</th>
                                        <th class="px-4 py-3">Berat Teh</th>
                                        <th class="px-4 py-3">Mutu</th>
                                        <th class="px-4 py-3">Grade</th>
                                        <th class="px-4 py-3">Pembeli</th>
                                        <th class="px-4 py-3">Harga</th>
                                        <th class="px-4 py-3">Status</th>
                                        <th class="px-4 py-3">Aksi</th>
                                    </tr>
                                </thead>
                                <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
                                    <?php if (mysqli_num_rows($query) > 0): ?>
                                        <?php while ($row = mysqli_fetch_assoc($query)): ?>
                                            <tr class="text-gray-700 dark:text-gray-400">
                                                <td class="px-4 py-3 text-sm"><?php echo $no++; ?></td>
                                                <td class="px-4 py-3 text-sm"><?php echo htmlspecialchars($row['kode_lelang_display']); ?></td>
                                                <td class="px-4 py-3 text-sm"><?php echo htmlspecialchars($row['tanggal_lelang_display']); ?></td>
                                                <td class="px-4 py-3 text-sm"><?php echo htmlspecialchars($row['berat_rusak']); ?> Kg</td>
                                                <td class="px-4 py-3 text-sm"><?php echo htmlspecialchars($row['kelas_mutu'] . ' - ' . $row['mutu']); ?></td>
                                                <td class="px-4 py-3 text-sm">Grade <?php echo htmlspecialchars($row['grade']); ?></td>
                                                <td class="px-4 py-3 text-sm"><?php echo htmlspecialchars($row['pembeli']); ?></td>
                                                <td class="px-4 py-3 text-sm">
                                                    <?php echo $row['harga_awal'] !== null ? 'Rp ' . number_format($row['harga_awal'], 0, ',', '.') : '-'; ?>
                                                </td>
                                                <td class="px-4 py-3">
                                                    <?php 
                                                    $status = $row['status'] ?? 'Belum Dilelang';
                                                    $status_badge = $status == 'Belum Dilelang' ? 
                                                        '<span class="inline-flex items-center px-2 py-1 text-xs font-medium bg-yellow-100 text-yellow-800 rounded-full">Belum Dilelang</span>' : 
                                                        '<span class="inline-flex items-center px-2 py-1 text-xs font-medium bg-green-100 text-green-800 rounded-full">Sudah Dilelang</span>';
                                                    echo $status_badge;
                                                    ?>
                                                </td>
                                                <td class="px-4 py-3">
                                                    <?php if (!empty($row['id_lelang'])): ?>
                                                        <div class="flex items-center space-x-2 text-sm">
                                                            <a href="update_lelang.php?id_lelang=<?php echo $row['id_lelang']; ?>" class="px-3 py-1 text-xs font-medium leading-5 text-gray-700 bg-gray-200 rounded dark:text-white dark:bg-gray-600">Edit</a>
                                                            <a href="hapus_lelang.php?id_lelang=<?php echo $row['id_lelang']; ?>" onclick="return confirm('Yakin ingin menghapus data ini?')" class="px-3 py-1 text-xs font-medium leading-5 text-white bg-red-600 rounded">Delete</a>
                                                        </div>
                                                    <?php else: ?>
                                                        <span class="text-gray-400">-</span>
                                                    <?php endif; ?>
                                                </td>
                                            </tr>
                                        <?php endwhile; ?>
                                    <?php else: ?>
                                        <tr>
                                            <td colspan="12" class="px-6 py-4 text-center text-sm text-gray-500 dark:text-gray-400">
                                                Tidak ada data lelang yang tersedia.
                                            </td>
                                        </tr>
                                    <?php endif; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>

                    <!-- Modal Tambah Data Lelang -->
                    <div x-show="open"
                         class="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50"
                         x-transition:enter="transition ease-out duration-300"
                         x-transition:enter-start="opacity-0"
                         x-transition:enter-end="opacity-100"
                         x-transition:leave="transition ease-in duration-200"
                         x-transition:leave-start="opacity-100"
                         x-transition:leave-end="opacity-0">
                      <div class="bg-white dark:bg-gray-800 p-6 rounded-xl w-full max-w-2xl mx-4">
                        <h2 class="text-lg font-semibold mb-4 text-gray-800 dark:text-white">Tambah Data Lelang</h2>
                        <form action="tambah_lelang.php" method="GET">
                          <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                            <div>
                              <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Pilih Data Sortasi (Rusak)</label>
                              <select name="id_sortasi" class="w-full px-3 py-2 border rounded dark:bg-gray-700 dark:text-white">
                                <?php while($opt = mysqli_fetch_assoc($query_sortasi)): ?>
                                  <option value="<?php echo $opt['id_sortasi']; ?>">
                                    <?php echo htmlspecialchars($opt['kode_produksi'] . ' - ' . $opt['kelas_mutu'] . ' - ' . $opt['mutu'] . ' - G' . $opt['grade'] . ' (' . $opt['berat_rusak'] . ' Kg)'); ?>
                                  </option>
                                <?php endwhile; ?>
                              </select>
                            </div>
                            <div>
                              <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Tanggal Lelang</label>
                              <input type="date" name="tanggal_lelang" class="w-full px-3 py-2 border rounded dark:bg-gray-700 dark:text-white" />
                            </div>
                            <div>
                              <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Pembeli</label>
                              <input type="text" name="pembeli" class="w-full px-3 py-2 border rounded dark:bg-gray-700 dark:text-white" placeholder="Nama Pembeli" />
                            </div>
                            <div>
                              <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Harga (Rp)</label>
                              <input type="number" name="harga_awal" class="w-full px-3 py-2 border rounded dark:bg-gray-700 dark:text-white" placeholder="Harga" />
                            </div>
                          </div>
                          <div class="flex justify-end space-x-2 mt-6">
                            <button type="button" @click="open = false" class="px-4 py-2 bg-gray-300 dark:bg-gray-600 text-gray-800 dark:text-white rounded hover:bg-gray-400 dark:hover:bg-gray-500">Batal</button>
                            <button type="submit" class="px-4 py-2 text-sm font-medium text-white bg-purple-600 hover:bg-purple-700 rounded-lg">Lanjut</button>
                          </div>
                        </form>
                      </div>
                    </div>
                </div>
            </main>
        </div>
    </div>
</body>
</html>
