/**
 * Panel instagram page scripts
 *
 * @version 1.0.0
 * @since 7.0.0
 */

window.et_panel = window.et_panel || {};
!function($) {
    $(document).ready(function ($) {
        var et_instagram = {
            instagram_save_options: function(_this,e) {
                e.preventDefault();
                $('.etheme-instagram-save-info').addClass('hidden');
                if (!confirm('Are you sure ?')) {
                    return;
                }
                $('.etheme-instagram-settings').addClass('processing');
                var data = {
                    action: 'et_panel_ajax',
                    action_type: 'et_instagram_save_settings',
                    time: $('#instagram_time').val(),
                    time_type: $('#instagram_time_type').val()
                };
                $.ajax({
                    type: 'POST',
                    url: ajaxurl,
                    data: data,
                    success: function (data) {
                        if (data.type == 'success') {
                            $('.etheme-instagram-save-info.info-success').removeClass('hidden');
                        } else {
                            $('.etheme-instagram-save-info.info-error').removeClass('hidden');
                        }
                        $(document).find('.etheme-instagram-reinit')[0].click();
                    },
                    error: function () {
                        alert('Error while deleting');
                    },
                    complete: function () {
                        $('.etheme-instagram-settings').removeClass('processing');
                    }
                });
            },
            instagram_toogle_manual: function (_this,e){
                e.preventDefault();
                var manual = $('.etheme-instagram-manual-form');
                if (manual.hasClass('hidden')) {
                    manual.removeClass('hidden');
                } else {
                    manual.addClass('hidden');
                }
            },
            instagram_connect_manual : function (_this,e){
                e.preventDefault();
                if (!confirm('Are you sure')) {
                    return;
                }
                var parent = _this.parent();
                var data = {
                    // action: 'et_panel_ajax',
                    action: 'et_panel_ajax',
                    action_type: 'et_instagram_user_add',
                    token: $('#etheme-manual-token').val()
                };

                if (!data['token']) {
                    parent.find('.etheme-form-error').removeClass('hidden');
                    return;
                } else {
                    parent.find('.etheme-form-error').addClass('hidden');
                }
                $.ajax({
                    type: 'POST',
                    url: ajaxurl,
                    data: data,
                    dataType: 'JSON',
                    success: function (data) {
                        if (data.status != 'success') {
                            parent.find('.etheme-form-error-holder').text('');
                            parent.find('.etheme-form-error-holder').text(data.msg);
                            parent.find('.etheme-form-error-holder').removeClass('hidden');
                        } else {
                            location.reload();
                        }
                    },
                    error: function () {
                        alert('Error while deleting');
                    },
                    complete: function () {

                    }
                });
            },
            instagram_user_remove: function (_this,e){
                e.preventDefault();
                if (!confirm('Are you sure')) {
                    return;
                }
                var user = _this.parents('.etheme-user');
                var data = {
                    action: 'et_panel_ajax',
                    action_type: 'et_instagram_user_remove',
                    token: user.find('.user-token').attr('data-token')
                };
                $.ajax({
                    type: 'POST',
                    url: ajaxurl,
                    data: data,
                    success: function (data) {
                        if (data.status != 'success') {
                        } else {
                            if ($('.etheme-user').length < 2) {
                                $('.etheme-no-users').removeClass('hidden');
                            }

                            user.remove();
                        }
                    },
                    error: function () {
                        alert('Error while deleting');
                    },
                    complete: function () {

                    }
                });
            }
        };

        window.et_panel = Object.assign(window.et_panel,et_instagram);

        var et_instagram_actions = {
            '1': {
                selector: '.etheme-instagram-settings .etheme-instagram-save',
                type: 'click',
                callback : et_panel.instagram_save_options
            },
            '2': {
                selector: '.etheme-instagram-manual',
                type: 'click',
                callback : et_panel.instagram_toogle_manual
            },
            '3': {
                selector: '.etheme-manual-btn',
                type: 'click',
                callback : et_panel.instagram_connect_manual
            },
            '4': {
                selector: '.etheme-user .user-remove',
                type: 'click',
                callback : et_panel.instagram_user_remove
            },
        }

        $.each(et_instagram_actions, function (i, t) {
            if (t.type == 'ready'){
                $(document).ready(function (e) {
                    t.callback($(this),e);
                });
            } else {
                $(t.selector).on(t.type,function (e){
                    t.callback($(this),e);
                });
            }
        });
    });
}(jQuery,window.et_panel);