jQuery(document).ready(function($){
    
    $('#boostify-generate-post').on('submit', function(e) {

        let formData = {
            topic: $("#post-topic").val(),
            max_word_count: parseInt( $("#post-word-count").val() ),
            keywords: $("#post-keyword").val(),
            post_date: $("#post-date").val(),
            number_of_posts: parseInt( $("#post-quantity").val() ),
            meta_tags : $("#include-meta-tags").is(":checked")
        };

      
        $.ajax({
            beforeSend: function() {
                $('#boostify-spinner').addClass('is-active');
            },
            url: boosyify_vars.ajaxurl,
            type: 'post',
            data: {
                action :'boostify_generate_content',
                data   : formData,
                nonce  : boosyify_vars.nonce
            },
            success: function( response ) {
              
                $.each( response.posts, function( key, post ) {
                  //  console.log(post);
                    let link = '<a href="'+post.link+'" target="_blank" >Edit</a>'
                    $('#boostify-posts tbody').append('<tr><td>'+post.title+'</td><td>'+link+'</td></tr>');
                });
                $('#boostify-posts').show();
            },
            complete: function( message ){
                $('#boostify-spinner').removeClass('is-active');
                console.log(message);
            }
        });

        e.preventDefault();
    });

    $('#boostify-schedule').on('submit', function(e) {
        $.ajax({
            beforeSend: function() {
                $('#boostify-spinner').addClass('is-active');
            },
            url: boosyify_vars.ajaxurl,
            type: 'post',
            data: {
                action :'boostify_save_schedule',
                topics : $("#boostify_post_topics").val(),
                keywords : $("#boostify_post_keywords").val(),
                quantity: $('#boostify_post_count').val(), 
                duration: $('#boostify_post_duration').val(), 
                trustAI: ($("#boostify_trust_ai").is(":checked") == true)?1:0,
                image: ($("#boostify_post_image").is(":checked") == true)?1:0,
                imagePosition: $('#boostify_post_image_position').val(),
                nonce  : boosyify_vars.nonce
            },
            success: function( response ) {
                
                $('.boostify-setting-notice').append('<div id="setting-error-settings_updated" class="notice notice-success settings-error is-dismissible"> <p><strong>Settings saved.</strong></p><button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button></div>').fadeOut(5000);
                $('#boostify-manual-run').show();
            },
            complete: function( message ){
                $('#boostify-spinner').removeClass('is-active');
               
            }
        });
        e.preventDefault();
    });

    /**
     * Show/Hide image position Input.
     */
    $('#boostify_post_image').on('change',function(){

        let includeImage = $(this).is(":checked");
        if( includeImage ){
            $('#boostify_post_image_position').closest('tr').show()
        }else{
            $('#boostify_post_image_position').closest('tr').hide()
        }

    }).change();

    $('#boostify-manual-run').on('click',function(){
        console.log('Run manually');
        $.ajax({
            beforeSend: function() {
                $('#boostify-spinner').addClass('is-active');
            },
            url: boosyify_vars.ajaxurl,
            type: 'post',
            data: {
                action :'boostify_run_schedule',
                nonce  : boosyify_vars.nonce
            },
            success: function( response ) {
                console.log(response.message);
                $('.boostify-setting-notice').append('<div id="setting-error-settings_updated" class="notice notice-success settings-error is-dismissible"> <p><strong>'+response.message+'.</strong></p><button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button></div>').fadeOut(5000);
            },
            complete: function( message ){
                $('#boostify-spinner').removeClass('is-active');
               
            }
        });
    });

    function checkSettings(){

        if(  $("#boostify_post_topics").val() == '' ){
            return ;
        }

        if( $("#boostify_post_keywords").val() == '' ){
            return;
        }

        if( $('#boostify_post_duration').val() == ''){
            return ;
        }
        
        if( $('#boostify_post_count').val() == '' ){
            return ;
        }

        $('#boostify-manual-run').show();
    }
    
    checkSettings();
});