(function($){


    function wkfe_template_dialog_message_func(param) {
        $("#wkfe-ld-template-dialog-message").dialog({
            title: param.title,
            modal: true,
            draggable: false,
            width: 600,
            show: {
                effect: "slideDown",
                duration: 300
            },
            hide: {
                effect: "slideUp",
                duration: 300
            },
            buttons: [
                {
                    text: param.button_title,
                    click: function() {
                        $(this).find(".wkfe-ld-template-modal").attr('data-in-elementor','1');
                        $(this).find('form.wkfe-ld-template-modal').trigger('submit');
                    }
                },
                {
                    text: "Save Template",
                    class: 'button button-primary',
                    click: function() {
                        $(this).find('form.wkfe-ld-template-modal').trigger('submit');
                    }
                }
            ]
        });
    }

    $(".page-title-action").on("click",function(event){
        let param = {
            title: 'Create New Template',
            button_title: 'Create Template'
        }
        wkfe_template_dialog_message_func(param);
        event.preventDefault();
        
    });

    $(".wkfe-ld-template-modal").on("submit",function(event){
        event.preventDefault();
        let $title = $(this).find('#ld_course_name');
        let $course_id = $(this).find('#ld_course_id');
        let $site_admin_url = $(this).data('site-admin-url');
        let $activation = $(this).find('#wkfe_ld_template_val');
        let $formid = $(this).data('form-id');
        let $inelementor = $(this).data('in-elementor');
        $.ajax({
            url: wkfe_ld_template_modal_obj.admin_url,
            type: 'POST',
            data: {
                action: 'wkfe_ld_template_save_data',
                title: $title.val(),
                course_id: $course_id.val(),
                activation: $activation.is(':checked') ? 'active' : '',
                security: wkfe_ld_template_modal_obj.security,
                formid: $formid,
                inelementor: $inelementor
            },
            success: function(response) {
                if( response.data.inelementor == '1' ) {
                    window.location.href = $site_admin_url + '?post=' + response.data.postid + '&action=elementor';
                } else {
                    location.reload();
                }
            }
        });
    });

    $(".row-actions .edit a").on("click",function(event){
        event.preventDefault();
        let $title = $("#wkfe-ld-template-dialog-message").find('#ld_course_name');
        let $course_id = $("#wkfe-ld-template-dialog-message").find('#ld_course_id');
        $title.val('');
        $course_id.val('');

        let $formid = $(this).parents('.column-title').find('.hidden').attr('id').split('_')[1];
        
        
        $("#wkfe-ld-template-dialog-message").find('.wkfe-ld-template-modal').attr('data-form-id',$formid);

        $.ajax({
            url: wkfe_ld_template_modal_obj.admin_url,
            type: 'POST',
            data: {
                action: 'wkfe_ld_template_get_data',
                formid: $formid
            },
            success: function(response) {
                $("#wkfe-ld-template-dialog-message .wkfe-ld-template-modal").find('#ld_course_name').val(response.data.title);
                $("#wkfe-ld-template-dialog-message .wkfe-ld-template-modal").find('#ld_course_id').val(response.data.course_id);
                let $activation = response.data.activation;
                
                if( $activation ) {
                    $("#wkfe-ld-template-dialog-message .wkfe-ld-template-modal").find('#wkfe_ld_template_val').attr('checked','checked');
                }

                let param = {
                    title: 'Update Template',
                    button_title: 'Edit Template'
                }
                wkfe_template_dialog_message_func(param);
            }
        });
    });

})(jQuery);