<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>Processing Purchase...</title>
    <script>
        window.onload = function() {
            var invoiceUrl = '/payment/generateInvoice' +
                '?planName=' + encodeURIComponent('${planName}') +
                '&coverage=' + encodeURIComponent('${coverage}') +
                '&premium=' + encodeURIComponent('${premium}') +
                '&fullName=' + encodeURIComponent('${fullName}') +
                '&dob=' + encodeURIComponent('${dob}') +
                '&address=' + encodeURIComponent('${address}') +
                '&phoneNumber=' + encodeURIComponent('${phoneNumber}') +
                '&emailId=' + encodeURIComponent('${emailId}') +
                '&policyNumber=' + encodeURIComponent('${policyNumber}') +
                '&purchaseDate=' + encodeURIComponent('${purchaseDate}') +
                '&expiryDate=' + encodeURIComponent('${expiryDate}');

            // Open the invoice in a new tab/window
            window.open(invoiceUrl, '_blank'); // '_blank' opens in a new tab/window

            // Redirect to the bought insurances page after a short delay
            // Give the browser a moment to open the new tab/window before redirecting the current one.
            setTimeout(function() {
                window.location.href = '${pageContext.request.contextPath}/viewInsurance';
            }, 1000); // Reduced delay to 1 second, you can adjust as needed.
        };
    </script>
</head>
<body>
    <p>Your purchase is confirmed!</p>
    <p>Your invoice download should start automatically.</p>
    <p>If the download doesn't start, <a href="${pageContext.request.contextPath}/payment/generateInvoice?planName=${planName}&coverage=${coverage}&premium=${premium}&fullName=${fullName}&dob=${dob}&address=${address}&phoneNumber=${phoneNumber}&emailId=${emailId}&policyNumber=${policyNumber}&purchaseDate=${purchaseDate}&expiryDate=${expiryDate}" target="_blank">click here to download manually</a>.</p>
    <p>You will be redirected to your bought insurances page shortly.</p>
</body>
</html>