<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mews Booking Engine with Discount Code</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }

        #bookingContainer {
            margin-top: 20px;
        }

        input,
        button {
            margin-top: 10px;
            padding: 10px;
        }

        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
    </style>
    <script src="https://api.mews-demo.com/distributor/distributor.min.js"></script>
    <!-- Mews BookingEngine -->
    <!-- <script>(function(m,e,w,s){c=m.createElement(e);c.onload=function(){
    Mews.D.apply(null,s)};c.async=1;c.src=w;t=m.getElementsByTagName(e)[0];t.parentNode.insertBefore(c,t);})
    (document,'script','https://app.mews-demo.com/distributor/distributor.min.js',[['ca05f29a-758a-4252-9dbe-ab3a00b6eb83'],'https://app.mews-demo.com']);</script> -->
    <!-- End Mews BookingEngine -->
</head>

<body>
    <input type="text" id="discountCodeInput" placeholder="Enter Discount Code">
    <button onclick="validateAndApplyCode()">Apply Discount</button>

    <div id="bookingContainer">
        <button class="distributor-open">Book Now</button>
    </div>

    <script>
        document.addEventListener("DOMContentLoaded", function () {
            Mews.Distributor({
                configurationIds: ['53871e4e-f099-4a3b-9b86-b15901616683'],
                openElements: '.distributor-open'
            }, function (api) {

                api.open();
            }, {
                dataBaseUrl: 'https://api.mews-demo.com'
            });
        });

        function validateAndApplyCode() {
            const discountCode = document.getElementById('discountCodeInput').value;
            // Call your backend API to validate the discount code
            fetch('http://localhost:14000/api/v1/mews/voucher/validate', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ code: discountCode })
            })
                .then(response => response.json())
                .then(data => {
                    console.log(data);
                    if (data.isValid) {
                        // Apply the voucher code through the Mews Distributor API
                        Mews.Distributor.setVoucherCode(discountCode);
                        alert('Discount code applied successfully!');
                    } else {
                        alert('Invalid discount code!');
                    }
                })
                .catch(error => {
                    console.error('Error validating discount code:', error);
                    alert('Failed to validate discount code.');
                });
        }
    </script>
</body>

</html>