// for dashboard start
jQuery(document).ready(function() {
    jQuery('#Dashboard').show();

    if (jQuery(".dash")[0]) { //if dash classs exist

        jQuery(".dash").addClass("active"); //add active class to dash
    }
    //style for header-footer and modal
    function EPressAdminonHashChange() {
        var hash = window.location.hash;
        if (hash) {
            // using ES6 template string syntax
            jQuery(`${hash}-tab`).trigger('click');
        }
    }
    EPressAdminonHashChange();

    jQuery('#epress-admin-settings-form').on('submit', function(e) {
        var form = $(this);
        var btn = form.find('.epress-admin-settings-form-submit');
        var formdata = form.serialize();
        form.addClass('is-loading');
        btn.attr("disabled", true);

        $.post(ajaxurl + '?action=epress_admin_action', formdata, function(data) {
            form.removeClass('is-loading');
            btn.removeAttr("disabled");
            show_header_footer_menu();
        });

        e.preventDefault();
    });

    jQuery('.epress-content-type-pro ').on('click', function(e) {
        e.preventDefault();
    });

    // only for header footer module
    function show_header_footer_menu() {
        var checked = $('#epress-admin-switch__module__list____header-footer').prop('checked');
        var menu_html = $('#elementpress-template-admin-menu').html();
        var menu_parent = $('#toplevel_page_elementpress .wp-submenu');
        var menu_item = menu_parent.find('a[href="edit.php?post_type=elementpresstemplate"]');

        if (checked == true) {
            if (menu_item.length > 0 || menu_parent.attr('item-added') == 'y') {
                menu_item.parent().show();
            } else {
                menu_parent.find('li.wp-first-item').after(menu_html);
                menu_parent.attr('item-added', 'y');
            }
        } else {
            menu_item.parent().hide();
        }
    };

    jQuery('#elementpress_go_pro_modal').on('show.bs.modal', function() {
        var myModal = $(this);
        clearTimeout(myModal.data('hideInterval'));
        myModal.data('hideInterval', setTimeout(function() {
            myModal.find('.close').click();
        }, 5000));
    }); //style for header-footer and modal end
}); //ready end 

function openDashboardTab(evt, dashboardTabid) { //function for toogle dashboard tabs
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("em-tablinks");
    for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");
    }
    document.getElementById(dashboardTabid).style.display = "block";
    evt.currentTarget.className += " active";
}

function showDashboardTabClass() {
    document.getElementById("em-tabs-btn").classList.toggle("show");
}

window.onclick = function(event) {
        if (!event.target.matches('.em-dropbtn')) {
            var dropdowns = document.getElementsByClassName("em-menu-btn");
            var i;
            for (i = 0; i < dropdowns.length; i++) {
                var openDropdown = dropdowns[i];
                if (openDropdown.classList.contains('show')) {
                    openDropdown.classList.remove('show');
                }
            }
        }
    }
    // for dashboard end