//edit sub category
function edit_subCat(e, indx, sub_id) {
    e.classList.toggle("edit");
    e.classList.toggle("save");
    if (e.dataset.action == "edit") {
        $("#subname-" + indx).attr('contenteditable', true);
        e.dataset.action = "save";
    }
    else{
        fetch('/edit/sub_category/' + sub_id, {
            method: 'PUT',
            headers: {
                'Content-type': 'application/json; charset=UTF-8'
            },
            body: JSON.stringify({ name: $("#subname-" + indx).text() })
        }).then(response => response.json()).then(function (res) {
            if (res.message == "success") {
                $("#subname-" + indx).attr('contenteditable', false);
                e.dataset.action = "edit";
            }
            else {
                alert("something went wrong");
            }
        });
    }
}

//delete sub category
function delete_sub_cat(indx,sub_id, name) {
    var userChoice=confirm("are you sure you want to delete this sub category: "+name);
    if(!userChoice) return false;
    fetch('/delete/sub_category/' + sub_id, {
        method: 'DELETE',
        headers: {
            'Content-type': 'application/json; charset=UTF-8'
        }
    }).then(response => response.json()).then(function (res) {
        if (res.message == "success") {
$("#tr_sub_"+indx).hide("slow");
        }
        else {
            alert("something went wrong");
        }
    });
}