jQuery(document).ready(function ($) {


    //	$(".ahachat_save_changes_shortcode").click(function(event){


    $(document).on('click', '.ahachat_close_shortcode', function () {

        $("#show_text_shortcode_title").text("15% discount");

        $("#show_text_shortcode_subtitle").text("Get your 15% discount now by subscribing to our newsletter in facebook messenger. Just click the blue button below.");

        //$("#message_shortcode").val("");

        $(".send_custom_text_width_sendto .emojionearea .emojionearea-editor").html("");

        $(".ahachat_text_preiew").css("border-color", "rgb(255, 98, 73)");

        $("#txt_preview").text("");

        $(".send_custom_text .emojionearea .emojionearea-editor").html("");

        $(".ahachat_text_preiew_width_cb").css("border-color", "rgb(255, 98, 73)");

        $("#txt_preview_width_cb").text("");

        //

        $("#ahachat_button_type_send_to").attr('checked', true);

        $("#ahachat_button_type_checbox").attr('checked', false);

        $('.ahachat_send_messenger').show();

        $('.ahachat_messenger_checkbox').hide();

        $('#ahachat_type_cb_send_me').val("Send me Coupon");

        //

        $("#ahachat_message_type_custom_text").attr('checked', true);

        $("#ahachat_message_type_product_reminder").attr('checked', false);

        $(".send_custom_text").show();

        $(".ahachat_root_popup").hide();

        $(".ahachat_root_popup li.ahachat_popup_dashboard_selected").html("");

        //

        $('.ahachat_with_button_type_sendto').show();

        $('.ahachat_with_button_type_checkbox').hide();

        $('.ahachat_text_preiew_width_cb').hide();

        $('.ahachat_text_preiew').show();

        //

        $("#ahachat_cartback_add_button_create_edit_shortcode").removeAttr("data-action");

        $("#ahachat_cartback_add_button_create_edit_shortcode").removeAttr("data-shortcode_edit");

        $("#ahachat_cartback_add_button_create_edit_shortcode").html('<button type="button" data-action="create" class="btn btn-primary ahachat_save_changes_shortcode">Save changes</button>');

        //console.log($("#ahachat_cartback_add_button_create_edit_shortcode").attr('data-action'));

        //console.log($("#ahachat_cartback_add_button_create_edit_shortcode").html());

    });

    $(document).on('click', '.ahachat_save_changes_shortcode', function () {

        var action_shortcode = $(this).data("action");


        if (action_shortcode == "edit") {

            var id_shortcode = $(this).data("shortcode_edit");

        } else {

            id_shortcode = "none";

        }


        //	$("div.ahachat_list_shortcode ul.ahachat_shortcode_template_show li:last").after('<li><a href="/user/messages"><span class="tab">Message Center</span></a></li>');


        var title = $("#show_text_shortcode_title").text();

        var sub_title = $("#show_text_shortcode_subtitle").text();

        var message = $("#message_shortcode").val();  //width cb

        var message_check = $(".send_custom_text .emojionearea .emojionearea-editor").html();//width cb

        var message_sendto = $("#message_custom_send").val();  //width cb

        var message_check_sendto = $(".send_custom_text_width_sendto .emojionearea .emojionearea-editor").html();//width cb

        var button_type = $('.ahachat_button_type:checked').val();

        var message_type = $('.ahachat_message_type:checked').val();

        var text_send_me = $('#ahachat_type_cb_send_me').val();

        var list_product = $('.ahachat_product_page_list_results').val();


        if (button_type == "checkbox") {

            if (message_type == "custom_text") {

                if (message_check == "") {

                    alert("Please enter text message!");

                    return false;

                }

            }


        } else {

            if (message_check_sendto == "") {

                alert("Please enter text message!");

                return false;

            }

        }


        var action_data = {

            action: 'ahachat_action_save_change_shortcode',

            title: title,

            sub_title: sub_title,

            message: message,

            message_sendto: message_sendto,

            button_type: button_type,

            message_type: message_type,

            text_send_me: text_send_me,

            list_product: list_product,

            action_shortcode: action_shortcode,

            id_shortcode: id_shortcode

        };

        $.post(ajaxurl, action_data, function (result) {

            //console.log(result);

            if (result) {

                if (result != "edit" && result != 0) {

                    $(".close").click();

                    // replace width

                    $("div.ahachat_list_shortcode ul.ahachat_shortcode_template_show li:last").after(result);

                    $(".ahachat_alert_shorcode_success").show();

                    $(".ahachat_alert_action_done").text("Save Changed");

                    setTimeout(function () {

                        $(".ahachat_alert_shorcode_success").fadeOut(1000);

                    }, 2000);

                } else {

                    $(".close").click();

                    $(".ahachat_alert_shorcode_success").show();

                    $(".ahachat_alert_action_done").text("Save Changed");

                    $("#show_text_shortcode_title").text("15% discount");

                    $("#show_text_shortcode_subtitle").text("Get your 15% discount now by subscribing to our newsletter in facebook messenger. Just click the blue button below.");

                    $("#message_shortcode").val("");
                    $("#message_custom_send").val("");

                    $(".ahachat_save_changes_shortcode").attr("data-action", "create");

                    $(".send_custom_text_width_sendto .emojionearea .emojionearea-editor").html("");

                    $(".ahachat_text_preiew").css("border-color", "rgb(255, 98, 73)");

                    $("#txt_preview").text("");

                    $(".send_custom_text .emojionearea .emojionearea-editor").html("");

                    $(".ahachat_text_preiew_width_cb").css("border-color", "rgb(255, 98, 73)");

                    $("#txt_preview_width_cb").text("");

                    setTimeout(function () {

                        $(".ahachat_alert_shorcode_success").fadeOut(1000);

                    }, 2000);

                }

            }

        });


    });


    //	$(".ahachat_edit_shortcode").click(function(event){

    $(document).on('click', '.ahachat_edit_shortcode', function () {

        var _this = $(this);

        var id_shortcode = $(this).data("shortcode_edit");

        _this.parent().find('.spinner').addClass('ahachat_popup_dashboard_spinner_visible');

        var action_data = {

            action: 'ahachat_action_eidt_shortcode',

            id_shortcode: id_shortcode,

        };

        $.post(ajaxurl, action_data, function (result) {

            //console.log(result);

            _this.parent().find('.spinner').removeClass('ahachat_popup_dashboard_spinner_visible');

            //alert(data.title);

            //alert(data.sub_title);

            if (result) {

                var data = JSON.parse(result);

                $("#show_text_shortcode_title").text(data.title);

                $("#show_text_shortcode_subtitle").text(data.sub_title);

                //$("#message_shortcode").val(data.message);

                $("#tooltip_show_empty").attr("flow", "none");

                //$(".ahachat_save_changes_shortcode").attr("data-action","edit");

                //$(".ahachat_save_changes_shortcode").attr("data-shortcode_edit",id_shortcode);

                $("#ahachat_cartback_add_button_create_edit_shortcode").attr('data-action', 'edit');

                $("#ahachat_cartback_add_button_create_edit_shortcode").attr('data-shortcode_edit', id_shortcode);

                $("#ahachat_cartback_add_button_create_edit_shortcode").html('<button type="button" data-action="edit" data-shortcode_edit="' + id_shortcode + '" class="btn btn-primary ahachat_save_changes_shortcode">Save changes</button>');

                $(".ahachat_click_create_shortcode").click();


                $(".ahachat_product_page_list_results").val(data.list_product);

                //

                if (data.text_type_cb == null) {

                    data.text_type_cb = "Send me Coupon";

                }

                $('#ahachat_type_cb_send_me').val(data.text_type_cb);

                //

                $(".send_custom_text_width_sendto .emojionearea .emojionearea-editor").text(data.message);

                $("#txt_preview").text(data.message);

                $(".ahachat_text_preiew").css("border-color", "rgb(241, 240, 240)");

                //

                $(".send_custom_text .emojionearea .emojionearea-editor").text(data.message_cb);

                $("#txt_preview_width_cb").text(data.message_cb);

                $(".ahachat_text_preiew_width_cb").css("border-color", "rgb(241, 240, 240)");

                if (data.button_type == null || data.button_type == "send_to") {

                    $("#ahachat_button_type_send_to").attr('checked', true);

                    $("#ahachat_button_type_checbox").attr('checked', false);

                    $('.ahachat_send_messenger').show();

                    $('.ahachat_messenger_checkbox').hide();

                    //

                    $('.ahachat_with_button_type_sendto').show();

                    $('.ahachat_with_button_type_checkbox').hide();


                    $('.ahachat_text_preiew_width_cb').hide();

                    $('.ahachat_text_preiew').show();


                } else {

                    $("#ahachat_button_type_send_to").attr('checked', false);

                    $("#ahachat_button_type_checbox").attr('checked', true);

                    $('.ahachat_send_messenger').hide();

                    $('.ahachat_messenger_checkbox').show();

                    //

                    $('.ahachat_with_button_type_sendto').hide();

                    $('.ahachat_with_button_type_checkbox').show();


                    $('.ahachat_text_preiew_width_cb').show();

                    $('.ahachat_text_preiew').hide();

                    //


                }

                if (data.message_type == null || data.message_type == "custom_text") {

                    $("#ahachat_message_type_custom_text").attr('checked', true);

                    $("#ahachat_message_type_product_reminder").attr('checked', false);

                    $(".send_custom_text").show();

                    $(".ahachat_root_popup").hide();

                    if (data.button_type == null || data.button_type == "send_to") {

                        $(".ahachat_show_custom_text_not_list_product").show();

                        $(".ahachat_show_custom_text_list_product").hide();

                    }


                } else {

                    $("#ahachat_message_type_custom_text").attr('checked', false);

                    $("#ahachat_message_type_product_reminder").attr('checked', true);

                    $(".send_custom_text").hide();

                    $(".ahachat_root_popup").show();

                    if (data.button_type == "checkbox") {

                        $(".ahachat_show_custom_text_not_list_product").hide();

                        $(".ahachat_show_custom_text_list_product").show();

                    }


                }

                if (data.list_product != "" && data.list_product != null) {

                    var action_data_second = {

                        action: 'ahachat_parse_edit_list_product',

                        list_product: data.list_product,

                    };

                    $.post(ajaxurl, action_data_second, function (result) {

                        //console.log(result);

                        $(".ahachat_root_popup li.ahachat_popup_dashboard_selected").html(result);

                    });

                }


            }

        });

    });


//		$(".ahachat_copy_shortcode").click(function(event){

    $(document).on('click', '.ahachat_copy_shortcode', function () {

        event.preventDefault();

        var shortcode_id = $(this).data('shortcode_id');

        //alert(shortcode_id);

        $(".ahachat_alert_shorcode_success").show();

        $(".ahachat_alert_action_done").text("Copied");

        setTimeout(function () {

            $(".ahachat_alert_shorcode_success").fadeOut(1000);

        }, 2000);

        var optin_copy = "#ahachat_popup_shortcode_" + shortcode_id;

        $(optin_copy).select();

        document.execCommand('copy');


    });

    $('body').on('click', '.ahachat_shortcode_delete:not(.clicked_button)', function () {

        var this_el = $(this);


        $('.ahachat_shortcode_delete').removeClass('clicked_button');


        this_el.addClass('clicked_button');

        $('.ahachat_popup_dashboard_confirmation').hide();


        this_el.find('.ahachat_popup_dashboard_confirmation').fadeToggle();

    });


    // WHEN CLICK CANCEL DELETE

    $('body').on('click', '.ahachat_popup_dashboard_cancel_delete', function () {

        $(this).parent().hide();

        $(this).parent().parent().removeClass('clicked_button');

    });


    $('body').on('click', '.ahachat_popup_dashboard_confirm_delete', function () {

        var id_shortcode = $(this).data("shortcdoe_delete");

        var delete_li = ".ahachat_shortcode_show_" + id_shortcode;


        var _this = $(this);

        var action_data = {

            action: 'ahachat_action_delete_shortcode',

            id_shortcode: id_shortcode,

        };

        $.post(ajaxurl, action_data, function (result) {


            if (result) {

                $(delete_li).remove();

                _this.parent().hide();

                _this.parent().parent().removeClass('clicked_button');

            }

        });

        //	remove_optin( optin_id,parent_id );


    });


    //


    $('#message_shortcode,#message_custom_send').emojioneArea({

        saveEmojisAs: true

    });


    $(".ahachat_button_type").change(function () {

        var button_type = $('.ahachat_button_type:checked').val();

        if (button_type == "send_to") {

            $('.ahachat_send_messenger').show();

            $('.ahachat_messenger_checkbox').hide();

            //

            $('.ahachat_with_button_type_sendto').show();

            $('.ahachat_with_button_type_checkbox').hide();


            $('.ahachat_text_preiew_width_cb').hide();

            $('.ahachat_text_preiew').show();

            $(".ahachat_show_custom_text_not_list_product").show();

            $(".ahachat_show_custom_text_list_product").hide();

        } else {

            $('.ahachat_send_messenger').hide();

            $('.ahachat_messenger_checkbox').show();

            //

            $('.ahachat_with_button_type_sendto').hide();

            $('.ahachat_with_button_type_checkbox').show();

            $('.ahachat_text_preiew_width_cb').show();

            $('.ahachat_text_preiew').hide();


            var message_type = $('.ahachat_message_type:checked').val();

            if (message_type == "custom_text") {

                $(".ahachat_show_custom_text_not_list_product").show();

                $(".ahachat_show_custom_text_list_product").hide();

            } else {

                $(".ahachat_show_custom_text_not_list_product").hide();

                $(".ahachat_show_custom_text_list_product").show();

            }

        }


    });

    $(".ahachat_button_type").trigger('change');


    $(".ahachat_message_type").change(function () {

        var message_type = $('.ahachat_message_type:checked').val();

        if (message_type == "custom_text") {

            $(".ahachat_show_custom_text_not_list_product").show();

            $(".ahachat_show_custom_text_list_product").hide();

            $(".send_custom_text").show();

            $(".ahachat_root_popup").hide();

        } else {

            $(".send_custom_text").hide();

            $(".ahachat_root_popup").show();

            $(".ahachat_show_custom_text_not_list_product").hide();

            $(".ahachat_show_custom_text_list_product").show();

        }


    });

    $(".ahachat_message_type").trigger('change');


    /***************************/

    $('body').on('focusin', '.ahachat_popup_dashboard_search_posts', function () {

        var $this_input = $(this);

        $this_input.closest('.ahachat_root_popup').find('.ahachat_popup_dashboard_live_search_res').addClass('visible_search_res');


        if (!$this_input.hasClass('already_triggered')) {

            $this_input.addClass('already_triggered');

            handle_live_search($this_input, 0, 1, true);

        }


    });

    $(document).click(function () {

        $('.ahachat_popup_dashboard_live_search_res').removeClass('visible_search_res');

    });

    $('body').on('click', '.ahachat_popup_dashboard_search_posts', function () {

        return false;

    });

    $('body').on('input', '.ahachat_popup_dashboard_search_posts', function () {

        handle_live_search($(this), 500, 1, true);

    });

    var ahachat_value_image = $('#ahachat_img_remove_list_product').val();

    $('body').on('click', '.ahachat_popup_dashboard_search_results li', function () {

        var $this_item = $(this);

        if (!$this_item.hasClass('ahachat_popup_dashboard_no_res')) {

            var $text = $this_item.text(),

                $id = $this_item.data('post_id'),

                $main_container = $this_item.closest('.ahachat_root_popup'),

                $display_box = $main_container.find('.ahachat_popup_dashboard_selected'),

                $value_field = $main_container.find('input[type="hidden"]'),

                $new_item = '<span data-post_id="' + $id + '">' + $text + '<span class="ahachat_popup_dashboard_menu_remove"><img style="position: absolute;" src="' + ahachat_value_image + '"></span></span>';

            if (-1 === $display_box.html().indexOf('data-post_id="' + $id + '"')) {

                $display_box.append($new_item);

                if ('' === $value_field.val()) {

                    $value_field.val($id);

                } else {

                    $value_field.val(function (index, value) {

                        return value + "," + $id;

                    });

                }

            }

        }

        return false;

    });

    $('body').on('mousewheel DOMMouseScroll', '.ahachat_popup_dashboard_search_results', function () {

        var $this_el = $(this),

            $page = typeof $this_el.data('page') === 'undefined' ? 1 : $this_el.data('page'),

            $max_scroll = typeof $this_el.data('scroll') === 'undefined' ? 0 : $this_el.data('scroll');

        if ((0 === $this_el.scrollTop() % 200) && $this_el.scrollTop() > $max_scroll) {

            $this_el.data('page', $page + 1);

            $this_el.data('scroll', $this_el.scrollTop());

            handle_live_search($this_el.closest('.ahachat_root_popup').find('.ahachat_popup_dashboard_search_posts'), 0, $page + 1, false);

        }

    });

    $('body').on('click', '.ahachat_popup_dashboard_selected span.ahachat_popup_dashboard_menu_remove', function () {

        var $this_item = $(this).parent(),

            $value_field = $this_item.closest('.ahachat_root_popup').find('input[type="hidden"]'),

            $value_string = $value_field.val(),

            $id = $this_item.data('post_id');

        if (-1 !== $value_string.indexOf($id)) {

            var $string_to_replace = -1 !== $value_string.indexOf(',' + $id) ? ',' + $id : $id + ',',

                $string_to_replace = -1 !== $value_string.indexOf(',') ? $string_to_replace : $id,

                $new_value = $value_string;

            $new_value = $value_string.replace($string_to_replace, '');

            $value_field.val($new_value);

        }

        $this_item.remove();

        return false;

    });

    function handle_live_search($input, $delay, $page, $full_content) {

        var $this_input = $input,

            $search_value = $this_input.val(),

            $post_type = $this_input.data('post_type');


        //	alert($search_value);

        //	alert($this_input.val());


        setTimeout(function () {

            if ($search_value === $this_input.val()) {

                $.ajax({

                    type: 'POST',

                    url: ajaxurl,

                    data: {

                        action: 'ahachat_popup_execute_live_search',

                        dashboard_live_search: $search_value,

                        dashboard_post_type: $post_type,

                        dashboard_page: $page,

                        dashboard_full_content: $full_content

                    },

                    beforeSend: function (data) {

                        $this_input.parent().find('.spinner').addClass('ahachat_popup_dashboard_spinner_visible');

                    },

                    success: function (data) {

                        $this_input.parent().find('.spinner').removeClass('ahachat_popup_dashboard_spinner_visible');

                        //console.log(data);

                        if (true == $full_content) {

                            $this_input.closest('.ahachat_root_popup').find('.ahachat_popup_dashboard_search_results').replaceWith(data);

                        } else {

                            $this_input.closest('.ahachat_root_popup').find('.ahachat_popup_dashboard_search_results').append(data);

                        }

                    }

                });

            }

        }, $delay);


    }

    //

    //$(".ahachat_click_create_shortcode").click(function(e){

    $(document).on('click', '.ahachat_click_create_shortcode', function () {

        console.log($("#ahachat_cartback_add_button_create_edit_shortcode").attr('data-action'));

        var shortcode_action = $("#ahachat_cartback_add_button_create_edit_shortcode").data('action');

        if (shortcode_action == undefined) {

            $("#ahachat_cartback_add_button_create_edit_shortcode").html('<button type="button" data-action="create" class="btn btn-primary ahachat_save_changes_shortcode">Save changes</button>');

        }

    });

});