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

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

// Ambil data sortasi yang tersedia untuk lelang
$query_sortasi = mysqli_query($conn, "
SELECT 
    s.id_sortasi,
    s.berat_sortasi,
    s.berat_rusak,
    s.kondisi,
    jt.kelas_mutu,
    jt.mutu,
    jt.grade,
    pm.kode_produksi,
    pm.tanggal_mulai
FROM sortasi s
JOIN jenis_teh jt ON s.id_teh = jt.id_teh
JOIN produksi_master pm ON s.id_produksi = pm.id_produksi
WHERE s.id_sortasi NOT IN (SELECT id_sortasi FROM lelang)
  AND (s.berat_sortasi > 0 OR s.berat_rusak > 0)
ORDER BY pm.id_produksi DESC, s.id_sortasi ASC
");

// Proses tambah data lelang
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $id_sortasi = $_POST['id_sortasi'];
    $kode_lelang = $_POST['kode_lelang'];
    $tanggal_lelang = $_POST['tanggal_lelang'];
    $harga_awal = $_POST['harga_awal'];
    $status = $_POST['status'];
    $keterangan = $_POST['keterangan'];
    
    $insert_query = mysqli_query($conn, "
        INSERT INTO lelang (id_sortasi, kode_lelang, tanggal_lelang, harga_awal, status, keterangan, id_user)
        VALUES ($id_sortasi, '$kode_lelang', '$tanggal_lelang', $harga_awal, '$status', '$keterangan', $id_user)
    ");
    
    if ($insert_query) {
        header('Location: data_lelang.php?success=1');
        exit;
    } else {
        $error = "Gagal menambah data lelang: " . mysqli_error($conn);
    }
}
?>

<!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>Tambah 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="flex-1 overflow-x-hidden overflow-y-auto bg-gray-50 dark:bg-gray-900">
                <div class="container mx-auto px-6 py-8">
                    <div class="mb-8">
                        <h1 class="text-2xl font-semibold text-gray-900 dark:text-white">Tambah Data Lelang</h1>
                        <p class="text-gray-600 dark:text-gray-400">Tambah data lelang baru</p>
                    </div>

                    <?php if (isset($error)): ?>
                        <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-6">
                            <?php echo htmlspecialchars($error); ?>
                        </div>
                    <?php endif; ?>

                    <!-- Form Tambah -->
                    <div class="bg-white dark:bg-gray-800 shadow-md rounded-lg p-6">
                        <form method="POST">
                            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                                <div>
                                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Pilih Data Sortasi</label>
                                    <select name="id_sortasi" required onchange="updateSortasiInfo(this.value)"
                                            class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
                                        <option value="">Pilih Data Sortasi</option>
                                        <?php while ($row = mysqli_fetch_assoc($query_sortasi)): ?>
                                            <option value="<?php echo $row['id_sortasi']; ?>" 
                                                    data-berat-sortasi="<?php echo $row['berat_sortasi']; ?>"
                                                    data-berat-rusak="<?php echo $row['berat_rusak']; ?>"
                                                    data-kondisi="<?php echo $row['kondisi']; ?>"
                                                    data-kelas-mutu="<?php echo $row['kelas_mutu']; ?>"
                                                    data-mutu="<?php echo $row['mutu']; ?>"
                                                    data-grade="<?php echo $row['grade']; ?>"
                                                    data-kode-produksi="<?php echo $row['kode_produksi']; ?>">
                                                <?php echo $row['kode_produksi'] . ' - ' . $row['kelas_mutu'] . ' - ' . $row['mutu'] . ' - Grade ' . $row['grade'] . ' (' . $row['kondisi'] . ')'; ?>
                                            </option>
                                        <?php endwhile; ?>
                                    </select>
                                </div>
                                
                                <div>
                                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Kode Lelang</label>
                                    <input type="text" name="kode_lelang" required
                                           class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white"
                                           placeholder="Contoh: LEL-001">
                                </div>
                                
                                <div>
                                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Tanggal Lelang</label>
                                    <input type="date" name="tanggal_lelang" required
                                           class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
                                </div>
                                
                                <div>
                                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Harga Awal</label>
                                    <input type="number" name="harga_awal" required
                                           class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white"
                                           placeholder="Harga per kg">
                                </div>
                                
                                <div>
                                    <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Status</label>
                                    <select name="status" required
                                            class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
                                        <option value="Belum Dilelang">Belum Dilelang</option>
                                        <option value="Sudah Dilelang">Sudah Dilelang</option>
                                    </select>
                                </div>
                            </div>
                            
                            <!-- Info Sortasi yang Dipilih -->
                            <div id="sortasi-info" class="mt-6 p-4 bg-gray-50 dark:bg-gray-700 rounded-lg hidden">
                                <h3 class="text-lg font-medium text-gray-900 dark:text-white mb-4">Informasi Data Sortasi</h3>
                                <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Kode Produksi</label>
                                        <input type="text" id="info-kode-produksi" readonly
                                               class="w-full px-3 py-2 border border-gray-300 rounded-md bg-gray-100 dark:bg-gray-600 dark:border-gray-600 dark:text-white">
                                    </div>
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Berat Sortasi</label>
                                        <input type="text" id="info-berat-sortasi" readonly
                                               class="w-full px-3 py-2 border border-gray-300 rounded-md bg-gray-100 dark:bg-gray-600 dark:border-gray-600 dark:text-white">
                                    </div>
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Berat Rusak</label>
                                        <input type="text" id="info-berat-rusak" readonly
                                               class="w-full px-3 py-2 border border-gray-300 rounded-md bg-gray-100 dark:bg-gray-600 dark:border-gray-600 dark:text-white">
                                    </div>
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Mutu</label>
                                        <input type="text" id="info-mutu" readonly
                                               class="w-full px-3 py-2 border border-gray-300 rounded-md bg-gray-100 dark:bg-gray-600 dark:border-gray-600 dark:text-white">
                                    </div>
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Grade</label>
                                        <input type="text" id="info-grade" readonly
                                               class="w-full px-3 py-2 border border-gray-300 rounded-md bg-gray-100 dark:bg-gray-600 dark:border-gray-600 dark:text-white">
                                    </div>
                                    <div>
                                        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Kondisi</label>
                                        <input type="text" id="info-kondisi" readonly
                                               class="w-full px-3 py-2 border border-gray-300 rounded-md bg-gray-100 dark:bg-gray-600 dark:border-gray-600 dark:text-white">
                                    </div>
                                </div>
                            </div>
                            
                            <div class="mt-6">
                                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">Keterangan</label>
                                <textarea name="keterangan" rows="3"
                                          class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white"
                                          placeholder="Keterangan tambahan..."></textarea>
                            </div>
                            
                            <div class="flex justify-end space-x-3 mt-8">
                                <a href="data_lelang.php" 
                                   class="px-4 py-2 border border-gray-300 rounded-md text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 dark:bg-gray-700 dark:border-gray-600 dark:text-white dark:hover:bg-gray-600">
                                    Batal
                                </a>
                                <button type="submit" 
                                        class="px-4 py-2 bg-blue-600 border border-transparent rounded-md text-sm font-medium text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
                                    Tambah Data
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </main>
        </div>
    </div>

    <script>
        function updateSortasiInfo(idSortasi) {
            if (!idSortasi) {
                document.getElementById('sortasi-info').classList.add('hidden');
                return;
            }
            
            const select = document.querySelector('select[name="id_sortasi"]');
            const option = select.options[select.selectedIndex];
            
            if (option) {
                document.getElementById('info-kode-produksi').value = option.dataset.kodeProduksi;
                document.getElementById('info-berat-sortasi').value = option.dataset.beratSortasi + ' Kg';
                document.getElementById('info-berat-rusak').value = option.dataset.beratRusak + ' Kg';
                document.getElementById('info-mutu').value = option.dataset.kelasMutu + ' - ' + option.dataset.mutu;
                document.getElementById('info-grade').value = 'Grade ' + option.dataset.grade;
                document.getElementById('info-kondisi').value = option.dataset.kondisi;
                
                document.getElementById('sortasi-info').classList.remove('hidden');
            }
        }
    </script>
</body>
</html>

