jQuery(document).ready(function($) {
    "use strict";
console.log("There");
    $(".elementor-tab-control-advanced elementor-active a").attr('href','javascript:void(0)');

    $('.row-actions .edit a, .page-title-action, .column-title .row-title').on('click', function(e) {
        e.preventDefault();
        var id = 0;
        var modal = $('#elementpress_headerfooter_modal');
        var parent = $(this).parents('.column-title');

        modal.addClass('loading');
        modal.modal('show');
        if (parent.length > 0) {
            id = parent.find('.hidden').attr('id').split('_')[1];

            $.get(window.elementpress.resturl + 'my-template/get/' + id, function(data) {
                ElementPress_Template_Editor(data);
                modal.removeClass('loading');
            });
        } else {
            var data = {
                title: '',
                type: 'header',
                condition_a: 'entire_site',
                condition_singular: 'all',
                activation: '',
            };
            ElementPress_Template_Editor(data);
            modal.removeClass('loading');
        }

        modal.find('form').attr('data-epress-id', id);
    });

    $('.epress-template-modalinput-type').on('change', function() {
        var type = $(this).val();
        var inputs = $('.epress-template-headerfooter-option-container');
        var inputs1 = $('.epress-template-modalinput-condition_singular-container');
        

        if (type == 'section') {
            inputs.hide();
        } else {
            inputs.show();
        }
        if (type =='header'){
               $('.epress-template-modalinput-condition_a').on('change', function() {
                var condition_a = $(this).val();
                var inputs22 = $('.epress-template-modalinput-condition_singular-container');

                if (condition_a == 'singular') {
                    inputs22.show();
                } else {
                    inputs22.hide();
                }
            });
        }
        else if (type =='footer'){
            $('.epress-template-modalinput-condition_a').on('change', function() {
                var condition_a = $(this).val();
                var inputs22 = $('.epress-template-modalinput-condition_singular-container');

                if (condition_a == 'singular') {
                    inputs22.show();
                } else {
                    inputs22.hide();
                }
            });
        }
        if(type == 'page_404'){
            console.log('in 404 if');
            inputs1.hide();
            $('.epress-template-modalinput-condition_a').find('option:eq(0)').attr('selected','selected');
            $('.epress-template-modalinput-condition_a').find('option:eq(1)').hide();
            $('.epress-template-modalinput-condition_a').find('option:eq(2)').hide();
        }

        else if (type =='maintenance'){
            console.log('in maintaince if');
            inputs1.hide();
            $('.epress-template-modalinput-condition_a').find('option:eq(0)').attr('selected','selected');
            $('.epress-template-modalinput-condition_a').find('option:eq(1)').hide();
            $('.epress-template-modalinput-condition_a').find('option:eq(2)').hide();
        }
   
        else if (type =='archive'){
            console.log('in archive if');
            inputs1.hide();
            $('.epress-template-modalinput-condition_a').find('option:eq(0)').hide();
            $('.epress-template-modalinput-condition_a').find('option:eq(1)').hide();
            $('.epress-template-modalinput-condition_a').find('option:eq(2)').attr('selected','selected');
        }

        else if (type =='single'){
            console.log('in single if');
            inputs1.show();
           
            $('.epress-template-modalinput-condition_a').find('option:eq(0)').hide();
            $('.epress-template-modalinput-condition_a').find('option:eq(1)').attr('selected','selected');
            $('.epress-template-modalinput-condition_a').find('option:eq(2)').hide();
            $('.epress-template-modalinput-condition_singular').find('option:eq(1)').hide(); //hide front  page if singular
            $('.epress-template-modalinput-condition_singular').find('option:eq(2)').hide(); //hide all posts page if singular
            $('.epress-template-modalinput-condition_singular').find('option:eq(3)').hide(); //hide all pages if singular
            $('.epress-template-modalinput-condition_singular').find('option:eq(5)').hide(); //hide 404 page if singular
        }
       
        else if (type =='search'){
            console.log('in search if');
            inputs1.hide();
            $('.epress-template-modalinput-condition_a').find('option:eq(0)').attr('selected','selected');
            $('.epress-template-modalinput-condition_a').find('option:eq(1)').hide();
            $('.epress-template-modalinput-condition_a').find('option:eq(2)').hide();
        }
        else if (type =='blog'){
            console.log('in blog if');
            inputs1.hide();
            $('.epress-template-modalinput-condition_a').find('option:eq(0)').attr('selected','selected');
            $('.epress-template-modalinput-condition_a').find('option:eq(1)').hide();
            $('.epress-template-modalinput-condition_a').find('option:eq(2)').hide();
        }
        else {
            console.log('in  else');
            inputs1.hide();
            $('.epress-template-modalinput-condition_a').find('option:eq(0)').show();
            $('.epress-template-modalinput-condition_a').find('option:eq(1)').show();
            $('.epress-template-modalinput-condition_a').find('option:eq(2)').show();
            $('.epress-template-modalinput-condition_singular').find('option:eq(1)').show(); //show front  page 
            $('.epress-template-modalinput-condition_singular').find('option:eq(2)').show(); //show all posts page 
            $('.epress-template-modalinput-condition_singular').find('option:eq(3)').show(); //show all pages 
            $('.epress-template-modalinput-condition_singular').find('option:eq(5)').show(); //show 404 page else
  
        }
    });


    // $('.epress-template-modalinput-condition_a').on('change', function() {
    //     var condition_a = $(this).val();
    //     var inputs = $('.epress-template-modalinput-condition_singular-container');

    //     if (condition_a == 'singular') {
    //         inputs.show();
    //     } else {
    //         inputs.hide();
    //     }
    // });

    $('.epress-template-modalinput-condition_singular').on('change', function() {
        var condition_singular = $(this).val();
        var inputs = $('.epress-template-modalinput-condition_singular_id-container');

        if (condition_singular == 'selective') {
            inputs.show();
        } else {
            inputs.hide();
        }
    });


    $('.elementpress-template-save-btn-editor').on('click', function() {

        var form = $('#elementpress-template-modalinput-form');
        form.attr('data-open-editor', '1');
        form.trigger('submit');
    });

    $('#elementpress-template-modalinput-form').on('submit', function(e) {
        e.preventDefault();
        var modal = $('#elementpress_headerfooter_modal');
        modal.addClass('loading');

        var form_data = $(this).serialize();
        var id = $(this).attr('data-epress-id');
        var open_editor = $(this).attr('data-open-editor');
        var admin_url = $(this).attr('data-editor-url');

        console.log(id);

        $.get(window.elementpress.resturl + 'my-template/update/' + id, form_data, function(output) {

          
            //console.log(output);
            modal.removeClass('loading');

            // set list table data
            var row = $('#post-' + output.data.id);

            //console.log("row-length".row);

            if (row.length > 0) {
                row.find('.column-type')
                    .html(output.data.type_html);

                row.find('.column-condition')
                    .html(output.data.cond_text);

                row.find('.row-title')
                    .html(output.data.title)
                    .attr('aria-label', output.data.title);

                 console.log("output data IN CONDITION");
            }
            console.log('output data',output.data.id);

            if (open_editor == '1') {
                window.location.href = admin_url +'post.php'+ '?post=' + output.data.id + '&action=elementor';
            } else if (id == '0') {
                location.reload();
            }
            console.log("id is", output.data.id);
        });

    });

    $('.epress-template-modalinput-condition_singular_id').select2({
    
        ajax: {
            url: window.elementpress.resturl + 'ajaxselect2/singular_list',
            dataType: 'json',
            data: function(params) {
                console.log('params',params);
                var query = {
                    s: params.term,
                }
                return query;
            }
        },
        cache: true,
        placeholder: "--",
        dropdownParent: $('#elementpress_headerfooter_modal_body')
            //minimumInputLength: 2,
    });

    function ElementPress_Template_Editor(data) {


        $('.epress-template-modalinput-title').val(data.title);
        $('.epress-template-modalinput-condition_a').val(data.condition_a);
        $('.epress-template-modalinput-condition_singular').val(data.condition_singular);
        $('.epress-template-modalinput-condition_singular_id').val(data.condition_singular_id);
        $('.epress-template-modalinput-type').val(data.type);

        var activation_input = $('.epress-template-modalinput-activition');
        if (data.activation == 'yes') {
            activation_input.attr('checked', true);
        } else {
            activation_input.removeAttr('checked');
        }

        $('.epress-template-modalinput-activition, .epress-template-modalinput-type, .epress-template-modalinput-condition_a, .epress-template-modalinput-condition_singular')
            .trigger('change');

        var el = $('.epress-template-modalinput-condition_singular_id');
        $.ajax({
            url: window.elementpress.resturl + 'ajaxselect2/singular_list',
            dataType: 'json',
            data: {
                ids: String(data.condition_singular_id)
            }
        }).then(function(data) {
        

            if (data !== null && data.results.length > 0) {
                el.html(' ');
                $.each(data.results, function(i, v) {
                    var option = new Option(v.text, v.id, true, true);
                    el.append(option).trigger('change');
                });
                el.trigger({
                    type: 'select2:select',
                    params: {
                        data: data
                    }
                });
            }
        });
    }






    function epress_url_replace_param(url, paramName, paramValue) {
        if (paramValue == null) {
            paramValue = '';
        }
        var pattern = new RegExp('\\b(' + paramName + '=).*?(&|#|$)');
        if (url.search(pattern) >= 0) {
            return url.replace(pattern, '$1' + paramValue + '$2');
        }
        url = url.replace(/[?#]$/, '');
        return url + (url.indexOf('?') > 0 ? '&' : '?') + paramName + '=' + paramValue;
    }

    var tab_container = $('.wp-header-end');
    var tabs = '';
    var xs_types = {
        'all': 'All',
        'header': 'Header',
        'footer': 'Footer',
        'section': 'Section',
        'single':'Single',
        'archive':'Archive',
        'page_404':'404',
        'maintenance':'Under Maintenance',
        'search':'Search',
        'blog':'Blog Page',
        'search':'Search',
        'blog':'Blog Page'
    };
    var url = new URL(window.location.href);
    var s = url.searchParams.get("elementpress_type_filter");
    s = (s == null) ? 'all' : s;

    console.log('xs types',xs_types);

    $.each(xs_types, function(k, v) {
        var url = epress_url_replace_param(window.location.href, 'elementpress_type_filter', k);
        var klass = (s == k) ? 'elementpress_type_filter_active nav-tab-active' : ' ';
        tabs += `
            <a href="${url}" class="${klass} elementpress_type_filter_tab_item nav-tab">${v}</a>
        `;
        tabs += "\n";
    });
    tab_container.after('<div class="elementpress_type_filter_tab_container nav-tab-wrapper">' + tabs + '</div><br/>');
});