/* 
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Other/javascript.js to edit this template
 */
var ajaxUrl = obj.ajaxurl;
var jq_doobert_admin_appdbrt = jQuery.noConflict();
var email_nonce = obj.email_nonce;
var custom_uploader;
jq_doobert_admin_appdbrt(function () {

    if (jq_doobert_admin_appdbrt('.rl_accordion').length > 0) {
        jq_doobert_admin_appdbrt(document).on('click', '.rl_accordion', function () {
            if (jq_doobert_admin_appdbrt(this).hasClass('rl_active')) {
                jq_doobert_admin_appdbrt(this).removeClass("rl_active");
                jq_doobert_admin_appdbrt(this).closest('.single_faq').find('.rl_panel').hide();
            } else {
                jq_doobert_admin_appdbrt('.rl_accordion').removeClass('rl_active');
                jq_doobert_admin_appdbrt('.rl_panel').hide();
                jq_doobert_admin_appdbrt(this).addClass("rl_active");
                jq_doobert_admin_appdbrt(this).closest('.single_faq').find('.rl_panel').show();
            }
        });
    }

    if (jq_doobert_admin_appdbrt('#is_already_paid_version').length > 0) {
        jq_doobert_admin_appdbrt('.buy_pro_link').remove();
    }

    jq_doobert_admin_appdbrt('#default_options_tag').select2({
        tags: true, // Enable tagging feature
        maximumSelectionLength: 5, // Allow only 5 tags
        placeholder: "Default Options to show",
//        tokenSeparators: [';', ' ']
    });
    jq_doobert_admin_appdbrt('#shelter_ids').select2({
        tags: true, // Enable tagging feature
        placeholder: "Enter Shelter Ids",
        tokenSeparators: [',', ' ']
        
    });
    jq_doobert_admin_appdbrt('#pages_to_exclude').select2();
    jq_doobert_admin_appdbrt('.pages_to_exclude_save').click(function () {
        var selectedValues = jq_doobert_admin_appdbrt('#pages_to_exclude').val();
        jq_doobert_admin_appdbrt.ajax({
            url: ajaxUrl,
            type: 'post',
            data: {
                action: 'doobert_save_excluded_posts',
                email_nonce: email_nonce,
                selectedValues: selectedValues
            },
            success: function (res) {
                jq_doobert_admin_appdbrt('.pages_to_exclude_save').html('Settings Saved!');
                setTimeout(function () {
                    jq_doobert_admin_appdbrt('.pages_to_exclude_save').html('Save Settings');
                }, 1000);
            }
        });
    });


    jq_doobert_admin_appdbrt('.full_select').select2();
    jq_doobert_admin_appdbrt('#language_for_pdf').select2();
     jq_doobert_admin_appdbrt('#adopt_pet_id').on('change', function () {
        var adopt_pet_value = jq_doobert_admin_appdbrt(this).val();
        if (adopt_pet_value == 1) {
            jq_doobert_admin_appdbrt('#shelter_ids').attr('required', true);
            jq_doobert_admin_appdbrt('#shelter_zipcode').attr('required', true);
        } else {
             jq_doobert_admin_appdbrt('#shelter_ids').removeAttr('required', true);
            jq_doobert_admin_appdbrt('#shelter_zipcode').removeAttr('required', true);
        }
    });
   
    jq_doobert_admin_appdbrt('.show').click(function () {
        jq_doobert_admin_appdbrt('.show_hide_icon').addClass('shown');
        jq_doobert_admin_appdbrt('#doobert_api_key').attr('type', 'text');
    });
    jq_doobert_admin_appdbrt('.hide').click(function () {
        jq_doobert_admin_appdbrt('.show_hide_icon').removeClass('shown');
        jq_doobert_admin_appdbrt('#doobert_api_key').attr('type', 'password');
    });
});

var url_to_hite = obj.admin_url;
//alert(url_to_hite);
url_to_hite = url_to_hite + 'admin.php?page=doobert-chatbot-settings&load_csv=1&_wpnonce=' + obj.nonce;



jq_doobert_admin_appdbrt('.verify_token').click(function () {
    var token = jq_doobert_admin_appdbrt('#doobert_authorization_token').val();
    var is_valid = 0;
    if (token && token != '') {
        jq_doobert_admin_appdbrt('.verify_token').html('please wait...');
        token = token.trim();
        if (token && token != '') {
            is_valid = 1;
            jq_doobert_admin_appdbrt.ajax({
                url: ajaxUrl,
                type: 'post',
                data: {
                    action: 'doobert_verify_token',
                    security: obj.security,
                    token: token
                },
                success: function (res) {
                    jq_doobert_admin_appdbrt('.verify_token').html('Verify');
                    if (res == 1) {
                        jq_doobert_admin_appdbrt('.doobert_error').html('');
                        jq_doobert_admin_appdbrt('.token_correct').html('Token Verified Successfully');
                        setTimeout(function () {
                            location.href = url_to_hite;
                        }, 1000);
                    } else {
                        jq_doobert_admin_appdbrt('.doobert_error').html('Invalid token');
                        setTimeout(function () {
                            jq_doobert_admin_appdbrt('.doobert_error').html('');
                        }, 2000);
                    }
                }
            });
        }
    }

    if (is_valid == 0) {
        jq_doobert_admin_appdbrt('.doobert_error').html('Enter Token to continue');
        setTimeout(function () {
            jq_doobert_admin_appdbrt('.doobert_error').html('');
        }, 2000);
    }

});

jq_doobert_admin_appdbrt('.doobert_petcolove_setting_save').click(function () {
    var petco_love = jq_doobert_admin_appdbrt('#petco_love_id').val();
    jq_doobert_admin_appdbrt.ajax({
        url: ajaxUrl,
        type: 'post',
        data: {
            action: 'doobert_petcolove_setting_save',
            email_nonce: email_nonce,
            petco_love: petco_love
        },
        success: function (res) {
            if (res == 0) {
                alert('Some error during save');
            } else {
                jq_doobert_admin_appdbrt('.doobert_petcolove_setting_save').val('Saved!');
                setTimeout(function () {
                jq_doobert_admin_appdbrt('.doobert_petcolove_setting_save').val('Save');
            }, 1000);
            }
        }
    });
});
jq_doobert_admin_appdbrt('.doobert_adopt_setting_save').click(function () {
    var shelter_zipcode = jq_doobert_admin_appdbrt('#shelter_zipcode').val();
    var shelter_ids = jq_doobert_admin_appdbrt('#shelter_ids').val();
    var adopt_image = jq_doobert_admin_appdbrt('#adopt_image').val();
    var adopt_pet = jq_doobert_admin_appdbrt('#adopt_pet_id').val();
    if (shelter_zipcode == '' && adopt_pet==1) {
         alert('Enter Zipcode!');
    }
    else if (shelter_ids == '' && adopt_pet==1) {
         alert('Enter Shelter Ids!');
    }
    else
    {
        jq_doobert_admin_appdbrt.ajax({
            url: ajaxUrl,
            type: 'post',
            data: {
                action: 'doobert_adopt_setting_save',
                email_nonce: email_nonce,
                security: obj.security,
                shelter_zipcode: shelter_zipcode,
                shelter_ids: shelter_ids,
                adopt_image: adopt_image,
                adopt_pet:adopt_pet
            },
            success: function (res) {
                if (res == 0) {
                    alert('Some error during save');
                } else {
                    jq_doobert_admin_appdbrt('.doobert_adopt_setting_save').val('Saved!');
                    setTimeout(function () {
                    jq_doobert_admin_appdbrt('.doobert_adopt_setting_save').val('Save');
                }, 1000);
                }
            }
        });
    }
});
jq_doobert_admin_appdbrt('.doobert_monthly_audit_email_save').click(function () {
    var email = jq_doobert_admin_appdbrt('#doobert_monthly_audit_email').val();
    if (email && email != '') {
        jq_doobert_admin_appdbrt.ajax({
            url: ajaxUrl,
            type: 'post',
            data: {
                action: 'doobert_monthly_audit_email_save',
                email_nonce: email_nonce,
                email: email
            },
            success: function (res) {
                if (res == 0) {
                    alert('Some error during save');
                } else {
                    jq_doobert_admin_appdbrt('.doobert_monthly_audit_email_save').text('Saved!');
                }
            }
        });
    } else {
        alert('Enter Email!');
    }
});

jq_doobert_admin_appdbrt(document).ready(function () {

    jq_doobert_admin_appdbrt(document).on('click', '.clear_logo', function () {
        jq_doobert_admin_appdbrt(this).closest('tr').find('.logo_upload').val('');
    });

    var custom_uploader;
    jq_doobert_admin_appdbrt(document).on('click', '.logo_upload', function (e) {
        e.preventDefault();
        let tg = jq_doobert_admin_appdbrt(this);
        custom_uploader = wp.media({
            title: 'Choose Image',
            library: {
                // limit the media library to show only image files
                type: 'image'
            },
            button: {
                text: 'Insert Image'
            },
            multiple: false  // set this to true if you want to allow multiple image selection
        });
        custom_uploader.on('select', function () {
            var attachment = custom_uploader.state().get('selection').first().toJSON();
            tg.val(attachment.url);
            if (jq_doobert_admin_appdbrt('.dflexbublle').length > 0) {
                update_chatbody_design();
                update_headers_colors();
            }
        });
        custom_uploader.open();
    });
});


function update_bubble_colors() {
    var bubble_icon_bk = jq_doobert_admin_appdbrt('input[name="bubble_icon_bk"]').val();
    var icon_color = jq_doobert_admin_appdbrt('input[name="bubble_icon_ic_bk"]').val();
    var bubble_icon_border = jq_doobert_admin_appdbrt('input[name="bubble_icon_border"]').val();

    jq_doobert_admin_appdbrt('.bubble_design1 .chat-icon').css({
        'background-color': bubble_icon_bk,
        'border-color': bubble_icon_border,
    });

    jq_doobert_admin_appdbrt('.bubble_design1 .chat-icon g').each(function () {
        this.style.setProperty('fill', icon_color, 'important');
    });
    var bubble_icon_hover_bk = jq_doobert_admin_appdbrt('input[name="bubble_icon_hover_bk"]').val();
    var bubble_icon_ic_hover_bk = jq_doobert_admin_appdbrt('input[name="bubble_icon_ic_hover_bk"]').val();
    jq_doobert_admin_appdbrt('.bubble_design2 .chat-icon').css({
        'background-color': bubble_icon_hover_bk,
        'border-color': bubble_icon_border,
    });

    jq_doobert_admin_appdbrt('.bubble_design2 .chat-icon g').each(function () {
        this.style.setProperty('fill', bubble_icon_ic_hover_bk, 'important');
    });
}
function update_headers_colors() {
    var header_bk_color = jq_doobert_admin_appdbrt('input[name="header_bk_color"]').val();
    var header_font_color = jq_doobert_admin_appdbrt('input[name="header_font_color"]').val();
    var header_logo = jq_doobert_admin_appdbrt('input[name="header_logo"]').val();
    var close_icon_bk_color = jq_doobert_admin_appdbrt('input[name="close_icon_bk_color"]').val();
    var close_icon_front_color = jq_doobert_admin_appdbrt('input[name="close_icon_front_color"]').val();
    var close_icon_border_color = jq_doobert_admin_appdbrt('input[name="close_icon_border_color"]').val();
    jq_doobert_admin_appdbrt('.chat_header_top').css({
        'background-color': header_bk_color,
    });
    jq_doobert_admin_appdbrt('.chat-close-button').css({
        'background-color': close_icon_bk_color,
    });

    jq_doobert_admin_appdbrt('.chat-close-button').css({
        'border-color': close_icon_border_color,
    });
    jq_doobert_admin_appdbrt('.chat-close-button g').each(function () {
        this.style.setProperty('fill', close_icon_front_color, 'important');
    });
    if (header_logo && header_logo != '') {
        jq_doobert_admin_appdbrt('.chat_d_icon img').attr('src', header_logo);
    }
    jq_doobert_admin_appdbrt('.chat-header h4').css({
        'color': header_font_color,
    });
}
function update_chatbody_design() {
    var body_logo = jq_doobert_admin_appdbrt('input[name="body_logo"]').val();
    if (body_logo && body_logo != '') {
        jq_doobert_admin_appdbrt('.chat_icon').attr('src', body_logo);
    }
    var cb_body_bk = jq_doobert_admin_appdbrt('input[name="cb_body_bk"]').val();
    jq_doobert_admin_appdbrt('#chat-body, .messageHistory').css({
        'background-color': cb_body_bk,
    });
    var cb_default_msg_color = jq_doobert_admin_appdbrt('input[name="cb_default_msg_color"]').val();
    jq_doobert_admin_appdbrt('.pre_button').css({
        'color': cb_default_msg_color,
        'border-color': cb_default_msg_color,
    });
    var cb_body_chat_bk = jq_doobert_admin_appdbrt('input[name="cb_body_chat_bk"]').val();
    jq_doobert_admin_appdbrt('.notification.is-success').css({
        'background-color': cb_body_chat_bk,
    });
    var cb_body_chat_front_color = jq_doobert_admin_appdbrt('input[name="cb_body_chat_front_color"]').val();
    jq_doobert_admin_appdbrt('.response_msg').css({
        'color': cb_body_chat_front_color,
    });
    var cb_body_user_bk = jq_doobert_admin_appdbrt('input[name="cb_body_user_bk"]').val();
    jq_doobert_admin_appdbrt('.uchat').css({
        'background-color': cb_body_user_bk,
    });
    var cb_body_user_front_color = jq_doobert_admin_appdbrt('input[name="cb_body_user_front_color"]').val();
    jq_doobert_admin_appdbrt('.user_mgs').css({
        'color': cb_body_user_front_color,
    });
    var send_message_bk = jq_doobert_admin_appdbrt('input[name="send_message_bk"]').val();
    jq_doobert_admin_appdbrt('#message-input').css({
        'background-color': send_message_bk,
    });
    var send_message_button_bk = jq_doobert_admin_appdbrt('input[name="send_message_button_bk"]').val();
    jq_doobert_admin_appdbrt('#send-message').css({
        'background-color': send_message_button_bk,
    });
    var send_message_button_icon_color = jq_doobert_admin_appdbrt('input[name="send_message_button_icon_color"]').val();
    jq_doobert_admin_appdbrt('#send-message svg').css({
        'fill': send_message_button_icon_color,
    });

    var scrollbarThumbColor = jq_doobert_admin_appdbrt('input[name="scroll_bar_color"]').val();
    var scroll_bar_color = jq_doobert_admin_appdbrt('input[name="scroll_bar_color"]').val();
    jq_doobert_admin_appdbrt("#scrollbar-style").remove();
    let $styleElement = jq_doobert_admin_appdbrt('#dynamic-scrollbar-style');
    if ($styleElement.length === 0) {
        $styleElement = jq_doobert_admin_appdbrt('<style id="dynamic-scrollbar-style"></style>');
        jq_doobert_admin_appdbrt('head').append($styleElement);
    }
    $styleElement.text(`
        #chat-body::-webkit-scrollbar {
            background-color: ${scroll_bar_color};
        }
        #chat-body::-webkit-scrollbar-thumb {
            background-color: ${scroll_bar_color};
        }
    `);
//    jq_doobert_admin_appdbrt("<style>")
//            .prop("type", "text/css")
//            .prop("id", "scrollbar-style")
//            .html(`
//        #chat-body::-webkit-scrollbar-thumb,
//        #chat-footer #message-input::-webkit-scrollbar-thumb {
//            background-color: ${scrollbarThumbColor};
//        }
//        #chat-body::-moz-scrollbar-thumb,
//        #chat-footer #message-input::-moz-scrollbar-thumb {
//            background-color: ${scrollbarThumbColor};
//        }
//    `).appendTo("head");
}
jq_doobert_admin_appdbrt(function () {
    if (jq_doobert_admin_appdbrt('.dflexbublle').length > 0) {
        update_bubble_colors();
        jq_doobert_admin_appdbrt(document).on('change input', '.dflexbublle input', function () {
            update_bubble_colors();
        });
        update_headers_colors();
        jq_doobert_admin_appdbrt(document).on('change input', '.chatbotheader input', function () {
            update_headers_colors();
        });
        update_chatbody_design();
        jq_doobert_admin_appdbrt(document).on('change input', '.chatbody input', function () {
            update_chatbody_design();
        });
    }
});

jq_doobert_admin_appdbrt('#upload_image_button').click(function(e) {

    e.preventDefault();

    //If the uploader object has already been created, reopen the dialog
    if (custom_uploader) {
        custom_uploader.open();
        return;
    }

    //Extend the wp.media object
    custom_uploader = wp.media.frames.file_frame = wp.media({
        title: 'Choose Image',
        button: {
            text: 'Choose Image'
        },
        multiple: true
    });

    //When a file is selected, grab the URL and set it as the text field's value
    custom_uploader.on('select', function() {
        console.log(custom_uploader.state().get('selection').toJSON());
        attachment = custom_uploader.state().get('selection').first().toJSON();
        jq_doobert_admin_appdbrt('#adopt_image').val(attachment.url);
        jq_doobert_admin_appdbrt('#adopt_image_id').attr('src',attachment.url);
        jq_doobert_admin_appdbrt('#adopt_image_id').css('display','block');

    });

    //Open the uploader dialog
    custom_uploader.open();

});