$(function () {
    setTimeout(function () {
        // initSlider();
    }, 0);


    $(".slink").click(function (e) {
        e.preventDefault();
        var aid = $(this).attr("href");
        $('html,body').animate({
            scrollTop: $(aid).offset().top
        }, 'slow');
    });


    $('body').on('click', '.close-popup', function (e) {
        e.preventDefault();
        $('.banner-popup').remove();
    });


    $('body').on('click', '.header .navbar-nav .nav-link', function () {
        $('.header .nav-item').removeClass('active');
        $(this).closest('li').addClass('active');
    });

    $('body').on('click', 'button.navbar-toggler', function () {
        $('body').toggleClass('menu-show');
    });
    $('body').on('click', '.header .hide-menu', function () {
        $('body').toggleClass('menu-show');
        $('.header .navbar-collapse').toggleClass('show collapse');
    });

    $('.input-datepicker').datepicker({
        format: 'yyyy-mm-dd',
        autoclose: true,
    });


    $('.img-popup').magnificPopup({
        delegate: 'a', // child items selector, by clicking on it popup will open
        type: 'image',
        gallery: {
            enabled: true
        },
        // other options
    });

    active = $('.dropdown-menu .current-menu-item');
    if (active.length) {
        active.closest('.dropdown-menu').parent().addClass('current-menu-item active');
    }

    initSlider();


    $('body').on('change', '[name="place"]', function () {
        $('.place-view').addClass('d-none');
        $('#b-' + $(this).val()).removeClass('d-none');
    });

    $('body').on('click', 'button.send', function () {
        e = $(this);
        e.prop('disabled', true);
        e.html('กำลังส่งข้อมูล');
        text = e.text();
        data = getFormData($('#register-form'));
        wp.ajax.post("get_data", data).done(function (response) {
            console.log(response);
            if (response.status == 'error') {
                Swal.fire(
                    'ผิดพลาด',
                    response.message,
                    'error'
                );
            } else {
                Swal.fire(
                    'สำเร็จ',
                    'ส่งข้อมูลแล้ว',
                    'success'
                );
                $('#register-form').find('input:not([type="radio"])').val('');
            }


            e.prop('disabled', false);
            e.text(text);
        });
    });

});

function getFormData($form) {
    var unindexed_array = $form.serializeArray();
    var indexed_array = {};

    $.map(unindexed_array, function (n, i) {
        indexed_array[n['name']] = n['value'];
    });

    return indexed_array;
}



function initSlider() {


    $('.slider-image .owl-carousel').owlCarousel({
        loop: true,
        singleItem: true,
        autoplay: true,
        autoplayTimeout: 5000,
        autoHeight: true,
        items: 1,
        nav: false,
        dots: true,
    });

}




// When the user scrolls the page, execute myFunction
window.onscroll = function () {
    myFunction()
};

// Get the header
var header = document.getElementById("header");

// Get the offset position of the navbar
var sticky = header.offsetTop;

// Add the sticky class to the header when you reach its scroll position. Remove "sticky" when you leave the scroll position
function myFunction() {
    if (window.pageYOffset > sticky) {
        header.classList.add("sticky");
    } else {
        header.classList.remove("sticky");
    }
}