{"size": 4135, "ext": "js", "max_stars_repo_path": "public/javascript/add-order-detail.js", "max_stars_repo_name": "JHallUofA/b2b-inventory", "max_stars_repo_stars_event_min_datetime": "2021-09-15T03:26:17.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-15T03:26:17.000Z", "max_issues_repo_path": "public/javascript/add-order-detail.js", "max_issues_repo_name": "zdensamson/b2b-inventory", "max_issues_repo_issues_event_min_datetime": "2021-09-21T15:11:52.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-21T15:40:35.000Z", "max_forks_repo_path": "public/javascript/add-order-detail.js", "max_forks_repo_name": "zdensamson/b2b-inventory", "max_forks_repo_forks_event_min_datetime": "2021-09-26T23:40:20.000Z", "max_forks_repo_forks_event_max_datetime": "2021-09-26T23:40:20.000Z"}
{"max_stars_count": 1, "max_issues_count": 2, "max_forks_count": 1, "avg_line_length": 30.6296296296, "max_line_length": 80, "alphanum_fraction": 0.6403869407}
const lineForm = document.getElementById('details');
const postArray = [];
const custId = document.getElementById('cust-id').innerHTML;
const orderId = document.getElementById('order-id').innerHTML;
const warningDivEl = document.getElementById('warning');
var filtCustId = custId.replace(/\D/g, "");
var filtOrderId = orderId.replace(/\D/g, "");

async function grabProducts() {
    warningDivEl.innerHTML = "";
    const response = await fetch(`/api/products`, {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json'
        }
    })
    if (response.ok) {
        const data = await response.json();
        createline(data);
    } else {
        alert(response.statusText);

    }
}

function createline(prodObj) {
    var formGroup = document.createElement('div');
    formGroup.classList.add('form-group');

    var labelEl = document.createElement('label');
    labelEl.setAttribute('for', 'select-prod');
    labelEl.classList.add('line-label')
    labelEl.innerHTML = "Select product";

    var lineEl = document.createElement('select');
    lineEl.classList.add('prod-select');
    lineEl.setAttribute('id', 'select-prod');

    var inputQtyEl = document.createElement('input');
    inputQtyEl.setAttribute('type', 'number');
    inputQtyEl.setAttribute('min', '1');
    inputQtyEl.setAttribute('max', '1000');
    inputQtyEl.classList.add('qty-input');

    prodObj.forEach(prod => {
        var dropDownEl = document.createElement('option');
        dropDownEl.setAttribute('value', `${prod.id}`);
        dropDownEl.text = `${prod.product_name}`
        lineEl.appendChild(dropDownEl);
    })

    formGroup.appendChild(labelEl);
    formGroup.appendChild(lineEl);
    formGroup.appendChild(inputQtyEl);
    lineForm.appendChild(formGroup);
}

function deleteLine() {
    if (lineForm.firstChild) {
        lineForm.removeChild(lineForm.lastChild);
    } else {
        warningDivEl.innerHTML = "No line to remove"
    }

}

function postDetails() {
    var orderDetailObj = {};
    const postArray = [];
    const selectElArray = document.getElementsByTagName('select');
    const inputElArray = document.getElementsByTagName('input');
    const filtSelectEl = [];
    const filtInputEl = [];

    for (i = 0; i < selectElArray.length; i++) {
        filtSelectEl.push(selectElArray[i].value);
        filtInputEl.push(inputElArray[i].value);
    }

    // product ID array
    console.log(filtSelectEl);
    // product QTY array
    console.log(filtInputEl);
    // customer ID
    console.log(filtCustId);
    // order ID
    console.log(filtOrderId);

    const doesArrayHaveDuplicates = filtSelectEl.some(
        (val, i) => filtSelectEl.indexOf(val) !== i
    )

    if (filtInputEl.includes('') == true) {
        console.log("THERE IS A NULL");
        warningDivEl.innerHTML = "Please ensure all quantities have a value"
    } else if (!filtInputEl.length) {
        warningDivEl.innerHTML = "Please add at least ONE line to this order"
    } else if (doesArrayHaveDuplicates) {
        warningDivEl.innerHTML = "Please ensure the order lines are unique"
    } else {
        for (i = 0; i < selectElArray.length; i++) {
            orderDetailObj = {
                order_id: parseInt(filtOrderId),
                customer_id: parseInt(filtCustId),
                product_id: parseInt(filtSelectEl[i]),
                quantity: parseInt(filtInputEl[i])
            }
            postArray.push(orderDetailObj);
        }

        bulkPost(postArray);
    }


}


async function bulkPost(postArray) {
    const response = await fetch(`/api/orderDetails/bulk`, {
        method: 'post',
        body: JSON.stringify(postArray),
        headers: {
            'Content-Type': 'application/json'
        }
    })
    if (response.ok) {
        document.location.replace('/dashboard')
    } else {
        alert(response.statusText);
    }
}

document.getElementById('remove-line').addEventListener('click', deleteLine);
document.getElementById('add-line').addEventListener('click', grabProducts);
document.getElementById('submit-detail').addEventListener('click', postDetails);