const type1 = document.querySelector(".btn.singlebet");
const type2 = document.querySelector(".btn.parlaybet");

const single = document.querySelector(".single");
const parlay = document.querySelector(".parlay");

function ardFunction(e) {
    if (e == "1" && type2.className == "btn parlaybet on") {
        type1.classList.add("on");
        type2.classList.remove("on");
        single.classList.add("on");
        parlay.classList.remove("on");
        // single.style.display = "flex";
        // parlay.style.display = "none";
        single.style.opacity = "1";
        parlay.style.opacity = "0";
    } else if (e == "2" && type1.className == "btn singlebet on") {
        type1.classList.remove("on");
        type2.classList.add("on");
        single.classList.remove("on");
        parlay.classList.add("on");
        // single.style.display = "none";
        // parlay.style.display = "flex";
        single.style.opacity = "0";
        parlay.style.opacity = "1";
    }
}

function dropNote(id) {
    if (id.firstElementChild.checked) {
        id.firstElementChild.checked = false;
        id.previousSibling.parentElement.lastElementChild.classList.remove("on");
        id.lastElementChild.classList.remove("on");
    } else if (!id.firstElementChild.checked) {
        id.firstElementChild.checked = true;
        id.previousSibling.parentElement.lastElementChild.classList.add("on");
        id.lastElementChild.classList.add("on");
    }
}

document.addEventListener("DOMContentLoaded", function () {
    if (screen.width > 600) {
        var odsize = document.querySelector(".odds-input");
        var betsize = document.querySelector(".bet-input");
    }
})

document.addEventListener("click", function () {
    var total = document.querySelector(".custom-select.partai .select-selected").innerHTML;
    const field = document.querySelector(".options.field");
    var kids = document.querySelectorAll(".options.child");

    try {
        var jumlah = parseInt(total.slice(0, 2))
        if (Number.isInteger(jumlah) && jumlah != kids.length) {
            for (let i = 0; i < kids.length; i++) {
                kids[i].remove();
            }
            for (let i = 1; i <= jumlah; i++) {
                var partai = "<div class='options child'><div class='lgnd'>ODDS " + i + "</div><div class='btn odds add'></g></g><input id='parlay_odds_1' class='odds-input' type='text' size='18'></div><div class='btn status'><div class='ardx custom-select'><Select><option value='0'>Kemenangan</option><option value='1'>Full Menang</option><option value='2'>Menang 1/2</option><option value='3'>Seri</option><option value='4'>Kalah 1/2</option><option value='5'>Kalah</option></Select></div></div></div>";
                field.innerHTML += partai;
            }
        }
        var ardx, i, j, l, ll, selElmnt, a, b, c;
        ardx = document.getElementsByClassName("ardx custom-select");
        l = ardx.length;
        for (i = 0; i < l; i++) {
            if (ardx[i].children.length < 3) {
                selElmnt = ardx[i].getElementsByTagName("select")[0];
                ll = selElmnt.length;
                a = document.createElement("DIV");
                a.setAttribute("class", "select-selected");
                a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
                ardx[i].appendChild(a);
                b = document.createElement("DIV");
                b.setAttribute("class", "select-items select-hide");
                for (j = 1; j < ll; j++) {
                    c = document.createElement("DIV");
                    c.innerHTML = selElmnt.options[j].innerHTML;
                    c.addEventListener("click", function (e) {
                        var y, i, k, s, h, sl, yl;
                        s = this.parentNode.parentNode.getElementsByTagName("select")[0];
                        sl = s.length;
                        h = this.parentNode.previousSibling;
                        for (i = 0; i < sl; i++) {
                            if (s.options[i].innerHTML == this.innerHTML) {
                                s.selectedIndex = i;
                                h.innerHTML = this.innerHTML;
                                y = this.parentNode.getElementsByClassName("same-as-selected");
                                yl = y.length;
                                for (k = 0; k < yl; k++) {
                                    y[k].removeAttribute("class");
                                }
                                this.setAttribute("class", "same-as-selected");
                                break;
                            }
                        }
                        h.click();
                    });
                    b.appendChild(c);
                }
                ardx[i].appendChild(b);
                var stts = document.querySelectorAll(".options.child .btn.status");
                a.addEventListener("click", function (e) {
                    e.stopPropagation();
                    closeAllSelect(this);
                    this.nextSibling.classList.toggle("select-hide");
                    for (var r = 0; r < stts.length; r++) {
                        if (stts[r] != this.parentElement.parentElement) {
                            stts[r].style.zIndex = 0;
                        }

                    }
                    this.parentElement.parentElement.style.zIndex = 2;
                    this.classList.toggle("select-arrow-active");
                });
            }
        }
    } catch (e) {
    }

})


var x, i, j, l, ll, selElmnt, a, b, c;
x = document.getElementsByClassName("custom-select");
l = x.length;
for (i = 0; i < l; i++) {
    selElmnt = x[i].getElementsByTagName("select")[0];
    ll = selElmnt.length;
    a = document.createElement("DIV");
    a.setAttribute("class", "select-selected");
    a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
    x[i].appendChild(a);
    b = document.createElement("DIV");
    b.setAttribute("class", "select-items select-hide");
    for (j = 1; j < ll; j++) {
        c = document.createElement("DIV");
        c.innerHTML = selElmnt.options[j].innerHTML;
        c.addEventListener("click", function (e) {
            var y, i, k, s, h, sl, yl;
            s = this.parentNode.parentNode.getElementsByTagName("select")[0];
            sl = s.length;
            h = this.parentNode.previousSibling;
            for (i = 0; i < sl; i++) {
                if (s.options[i].innerHTML == this.innerHTML) {
                    s.selectedIndex = i;
                    h.innerHTML = this.innerHTML;
                    y = this.parentNode.getElementsByClassName("same-as-selected");
                    yl = y.length;
                    for (k = 0; k < yl; k++) {
                        y[k].removeAttribute("class");
                    }
                    this.setAttribute("class", "same-as-selected");
                    break;
                }
            }
            h.click();
        });
        b.appendChild(c);
    }
    x[i].appendChild(b);
    a.addEventListener("click", function (e) {
        e.stopPropagation();
        closeAllSelect(this);
        this.nextSibling.classList.toggle("select-hide");
        this.classList.toggle("select-arrow-active");
    });
}

function closeAllSelect(elmnt) {
    var x, y, i, xl, yl, arrNo = [];
    x = document.getElementsByClassName("select-items");
    y = document.getElementsByClassName("select-selected");
    xl = x.length;
    yl = y.length;
    for (i = 0; i < yl; i++) {
        if (elmnt == y[i]) {
            arrNo.push(i)
        } else {
            y[i].classList.remove("select-arrow-active");
        }
    }
    for (i = 0; i < xl; i++) {
        if (arrNo.indexOf(i)) {
            x[i].classList.add("select-hide");
        }
    }
}
document.addEventListener("click", closeAllSelect);

document.addEventListener('click', function () {
var status = document.querySelector('div.select-selected').innerHTML;
try {
    singlebet(status);
} catch (e) {
}
parlaybet();
// try {
// } catch (e) {
// }
});


document.addEventListener('keyup', function () {
var status = document.querySelector('div.select-selected').innerHTML;
try {
    singlebet(status);
} catch (e) {
}
parlaybet();
});


function getKei(n) {
if (Number.isInteger(n)) {
    return 0;
}

const decimalStr = n.toString().split('.')[1];
return Number(decimalStr);
}

function getOdds(n) {
if (Number.isInteger(n)) {
    return 0;
}

const decimalStr = n.toString().split('.')[0];
return Number(decimalStr);
}

function singlebet(status) {

var betField = document.getElementById('single_result_modal');
var profitField = document.getElementById('single_result_win');

var odds = document.getElementById('single_odds').value;
var bet = document.getElementById('single_bet').value;

switch (status) {
    case "Full Menang":
        if (odds < 0) {
            modal = Intl.NumberFormat(["ban", "id"]).format(parseFloat(bet) * Math.abs(odds)).toString();
            result = Intl.NumberFormat(["ban", "id"]).format(parseFloat(bet)).toString();
        } else {
            modal = Intl.NumberFormat(["ban", "id"]).format(parseFloat(bet)).toString();
            result = Intl.NumberFormat(["ban", "id"]).format(Math.abs(odds) * parseFloat(bet)).toString();
        }
        break;
    case "Menang 1/2":
        if (odds < 0) {
            modal = Intl.NumberFormat(["ban", "id"]).format(parseFloat(bet) * Math.abs(odds)).toString();
            result = Intl.NumberFormat(["ban", "id"]).format(parseFloat(bet) / 2).toString();
        } else {
            modal = Intl.NumberFormat(["ban", "id"]).format(parseFloat(bet)).toString();
            result = Intl.NumberFormat(["ban", "id"]).format(parseFloat(bet) / 2 * Math.abs(odds)).toString();
        }
        break;
    case "Seri":
        if (odds < 0) {
            modal = Intl.NumberFormat(["ban", "id"]).format(parseFloat(bet) * Math.abs(odds)).toString();
        } else {
            modal = Intl.NumberFormat(["ban", "id"]).format(parseFloat(bet)).toString();
        }
        result = 0;
        break;
    case "Kalah 1/2":
        if (odds < 0) {
            modal = Intl.NumberFormat(["ban", "id"]).format(parseFloat(bet) / 2 * Math.abs(odds)).toString();
        } else {
            modal = Intl.NumberFormat(["ban", "id"]).format(parseFloat(bet) / 2 ).toString();
        }
        result = 0;
        break;
    case "Kalah":
        if (odds < 0) {
            modal = Intl.NumberFormat(["ban", "id"]).format(parseFloat(bet) * odds).toString();
        } else {
            modal = "-" + Intl.NumberFormat(["ban", "id"]).format(parseFloat(bet)).toString();
        }
        result = 0;
        break;
}

if (modal == "NaN") {
    betField.innerHTML = 0;
} else {
    betField.innerHTML = modal;
}

if (result == "NaN") {
    profitField.innerHTML = 0;
} else {
    profitField.innerHTML = result;
}
}


function parlaybet() {

var profitField = document.getElementById('parlay_result_win');
var oddsAll = document.querySelectorAll('.child .odds-input');
var statuses = document.querySelectorAll('.child .select-selected');

let odds = 1;
for (var i = 0; i < oddsAll.length; i++) {
    if (oddsAll[i].value != "") {
        switch (statuses[i].innerHTML) {
            case "Full Menang":
                if (oddsAll[i].value < 0) {
                    odds *= Math.abs(oddsAll[i].value);
                } else {
                    odds *= oddsAll[i].value;
                }
                break;
            case "Menang 1/2":
                if (oddsAll[i].value < 0) {
                    odds *= ((Math.abs(oddsAll[i].value) - 1) / 2) + 1;
                } else {
                    odds *= ((oddsAll[i].value - 1) / 2) + 1;
                }
                break;
            case "Seri":
                break;
            case "Kalah 1/2":
                odds *= 0.5;
                break;
            case "Kalah":
                odds *= 0;
                break;
            case "Kemenangan":
                break;
        }
    }
}

var bet = document.getElementById('parlay_bet').value;

var result = Intl.NumberFormat(["ban", "id"]).format(parseFloat(bet) * (odds-1)).toString();

if (result == "NaN") {
    profitField.innerHTML = 0;
} else {
    profitField.innerHTML = result;
}
}
var acc = document.getElementsByClassName("accordion");
        var i;

        for (i = 0; i < acc.length; i++) {
        acc[i].addEventListener("click", function() {

        var panel = this.nextElementSibling;
        if (panel.style.maxHeight){
          panel.style.maxHeight = null;
        } else {
          let active = document.querySelectorAll(".accordion-div .accordion.active");
          for(let j = 0; j < active.length; j++){
            active[j].classList.remove("active");
            active[j].nextElementSibling.style.maxHeight = null;
          }
          this.classList.toggle("active");
          panel.style.maxHeight = panel.scrollHeight + "px";
        }
        });
        }