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

$id_user = $_SESSION['id_user'] ?? 1;
$id_lelang = $_GET['id_lelang'] ?? 0;

if (!$id_lelang) {
    header('Location: data_lelang.php');
    exit;
}

// Ambil data lelang yang akan diupdate
$query = mysqli_query($conn, "
SELECT 
    l.*,
    s.berat_sortasi,
    s.berat_rusak,
    s.kondisi,
    jt.kelas_mutu,
    jt.mutu,
    jt.grade,
    pm.kode_produksi
FROM lelang l
JOIN sortasi s ON l.id_sortasi = s.id_sortasi
JOIN jenis_teh jt ON s.id_teh = jt.id_teh
JOIN produksi_master pm ON s.id_produksi = pm.id_produksi
WHERE l.id_lelang = $id_lelang
");

if (!$query || mysqli_num_rows($query) == 0) {
    header('Location: data_lelang.php');
    exit;
}

$lelang = mysqli_fetch_assoc($query);

// Proses update
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $kode_lelang = $_POST['kode_lelang'];
    $tanggal_lelang = $_POST['tanggal_lelang'];
    $harga_awal = $_POST['harga_awal'];
    $status = $_POST['status'];
    $keterangan = $_POST['keterangan'];
    
    $update_query = mysqli_query($conn, "
        UPDATE lelang 
        SET kode_lelang = '$kode_lelang',
            tanggal_lelang = '$tanggal_lelang',
            harga_awal = $harga_awal,
            status = '$status',
            keterangan = '$keterangan'
        WHERE id_lelang = $id_lelang
    ");
    
    if ($update_query) {
        header('Location: data_lelang.php?success=1');
        exit;
    } else {
        $error = "Gagal mengupdate 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>Update 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">Update Data Lelang</h1>
                        <p class="text-gray-600 dark:text-gray-400">Update informasi data lelang</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 Update -->
                    <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">Kode Lelang</label>
                                    <input type="text" name="kode_lelang" value="<?php echo htmlspecialchars($lelang['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">
                                </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" value="<?php echo htmlspecialchars($lelang['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">Kode Produksi</label>
                                    <input type="text" value="<?php echo htmlspecialchars($lelang['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" value="<?php echo htmlspecialchars($lelang['berat_sortasi']); ?> Kg" 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" value="<?php echo htmlspecialchars($lelang['berat_rusak']); ?> Kg" 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" value="<?php echo htmlspecialchars($lelang['kelas_mutu'] . ' - ' . $lelang['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" value="Grade <?php echo htmlspecialchars($lelang['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" value="<?php echo htmlspecialchars($lelang['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>
                                    <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" value="<?php echo htmlspecialchars($lelang['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">
                                </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" <?php echo $lelang['status'] == 'Belum Dilelang' ? 'selected' : ''; ?>>Belum Dilelang</option>
                                        <option value="Sudah Dilelang" <?php echo $lelang['status'] == 'Sudah Dilelang' ? 'selected' : ''; ?>>Sudah Dilelang</option>
                                    </select>
                                </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"><?php echo htmlspecialchars($lelang['keterangan'] ?? ''); ?></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">
                                    Update Data
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </main>
        </div>
    </div>
</body>
</html>

