var key = document.getElementById("buyNow").getAttribute("data-key")
var amount = document.getElementById("buyNow").getAttribute("data-amount")
var order_id = document.getElementById("buyNow").getAttribute("data-order-id")

// Place Order
function placeOrder(data) {
    show_Toaster('Placing Order Please Wait', 'success');
    $.ajax({
        url: "/placeOrder",
        method: "POST",
        data,
        processData: false,
        success: function (res) {
            if (res.error) {
                res.error.map((item) => {
                    show_Toaster(item.msg, 'error');
                    setTimeout(() => {
                        window.location.href = `/error`
                    }, 500);
                })
            }
            if (res.success) {
                res.success.map((item) => {
                    show_Toaster(item.msg, 'success');
                    setTimeout(() => {
                        window.location.href = `/success/${res.data.order_id}`
                    }, 500);
                })
            }
        },
    });
}

var options = {
    "key": key,
    "amount": parseInt(amount) * 100,
    "currency": "INR",
    "name": "Darwin Pharmaceuticals",
    "description": "Order Transaction",
    "image": "https://dcloud.co.in/api/files/1667800676_91deeccc3d80d9f386a9.png",
    "order_id": order_id,
    "handler": function (response) {
        var data = `razorpay_order_id=${response.razorpay_order_id}&razorpay_payment_id=${response.razorpay_payment_id}&razorpay_signature=${response.razorpay_signature}`
        $.ajax({
            url: "/validateOrder",
            method: "POST",
            data,
            processData: false,
            success: function (res) {
                if (res.error) {
                    res.error.map((item) => {
                        show_Toaster(item.msg, 'error');
                    })
                }
                if (res.success) {
                    res.success.map((item) => {
                        show_Toaster(item.msg, 'success');
                        var addressID = document.querySelector('input[name="address"]:checked').value
                        var data = `razorpay_order_id=${response.razorpay_order_id}&razorpay_payment_id=${response.razorpay_payment_id}&razorpay_signature=${response.razorpay_signature}&address=${addressID}`
                        placeOrder(data)
                    })
                }
            },
        });
    },
    "prefill": user,
    "theme": {
        "color": "#3399cc"
    }
};
var rzp1 = new Razorpay(options);
rzp1.on('payment.failed', function (response) {
    alert(response.error.code);
    alert(response.error.description);
    alert(response.error.source);
    alert(response.error.step);
    alert(response.error.reason);
    alert(response.error.metadata.order_id);
    alert(response.error.metadata.payment_id);
});
document.getElementById('buyNow').onclick = function (e) {
    e.preventDefault();
    var addressID = document.querySelector('input[name="address"]:checked')
    if (addressID) rzp1.open();
    else show_Toaster('Please Select Address First', 'error');
}
