const productCode = document.getElementById("productCode").value.trim();
const productName = document.getElementById("productName").value.trim();
const category = parseInt(document.getElementById("category").value);
const unit = parseInt(document.getElementById("unit").value);
const status = parseInt(document.getElementById("status").value);
const description = document.getElementById("description").value.trim();
if (!productCode || !productName) {
alert("Product Code and Name are required.");
return;
}
const payload = {
companyPk: 478,
name: productName,
category: category,
description: description,
productCode: productCode,
unit: unit,
status: status
};
console.log("Submitting payload:", payload);
fetch('/Product/CreateProduct', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
})
.then(res => res.json())
.then(data => {
if (data.success) {
const unitLabels = { 1: 'Kg', 2: 'Pcs', 3: 'Meters', 4: 'Liters' };
const categoryLabels = { 1: 'Raw Material', 2: 'Semi-Finished', 3: 'Finished Good' };
const statusLabels = { 1: 'bg-label-success', 2: 'bg-label-warning', 3: 'bg-label-danger' };
const statusText = { 1: 'Active', 2: 'Inactive', 3: 'Discontinued' };
const tbody = document.querySelector("table tbody");
const newRow = document.createElement("tr");
newRow.innerHTML = `
| ${productCode} |
${productName} |
${categoryLabels[category]} |
|
${unitLabels[unit]} |
${statusText[status]} |
| `;
tbody.appendChild(newRow);
bootstrap.Modal.getInstance(document.getElementById('addProductModal')).hide();
document.getElementById("productCode").value = "";
document.getElementById("productName").value = "";
document.getElementById("description").value = "";
} else {
alert("Error: " + data.message);
}
})
.catch(err => {
console.error("API error:", err);
alert("Failed to connect to server.");
});