(function ($) {
    $.field_selectmonth = function (element, options) {
        var defaults = {
            input_name: "",
            value:[]
        }
        var plugin = this;
        plugin.settings = {}
        var $element = $(element),
            element = element;

        plugin.init = function () {
            plugin.settings = $.extend({}, defaults, options);
            $element.find('input.month-of-year').iCheck({
                checkboxClass: 'icheckbox_minimal',
                radioClass: 'iradio_minimal',
                increaseArea: '20%'
            });
            let value=plugin.settings.value;
            let list_working_month=value;
            $(`input[name="${plugin.settings.input_name}"]`).val(JSON.stringify(value));
            $element.find('input.month-of-year').on('ifChecked', function () {
                let val = $(this).val();
                list_working_month.push(val);
                $element.find(`input[name="${plugin.settings.input_name}"]`).val(JSON.stringify(list_working_month));
            });
            $element.find('input.month-of-year').on('ifUnchecked', function () {
                let val = $(this).val();
                let index = list_working_month.indexOf(val);
                list_working_month.splice(index, 1);
                $element.find(`input[name="${plugin.settings.input_name}"]`).val(JSON.stringify(list_working_month));
            });

        };
        plugin.example_function = function () {
        };
        plugin.init();
    }

    $.fn.field_selectmonth = function (options) {

        return this.each(function () {

            if (undefined == $(this).data('field_selectmonth')) {
                var plugin = new $.field_selectmonth(this, options);
                $(this).data('field_selectmonth', plugin);
            }
        });
    }
})(jQuery);