<!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>
</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: ['ca05f29a-758a-4252-9dbe-ab3a00b6eb83'],
        //         openElements: ''
        //     }, function (api) {
        // 
        //     }, {
        //         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) {
                        // Mews.Distributor.setVoucherCode(discountCode);
                        // alert('Discount code applied successfully!');
                        Mews.Distributor({
                            configurationIds: ['ca05f29a-758a-4252-9dbe-ab3a00b6eb83'],
                            openElements: '.distributor-open'
                        }, function (api) {

                            api.open();
                        }, {
                            dataBaseUrl: 'https://api.mews-demo.com'  // Points to Demo environment
                        });
                    } else {
                        alert('Invalid discount code!');
                    }
                })
                .catch(error => {
                    console.error('Error validating discount code:', error);
                    alert('Failed to validate discount code.');
                });
        }
    </script>
</body>

</html>