
//using load event listener while page loads 
window.addEventListener('load', function() {
    let name = sessionStorage.getItem('loginedUserName');
    document.getElementById('logined-user-name').innerText = name;
    calllistproductAPI();
});

// To display product as table design the HTML
function listProductData(product) {
let newRowProduct = '';
if(product.length) {
    for( let i=0;i< product.length; i++ ) {
        newRowProduct += '<tr><td>'+product[i].productId+
        '</td><td>'+product[i].productName+'</td><td>'+
        product[i].price+'</td><td>'+
        product[i].vendor+'</td><td>'+
        product[i].quantity+'</td><td>'+
        product[i].warranty+'</td>'+
        '<td><a href="#" id="editProduct"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-pencil-square" viewBox="0 0 16 16"><path d="M15.502 1.94a.5.5 0 0 1 0 .706L14.459 3.69l-2-2L13.502.646a.5.5 0 0 1 .707 0l1.293 1.293zm-1.75 2.456-2-2L4.939 9.21a.5.5 0 0 0-.121.196l-.805 2.414a.25.25 0 0 0 .316.316l2.414-.805a.5.5 0 0 0 .196-.12l6.813-6.814z"/><path fill-rule="evenodd" d="M1 13.5A1.5 1.5 0 0 0 2.5 15h11a1.5 1.5 0 0 0 1.5-1.5v-6a.5.5 0 0 0-1 0v6a.5.5 0 0 1-.5.5h-11a.5.5 0 0 1-.5-.5v-11a.5.5 0 0 1 .5-.5H9a.5.5 0 0 0 0-1H2.5A1.5 1.5 0 0 0 1 2.5v11z"/></svg></a>'+
        '<a href="#" id="deleteProduct'+i+'" onclick="deleteProductAPI('+product[i].productId+')"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash" viewBox="0 0 16 16"><path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z"/><path fill-rule="evenodd" d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z"/></svg></a></td></tr>';
        document.getElementById('productData').innerHTML = newRowProduct;
    }
} else {
    newRowProduct = '<td colspan="7">No Data to Display</td>'
    document.getElementById('productData').innerHTML = newRowProduct;
}
}

//list product function to display the product
async function calllistproductAPI() {
const location = "localhost";
const settings = {
    method: 'GET',
    headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
    }
};

try {
    const fetchResponse = await fetch(`http://${location}:4000/api/productManagement/listProduct/`, settings);
    const data = await fetchResponse.json();
    listProductData(data.product);

    return data;
} catch (e) {
    
    return e;
}
};

//delete function from product table   
async function deleteProductAPI(productID) {
if(confirm("Do you want to delete the product?")){
    const location = "localhost";
const data = {
    "productId": productID
}
const settings = {
    method: 'DELETE',
    headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',

    },
    body: JSON.stringify(data)
};
try {
    const fetchResponse = await fetch(`http://${location}:4000/api/productManagement/deleteProduct/`, settings);
    const data = await fetchResponse.json();
    calllistproductAPI();

    return data;
} catch (e) {

    return e;
}
}
};

//edit a product in the product details table
async function callUpdateproductAPI(productID) {
    const location = "localhost";
    const data = {
        "productId":productID,
        "productName": document.getElementById('productName').value,
        "price": document.getElementById('price').value,
        "quantity": document.getElementById('quantity').value,
        "vendor": document.getElementById('vendor').value,
        "warranty": document.getElementById('warranty').value
    }
    const settings = {
        method: 'PATCH',
        headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(data)
    };
    try {
        const fetchResponse = await fetch(`http://${location}:4000/api/productManagement/updateProduct/`, settings);
        const data = await fetchResponse.json();
        calllistproductAPI();

        return data;
    } catch (e) {
        
        return e;
    }
};
