/home/ejrndhmu/.trash/products.php
<?php
require_once '../config/database.php';
require_once '../config/helpers.php';

startSession();

// Check if user is logged in and is admin
if (!isLoggedIn() || !isAdmin()) {
    redirect('../login.php');
}

$db = new Database();

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['add_product'])) {
        // Add new product
        $name = sanitize($_POST['name']);
        $description = sanitize($_POST['description']);
        $price = (float)$_POST['price'];
        $stock = (int)$_POST['stock'];
        $category_id = (int)$_POST['category_id'];
        
        $errors = [];
        
        // Validation
        if (empty($name) || empty($description) || $price <= 0 || $stock < 0) {
            $errors[] = 'Semua field wajib diisi dengan benar';
        }
        
        // Handle image upload
        $image_path = null;
        if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) {
            $allowed_types = ['image/jpeg', 'image/png', 'image/gif'];
            $max_size = 5 * 1024 * 1024; // 5MB
            
            if (!in_array($_FILES['image']['type'], $allowed_types)) {
                $errors[] = 'Format gambar harus JPG, PNG, atau GIF';
            } elseif ($_FILES['image']['size'] > $max_size) {
                $errors[] = 'Ukuran gambar maksimal 5MB';
            } else {
                $upload_dir = '../uploads/products/';
                $file_extension = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
                $filename = uniqid() . '.' . $file_extension;
                $upload_path = $upload_dir . $filename;
                
                if (move_uploaded_file($_FILES['image']['tmp_name'], $upload_path)) {
                    $image_path = 'uploads/products/' . $filename;
                } else {
                    $errors[] = 'Gagal mengupload gambar';
                }
            }
        }
        
        if (empty($errors)) {
            $db->query('INSERT INTO products (name, description, price, stock, category_id, image, status, created_at) 
                       VALUES (:name, :description, :price, :stock, :category_id, :image, :status, NOW())');
            $db->bind(':name', $name);
            $db->bind(':description', $description);
            $db->bind(':price', $price);
            $db->bind(':stock', $stock);
            $db->bind(':category_id', $category_id);
            $db->bind(':image', $image_path);
            $db->bind(':status', 'active');
            
            if ($db->execute()) {
                setFlashMessage('success', 'Produk berhasil ditambahkan');
                redirect('products.php');
            } else {
                $errors[] = 'Gagal menambahkan produk';
            }
        }
    } elseif (isset($_POST['edit_product'])) {
        // Edit product
        $product_id = (int)$_POST['product_id'];
        $name = sanitize($_POST['name']);
        $description = sanitize($_POST['description']);
        $price = (float)$_POST['price'];
        $stock = (int)$_POST['stock'];
        $category_id = (int)$_POST['category_id'];
        
        $errors = [];
        
        // Validation
        if (empty($name) || empty($description) || $price <= 0 || $stock < 0) {
            $errors[] = 'Semua field wajib diisi dengan benar';
        }
        
        // Get current product data
        $db->query('SELECT image FROM products WHERE id = :id');
        $db->bind(':id', $product_id);
        $current_product = $db->single();
        $image_path = $current_product->image;
        
        // Handle image upload
        if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) {
            $allowed_types = ['image/jpeg', 'image/png', 'image/gif'];
            $max_size = 5 * 1024 * 1024; // 5MB
            
            if (!in_array($_FILES['image']['type'], $allowed_types)) {
                $errors[] = 'Format gambar harus JPG, PNG, atau GIF';
            } elseif ($_FILES['image']['size'] > $max_size) {
                $errors[] = 'Ukuran gambar maksimal 5MB';
            } else {
                $upload_dir = '../uploads/products/';
                $file_extension = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
                $filename = uniqid() . '.' . $file_extension;
                $upload_path = $upload_dir . $filename;
                
                if (move_uploaded_file($_FILES['image']['tmp_name'], $upload_path)) {
                    // Delete old image if exists
                    if ($image_path && file_exists('../' . $image_path)) {
                        unlink('../' . $image_path);
                    }
                    $image_path = 'uploads/products/' . $filename;
                } else {
                    $errors[] = 'Gagal mengupload gambar';
                }
            }
        }
        
        if (empty($errors)) {
            $db->query('UPDATE products SET name = :name, description = :description, price = :price, 
                       stock = :stock, category_id = :category_id, image = :image, updated_at = NOW() 
                       WHERE id = :product_id');
            $db->bind(':name', $name);
            $db->bind(':description', $description);
            $db->bind(':price', $price);
            $db->bind(':stock', $stock);
            $db->bind(':category_id', $category_id);
            $db->bind(':image', $image_path);
            $db->bind(':product_id', $product_id);
            
            if ($db->execute()) {
                setFlashMessage('success', 'Produk berhasil diupdate');
                redirect('products.php');
            } else {
                $errors[] = 'Gagal mengupdate produk';
            }
        }
    } elseif (isset($_POST['delete_product'])) {
        // Delete product
        $product_id = (int)$_POST['product_id'];
        
        // Get product image to delete
        $db->query('SELECT image FROM products WHERE id = :id');
        $db->bind(':id', $product_id);
        $product = $db->single();
        
        $db->query('DELETE FROM products WHERE id = :product_id');
        $db->bind(':product_id', $product_id);
        
        if ($db->execute()) {
            // Delete image file if exists
            if ($product && $product->image && file_exists('../' . $product->image)) {
                unlink('../' . $product->image);
            }
            setFlashMessage('success', 'Produk berhasil dihapus');
        } else {
            setFlashMessage('error', 'Gagal menghapus produk');
        }
        redirect('products.php');
    }
}

// Get products with pagination
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 10;
$offset = ($page - 1) * $limit;

$search = isset($_GET['search']) ? sanitize($_GET['search']) : '';
$category_filter = isset($_GET['category']) ? (int)$_GET['category'] : 0;

// Build query
$where_conditions = [];
$params = [];

if (!empty($search)) {
    $where_conditions[] = '(p.name LIKE :search OR p.description LIKE :search)';
    $params[':search'] = '%' . $search . '%';
}

if ($category_filter > 0) {
    $where_conditions[] = 'p.category_id = :category';
    $params[':category'] = $category_filter;
}

$where_clause = !empty($where_conditions) ? 'WHERE ' . implode(' AND ', $where_conditions) : '';

// Get total count
$db->query('SELECT COUNT(*) as total FROM products p ' . $where_clause);
foreach ($params as $key => $value) {
    $db->bind($key, $value);
}
$total_products = $db->single()->total;

// Get products with category names
$db->query('SELECT p.*, c.name as category_name FROM products p 
           LEFT JOIN categories c ON p.category_id = c.id 
           ' . $where_clause . ' ORDER BY p.created_at DESC LIMIT :limit OFFSET :offset');
foreach ($params as $key => $value) {
    $db->bind($key, $value);
}
$db->bind(':limit', $limit);
$db->bind(':offset', $offset);
$products = $db->resultSet();

$total_pages = ceil($total_products / $limit);

// Get categories for dropdown
$db->query('SELECT * FROM categories ORDER BY name');
$categories = $db->resultSet();

// Get product for editing if edit_id is provided
$edit_product = null;
if (isset($_GET['edit'])) {
    $edit_id = (int)$_GET['edit'];
    $db->query('SELECT * FROM products WHERE id = :id');
    $db->bind(':id', $edit_id);
    $edit_product = $db->single();
}

$flash_message = getFlashMessage();
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kelola Produk - Admin Toko Pro</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: {
                            50: '#f0fdf4',
                            100: '#dcfce7',
                            200: '#bbf7d0',
                            300: '#86efac',
                            400: '#4ade80',
                            500: '#22c55e',
                            600: '#16a34a',
                            700: '#15803d',
                            800: '#166534',
                            900: '#14532d'
                        }
                    }
                }
            }
        }
    </script>
</head>
<body class="bg-gray-50">
    <div class="flex h-screen">
        <?php include 'includes/sidebar.php'; ?>
        
        <!-- Main Content -->
        <div class="flex-1 overflow-auto">
            <!-- Header -->
            <header class="bg-white shadow-sm border-b">
                <div class="px-6 py-4">
                    <div class="flex items-center justify-between">
                        <div>
                            <h1 class="text-2xl font-bold text-gray-800">Kelola Produk</h1>
                            <p class="text-gray-600">Manajemen produk toko</p>
                        </div>
                        <button onclick="openModal('addProductModal')" class="bg-primary-600 hover:bg-primary-700 text-white px-4 py-2 rounded-lg transition duration-200">
                            <svg class="w-5 h-5 inline mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"></path>
                            </svg>
                            Tambah Produk
                        </button>
                    </div>
                </div>
            </header>
            
            <!-- Content -->
            <main class="p-6">
                <?php if ($flash_message): ?>
                    <div class="mb-6 p-4 rounded-lg <?php echo $flash_message['type'] == 'success' ? 'bg-green-50 border border-green-200 text-green-700' : 'bg-red-50 border border-red-200 text-red-700'; ?>">
                        <?php echo $flash_message['message']; ?>
                    </div>
                <?php endif; ?>
                
                <?php if (isset($errors) && !empty($errors)): ?>
                    <div class="mb-6 p-4 rounded-lg bg-red-50 border border-red-200 text-red-700">
                        <ul class="list-disc list-inside">
                            <?php foreach ($errors as $error): ?>
                                <li><?php echo $error; ?></li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                <?php endif; ?>
                
                <!-- Filters -->
                <div class="bg-white rounded-lg shadow-lg p-6 mb-6">
                    <form method="GET" class="flex flex-col md:flex-row gap-4">
                        <div class="flex-1">
                            <input type="text" name="search" value="<?php echo htmlspecialchars($search); ?>" 
                                   placeholder="Cari nama atau deskripsi produk..." 
                                   class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                        </div>
                        <div>
                            <select name="category" class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                                <option value="">Semua Kategori</option>
                                <?php foreach ($categories as $category): ?>
                                    <option value="<?php echo $category->id; ?>" <?php echo $category_filter == $category->id ? 'selected' : ''; ?>>
                                        <?php echo htmlspecialchars($category->name); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <button type="submit" class="bg-primary-600 hover:bg-primary-700 text-white px-6 py-2 rounded-lg transition duration-200">
                            Cari
                        </button>
                        <?php if (!empty($search) || $category_filter > 0): ?>
                            <a href="products.php" class="bg-gray-100 hover:bg-gray-200 text-gray-700 px-6 py-2 rounded-lg transition duration-200">
                                Reset
                            </a>
                        <?php endif; ?>
                    </form>
                </div>
                
                <!-- Products Grid -->
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
                    <?php if (empty($products)): ?>
                        <div class="col-span-full text-center py-12">
                            <svg class="mx-auto h-12 w-12 text-gray-400 mb-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"></path>
                            </svg>
                            <p class="text-gray-500">Tidak ada produk ditemukan</p>
                        </div>
                    <?php else: ?>
                        <?php foreach ($products as $product): ?>
                            <div class="bg-white rounded-lg shadow-lg overflow-hidden">
                                <div class="aspect-w-1 aspect-h-1">
                                    <?php if ($product->image): ?>
                                        <img src="../<?php echo $product->image; ?>" alt="<?php echo htmlspecialchars($product->name); ?>" 
                                             class="w-full h-48 object-cover">
                                    <?php else: ?>
                                        <div class="w-full h-48 bg-gray-200 flex items-center justify-center">
                                            <svg class="w-12 h-12 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"></path>
                                            </svg>
                                        </div>
                                    <?php endif; ?>
                                </div>
                                <div class="p-4">
                                    <h3 class="text-lg font-semibold text-gray-800 mb-2"><?php echo htmlspecialchars($product->name); ?></h3>
                                    <p class="text-sm text-gray-600 mb-2"><?php echo htmlspecialchars($product->category_name ?: 'Tanpa Kategori'); ?></p>
                                    <p class="text-gray-600 text-sm mb-3 line-clamp-2"><?php echo htmlspecialchars(substr($product->description, 0, 100)) . (strlen($product->description) > 100 ? '...' : ''); ?></p>
                                    <div class="flex items-center justify-between mb-3">
                                        <span class="text-lg font-bold text-primary-600"><?php echo formatCurrency($product->price); ?></span>
                                        <span class="text-sm text-gray-500">Stok: <?php echo $product->stock; ?></span>
                                    </div>
                                    <div class="flex space-x-2">
                                        <a href="?edit=<?php echo $product->id; ?>" 
                                           class="flex-1 bg-primary-600 hover:bg-primary-700 text-white px-3 py-2 rounded text-sm text-center transition duration-200">
                                            Edit
                                        </a>
                                        <button onclick="confirmDelete(<?php echo $product->id; ?>, '<?php echo htmlspecialchars($product->name); ?>')" 
                                                class="flex-1 bg-red-600 hover:bg-red-700 text-white px-3 py-2 rounded text-sm transition duration-200">
                                            Hapus
                                        </button>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
                
                <!-- Pagination -->
                <?php if ($total_pages > 1): ?>
                    <div class="mt-8 flex items-center justify-center">
                        <nav class="relative z-0 inline-flex rounded-md shadow-sm -space-x-px">
                            <?php if ($page > 1): ?>
                                <a href="?page=<?php echo $page - 1; ?>&search=<?php echo urlencode($search); ?>&category=<?php echo $category_filter; ?>" 
                                   class="relative inline-flex items-center px-2 py-2 rounded-l-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                                    <svg class="h-5 w-5" fill="currentColor" viewBox="0 0 20 20">
                                        <path fill-rule="evenodd" d="M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z" clip-rule="evenodd"></path>
                                    </svg>
                                </a>
                            <?php endif; ?>
                            
                            <?php for ($i = max(1, $page - 2); $i <= min($total_pages, $page + 2); $i++): ?>
                                <a href="?page=<?php echo $i; ?>&search=<?php echo urlencode($search); ?>&category=<?php echo $category_filter; ?>" 
                                   class="relative inline-flex items-center px-4 py-2 border text-sm font-medium 
                                   <?php echo $i == $page ? 'z-10 bg-primary-50 border-primary-500 text-primary-600' : 'bg-white border-gray-300 text-gray-500 hover:bg-gray-50'; ?>">
                                    <?php echo $i; ?>
                                </a>
                            <?php endfor; ?>
                            
                            <?php if ($page < $total_pages): ?>
                                <a href="?page=<?php echo $page + 1; ?>&search=<?php echo urlencode($search); ?>&category=<?php echo $category_filter; ?>" 
                                   class="relative inline-flex items-center px-2 py-2 rounded-r-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                                    <svg class="h-5 w-5" fill="currentColor" viewBox="0 0 20 20">
                                        <path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd"></path>
                                    </svg>
                                </a>
                            <?php endif; ?>
                        </nav>
                    </div>
                <?php endif; ?>
            </main>
        </div>
    </div>
    
    <!-- Add Product Modal -->
    <div id="addProductModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 hidden z-50">
        <div class="flex items-center justify-center min-h-screen p-4">
            <div class="bg-white rounded-lg shadow-xl max-w-lg w-full max-h-screen overflow-y-auto">
                <div class="px-6 py-4 border-b">
                    <h3 class="text-lg font-semibold text-gray-800">Tambah Produk Baru</h3>
                </div>
                <form method="POST" enctype="multipart/form-data">
                    <div class="px-6 py-4 space-y-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Nama Produk</label>
                            <input type="text" name="name" required 
                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Deskripsi</label>
                            <textarea name="description" rows="3" required 
                                      class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500"></textarea>
                        </div>
                        <div class="grid grid-cols-2 gap-4">
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Harga</label>
                                <input type="number" name="price" step="0.01" min="0" required 
                                       class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Stok</label>
                                <input type="number" name="stock" min="0" required 
                                       class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                            </div>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Kategori</label>
                            <select name="category_id" required 
                                    class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                                <option value="">Pilih Kategori</option>
                                <?php foreach ($categories as $category): ?>
                                    <option value="<?php echo $category->id; ?>"><?php echo htmlspecialchars($category->name); ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Gambar Produk</label>
                            <input type="file" name="image" accept="image/*" 
                                   class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                            <p class="text-xs text-gray-500 mt-1">Format: JPG, PNG, GIF. Maksimal 5MB</p>
                        </div>
                    </div>
                    <div class="px-6 py-4 border-t flex justify-end space-x-3">
                        <button type="button" onclick="closeModal('addProductModal')" 
                                class="px-4 py-2 text-gray-700 bg-gray-100 hover:bg-gray-200 rounded-lg transition duration-200">
                            Batal
                        </button>
                        <button type="submit" name="add_product" 
                                class="px-4 py-2 bg-primary-600 hover:bg-primary-700 text-white rounded-lg transition duration-200">
                            Tambah Produk
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    
    <!-- Edit Product Modal -->
    <?php if ($edit_product): ?>
        <div id="editProductModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 z-50">
            <div class="flex items-center justify-center min-h-screen p-4">
                <div class="bg-white rounded-lg shadow-xl max-w-lg w-full max-h-screen overflow-y-auto">
                    <div class="px-6 py-4 border-b">
                        <h3 class="text-lg font-semibold text-gray-800">Edit Produk</h3>
                    </div>
                    <form method="POST" enctype="multipart/form-data">
                        <input type="hidden" name="product_id" value="<?php echo $edit_product->id; ?>">
                        <div class="px-6 py-4 space-y-4">
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Nama Produk</label>
                                <input type="text" name="name" value="<?php echo htmlspecialchars($edit_product->name); ?>" required 
                                       class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Deskripsi</label>
                                <textarea name="description" rows="3" required 
                                          class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500"><?php echo htmlspecialchars($edit_product->description); ?></textarea>
                            </div>
                            <div class="grid grid-cols-2 gap-4">
                                <div>
                                    <label class="block text-sm font-medium text-gray-700 mb-1">Harga</label>
                                    <input type="number" name="price" step="0.01" min="0" value="<?php echo $edit_product->price; ?>" required 
                                           class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                                </div>
                                <div>
                                    <label class="block text-sm font-medium text-gray-700 mb-1">Stok</label>
                                    <input type="number" name="stock" min="0" value="<?php echo $edit_product->stock; ?>" required 
                                           class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                                </div>
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Kategori</label>
                                <select name="category_id" required 
                                        class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                                    <option value="">Pilih Kategori</option>
                                    <?php foreach ($categories as $category): ?>
                                        <option value="<?php echo $category->id; ?>" <?php echo $edit_product->category_id == $category->id ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($category->name); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <?php if ($edit_product->image): ?>
                                <div>
                                    <label class="block text-sm font-medium text-gray-700 mb-1">Gambar Saat Ini</label>
                                    <img src="../<?php echo $edit_product->image; ?>" alt="Current image" class="w-32 h-32 object-cover rounded-lg">
                                </div>
                            <?php endif; ?>
                            <div>
                                <label class="block text-sm font-medium text-gray-700 mb-1">Gambar Baru (opsional)</label>
                                <input type="file" name="image" accept="image/*" 
                                       class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
                                <p class="text-xs text-gray-500 mt-1">Format: JPG, PNG, GIF. Maksimal 5MB. Kosongkan jika tidak ingin mengubah gambar.</p>
                            </div>
                        </div>
                        <div class="px-6 py-4 border-t flex justify-end space-x-3">
                            <a href="products.php" class="px-4 py-2 text-gray-700 bg-gray-100 hover:bg-gray-200 rounded-lg transition duration-200">
                                Batal
                            </a>
                            <button type="submit" name="edit_product" 
                                    class="px-4 py-2 bg-primary-600 hover:bg-primary-700 text-white rounded-lg transition duration-200">
                                Update Produk
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    <?php endif; ?>
    
    <!-- Delete Confirmation Modal -->
    <div id="deleteModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 hidden z-50">
        <div class="flex items-center justify-center min-h-screen p-4">
            <div class="bg-white rounded-lg shadow-xl max-w-md w-full">
                <div class="px-6 py-4">
                    <h3 class="text-lg font-semibold text-gray-800 mb-2">Konfirmasi Hapus</h3>
                    <p class="text-gray-600">Apakah Anda yakin ingin menghapus produk <span id="deleteProductName" class="font-semibold"></span>?</p>
                    <p class="text-red-600 text-sm mt-2">Tindakan ini tidak dapat dibatalkan.</p>
                </div>
                <div class="px-6 py-4 border-t flex justify-end space-x-3">
                    <button type="button" onclick="closeModal('deleteModal')" 
                            class="px-4 py-2 text-gray-700 bg-gray-100 hover:bg-gray-200 rounded-lg transition duration-200">
                        Batal
                    </button>
                    <form method="POST" class="inline">
                        <input type="hidden" name="product_id" id="deleteProductId">
                        <button type="submit" name="delete_product" 
                                class="px-4 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg transition duration-200">
                            Hapus
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        function openModal(modalId) {
            document.getElementById(modalId).classList.remove('hidden');
        }
        
        function closeModal(modalId) {
            document.getElementById(modalId).classList.add('hidden');
        }
        
        function confirmDelete(productId, productName) {
            document.getElementById('deleteProductId').value = productId;
            document.getElementById('deleteProductName').textContent = productName;
            openModal('deleteModal');
        }
        
        // Close modal when clicking outside
        document.addEventListener('click', function(e) {
            if (e.target.classList.contains('bg-opacity-50')) {
                e.target.classList.add('hidden');
            }
        });
    </script>
</body>
</html>