View file File name : item-form.php Content :<?php require_once '_helpers.php'; $conn = get_db_connection(); $business = $conn->query("SELECT * FROM business_settings LIMIT 1")->fetch_assoc(); $is_editing = false; $item = null; $item_id = $_GET['id'] ?? null; if ($item_id) { try { $stmt = $conn->prepare("SELECT * FROM items WHERE id = ?"); $stmt->bind_param("s", $item_id); $stmt->execute(); $result = fetch_all_assoc($stmt); $stmt->close(); if (!empty($result)) { $item = $result[0]; $is_editing = true; } } catch (Exception $e) { // Handle error } } $conn->close(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><?php echo $is_editing ? 'Edit Item' : 'Add Item'; ?> - <?php echo htmlspecialchars($business['name']); ?></title> <script src="https://cdn.tailwindcss.com"></script> <style>:root { --primary: <?php echo htmlspecialchars($business['primary_color'] ?? '217 91% 60%'); ?>; } .bg-primary { background-color: hsl(var(--primary)); } .text-primary { color: hsl(var(--primary)); } .ring-primary:focus-visible { --tw-ring-color: hsl(var(--primary)); }</style> </head> <body class="bg-slate-50"> <div class="flex h-screen"> <?php include '_sidebar.php'; ?> <div class="flex-1 flex flex-col overflow-hidden lg:ml-64"> <?php include '_header.php'; ?> <main class="flex-1 overflow-x-hidden overflow-y-auto bg-slate-50 p-6"> <div class="max-w-4xl mx-auto"> <form id="item-form" class="space-y-8"> <input type="hidden" name="action" value="<?php echo $is_editing ? 'update_item' : 'add_item'; ?>"> <?php if ($is_editing): ?> <input type="hidden" name="item_id" value="<?php echo htmlspecialchars($item['id']); ?>"> <?php endif; ?> <div class="space-y-2"> <h1 class="text-3xl font-bold tracking-tight"><?php echo $is_editing ? 'Edit a' : 'Add a'; ?> Product or Service</h1> <p class="text-slate-500 max-w-2xl">Products and services that you sell to customers are used as items on Invoices to record those sales.</p> </div> <div class="space-y-6 bg-white border rounded-lg p-6"> <div class="grid grid-cols-1 md:grid-cols-3 items-center"> <label for="name" class="font-medium">Name *</label> <div class="md:col-span-2"> <input type="text" id="name" name="name" value="<?php echo htmlspecialchars($item['name'] ?? ''); ?>" required class="w-full h-10 px-3 py-2 border border-slate-300 rounded-md"> </div> </div> <div class="grid grid-cols-1 md:grid-cols-3 items-start"> <label for="description" class="font-medium pt-2">Description</label> <div class="md:col-span-2"> <textarea id="description" name="description" rows="4" class="w-full px-3 py-2 border border-slate-300 rounded-md"><?php echo htmlspecialchars($item['description'] ?? ''); ?></textarea> </div> </div> <div class="grid grid-cols-1 md:grid-cols-3 items-center"> <label for="price" class="font-medium">Price</label> <div class="md:col-span-2"> <input type="number" id="price" name="price" value="<?php echo htmlspecialchars($item['price'] ?? '0.00'); ?>" step="0.01" class="w-full max-w-[150px] h-10 px-3 py-2 border border-slate-300 rounded-md"> </div> </div> <hr> <div class="grid grid-cols-1 md:grid-cols-3 items-start"> <label class="font-medium pt-2">Sell this</label> <div class="md:col-span-2"> <div class="flex items-center space-x-3"> <input type="checkbox" id="sell" name="sell" value="1" class="h-4 w-4 rounded border-gray-300 text-primary focus:ring-primary" <?php echo ($item['sell'] ?? 1) ? 'checked' : ''; ?>> <label for="sell" class="text-sm text-slate-600">Allow this product or service to be added to Invoices.</label> </div> <div id="sell-fields" class="mt-4 pl-7 space-y-4 <?php echo ($item['sell'] ?? 1) ? '' : 'hidden'; ?>"> <div class="grid grid-cols-1 sm:grid-cols-3 items-center"> <label for="incomeAccountId" class="text-sm font-medium">Income account *</label> <div class="sm:col-span-2"> <select name="incomeAccountId" id="incomeAccountId" class="w-full h-10 px-3 py-2 border border-slate-300 rounded-md"> <option value="sales" <?php echo (($item['incomeAccountId'] ?? '') === 'sales') ? 'selected' : ''; ?>>Sales</option> <option value="other" <?php echo (($item['incomeAccountId'] ?? '') === 'other') ? 'selected' : ''; ?>>Other Income</option> </select> </div> </div> </div> </div> </div> <hr> <div class="grid grid-cols-1 md:grid-cols-3 items-start"> <label class="font-medium pt-2">Buy this</label> <div class="md:col-span-2"> <div class="flex items-center space-x-3"> <input type="checkbox" id="buy" name="buy" value="1" class="h-4 w-4 rounded border-gray-300 text-primary focus:ring-primary" <?php echo ($item['buy'] ?? 0) ? 'checked' : ''; ?>> <label for="buy" class="text-sm text-slate-600">Allow this product or service to be added to Bills.</label> </div> </div> </div> </div> <div class="flex justify-start gap-2"> <button type="submit" class="px-6 py-2 bg-primary text-white rounded-full font-semibold hover:bg-opacity-90">Save</button> <a href="items.php" class="px-6 py-2 border rounded-full font-medium hover:bg-slate-100">Cancel</a> </div> </form> </div> </main> </div> </div> <script> document.addEventListener('DOMContentLoaded', () => { const sellCheckbox = document.getElementById('sell'); const sellFields = document.getElementById('sell-fields'); sellCheckbox.addEventListener('change', () => { sellFields.classList.toggle('hidden', !sellCheckbox.checked); }); document.getElementById('item-form').addEventListener('submit', function(e) { e.preventDefault(); const formData = new FormData(this); if (!this.sell.checked) formData.append('sell', '0'); if (!this.buy.checked) formData.append('buy', '0'); fetch('api.php', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { if (data.success) { alert(data.data.message); window.location.href = 'items.php'; } else { alert('Error: ' + data.message); } }) .catch(error => console.error('Error:', error)); }); }); </script> </body> </html>