var EventosList = {
    last_count: null,
    init: function() {
        EventosList.last_count = $('#new_list_container').data('last-count');

        $('.quitar_list').on('click', function () {
            var num = $(this).data('num');

            if ($(this).is(':checked')) {
                $('input[name="list_plt_' + num + '"]').attr("disabled", "disabled");
                $('#urlf5_list_url' + num).attr("readonly", "readonly");
            } else {
                $('input[name="list_plt_' + num + '"]').removeAttr("disabled");
                $('#urlf5_list_url' + num).removeAttr("readonly");
            }
        });

        $('#add_list').on('click', function (e) {
            e.preventDefault();

            if ($('select[name="list_plt_' + EventosList.last_count + '"]').val() == '') {
                showModalAlert({
                    title: 'Error',
                    type: 'orange',
                    msg: 'Primero debes seleccionar una plantilla'
                });

                return;
            }

            var html = $('#plt_new_list').html();
            EventosList.last_count++;

            // Quitar las plantillas que ya fueron seleccionadas
            $('select[name^="list_plt_"]').each(function () {
                var val = $(this).val();

                if (val != '') {
                    var re_str = '<option value="' + val + '">' + val + '</option>';
                    var re = new RegExp(re_str, "g");

                    html = html.replace(re, '');
                }
            });

            html = html.replace(/__num__/g, EventosList.last_count);

            $('#new_list_container').append(html);

            UrlF5.methods.bindEvents();
        });
    }
};

$(document).ready(function () {
    EventosList.init();
});
