(function ($) {
    $.field_selectrepeat = 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.repeat').iCheck({
                checkboxClass: 'icheckbox_minimal',
                radioClass: 'iradio_minimal',
                increaseArea: '20%'
            });
            let value=plugin.settings.value;
            let list_repeat=value;
            $(`input[name="${plugin.settings.input_name}"]`).val(JSON.stringify(value));
            $element.find('input.repeat').on('ifChecked', function () {
                let val = $(this).val();
                list_repeat.push(val);
                $element.find(`input[name="${plugin.settings.input_name}"]`).val(JSON.stringify(list_repeat));
            });
            $element.find('input.repeat').on('ifUnchecked', function () {
                let val = $(this).val();
                let index = list_repeat.indexOf(val);
                list_repeat.splice(index, 1);
                $element.find(`input[name="${plugin.settings.input_name}"]`).val(JSON.stringify(list_repeat));
            });

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

    $.fn.field_selectrepeat = function (options) {

        return this.each(function () {

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