var purchaseQuantity = 1
var areaId = 0

function init(skuHistoricalPrice, skuPrice) {
    var priceInput = document.getElementById("irpc-input")
    priceInput.addEventListener("input", (e) => {
        purchaseQuantity = e.target.value
        if (purchaseQuantity <= 0) {
            purchaseQuantity = 1
        }
        if (purchaseQuantity > 999) {
            purchaseQuantity = 999
        }

    })


    var pchp = document.getElementById("price-calc-historical-price")
    var pcp = document.getElementById("price-calc-price")


    setInterval(() => {
        priceInput.value = purchaseQuantity
        //计算价格
        if (pchp) {
            pchp.innerText = (skuHistoricalPrice * purchaseQuantity).toFixed(2)
        }
        if (pcp) {
            pcp.innerText = (skuPrice * purchaseQuantity).toFixed(2)
        }
    }, 100)
}

// 购买数量减
function purchaseQuantityAdd() {
    if (purchaseQuantity >= 999) {
        purchaseQuantity = 999
        return
    }
    purchaseQuantity++;
}

// 购买数量减
function purchaseQuantityMinus() {
    if (purchaseQuantity <= 1) {
        purchaseQuantity = 1
        return
    }
    purchaseQuantity--;
}

function GetAreaById(areaId) {
    let areaEles = document.getElementsByClassName("info-left-select-btn")
    for (let i = 0; i < areaEles.length; i++) {
        if (areaEles[i].getAttribute("areaId") == areaId) {
            areaEles[i].style.backgroundColor = "rgb(199, 199, 199)"
        } else {
            areaEles[i].style.backgroundColor = ""
        }
    }


    console.log(areaId)
    let eles = document.getElementsByClassName("info-left-select-param-btn");
    for (let i = 0; i < eles.length; i++) {
        if (eles[i].getAttribute("areaId") == areaId) {
            eles[i].style = ""
        } else {
            eles[i].style.display = "none"
        }
    }
    return areaId
}


function RouterJumpId(id) {
    window.location.href = "?skuId=" + id
}

//TODO a
function orderSubmit() {

    const data = {
        skuId: skuData.id,
        count: purchaseQuantity,
        //优惠券
        couponUserId: 0,
        //联系信息
        contactInformation: ""
    };

    fetch('/api/order/submit', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(data)
    })
        .then(response => response.json())
        .then(data => {
            console.log('Success:', data);
            if (data.code === 200) {
                let orderAmount = document.getElementById("order-amount")
                let orderPrice = document.getElementById("order-price")
                let orderHistoricalPrice = document.getElementById("order-historical-price")
                let orderCount = document.getElementById("order-count")

                let orderBuyCurtain = document.getElementById("order-buy-curtain")
                orderBuyCurtain.style.display = ""


                orderCount.innerText = purchaseQuantity
                orderPrice.innerText = (skuPrice * purchaseQuantity).toFixed(2)
                orderAmount.innerText = (skuPrice * purchaseQuantity).toFixed(2)
                orderHistoricalPrice.innerText = (skuHistoricalPrice * purchaseQuantity).toFixed(2)

            }
            alert(data.msg);
        })
        .catch((error) => {
            console.error('Error:', error);
            alert("购买失败");
        });
}

let selectIndex = 0;
let nowSku = {Price: 100, SkuImage: "path/to/image", SkuName: "SKU 名称", HistoricalPrices: 120}; // Example data
let count = 1;
let goodsName = "商品名称"; // Example data

function closeView() {
    document.querySelector(".order-buy-curtain").style.display = "none";
}

function selectPayMethod(index) {
    selectIndex = index;
    document.querySelectorAll(".order-buy-pay-item").forEach((item, idx) => {
        item.style.backgroundColor = idx === index ? "#cccccc" : "#dddddd";
    });
    updatePaySelectPosition();
    document.getElementById("test-purchase").style.display = index === 2 ? "flex" : "none";
}

function updatePaySelectPosition() {
    const select = document.getElementById("pay-select");
    const wxColor = "rgba(0, 168, 242, 1)";
    const zfbColor = "rgba(1, 183, 0, 1)";
    const pyColor = "rgba(234, 98, 0, 1)";
    let bc = "";
    switch (selectIndex) {
        case 0:
            bc = wxColor;
            break;
        case 1:
            bc = zfbColor;
            break;
        case 2:
            bc = pyColor;
            break;
    }
    select.style.left = 10 + (selectIndex * (10 + 340 / 3)) + "px";
    select.style.borderColor = bc;
}

function orderTest() {
    console.log("Test order submitted");
    alert("假装购买成功");
}

document.addEventListener("DOMContentLoaded", function () {
    selectPayMethod(0); // Set default selection
});

