<%inherit file="//layout/admin_lite_dialog.html"/>
<%namespace name="lib" file="//lib.html" />
<%namespace name="formset" file="//formset.html" />
<%def name="title()">${_('Add pool allocation')} - ${parent.title()}</%def>
<%def name="get_breadcrumbs()">
    <%
        return [
            [_('Manager'), ['manager']],
            [_('Licensing'), ['manager','system','licensing']],
            [_('Manage license pool'), None]
        ]        
    %>
</%def>

##
## contents
##

% if controller_exception:

    <div class="error">
        % if isinstance(controller_exception, splunk.ResourceNotFound):
            ${_('Could not find the stack:')} ${controller_exception | h}
        % elif controller_exception == splunk.AuthorizationFailed:
            ${_('You do not have permission to edit this stack:')} ${controller_exception | h}
        % else:
            <%lib:render_exception e="${controller_exception}" />
        % endif 
    </div>

% endif


% if is_success:

    % if action == 'add':
        <div class="message_block_success"><span class="message_block_success_text">${_('Create successful')}</span></div>
        <p>${_('The license pool %s has been successfully created.') % h(pool_object.name)}</p>
    % else:
        <div class="message_block_success"><span class="message_block_success_text">${_('Update successful')}</span></div>
        <p>${_('The license pool %s has been successfully updated.') % h(pool_object.name)}</p>
    % endif

% else:

    <h1>
    % if action == 'add':
        ${_('Create new license pool')}
    % else:
        ${_('Edit license pool:')} ${pool_object.name | h}
    % endif
    </h1>

    <table class="form_table">
        <tr>
            <td class="k">${_('Stack')}</td><td>${stack_label | h}</td>
        </tr>
        % if action == 'add':
            <tr>
                <td class="k"><label for="poolName">${_('Pool name')}</label></td><td>${formset.textbox(name="name",id="poolName", model=pool_object)}</td>
            </tr>
        % endif
        <tr>
            <td class="k"><label for="desc">${_('Description')}</label></td><td>${formset.textarea(name="description",id="desc", model=pool_object)}</td>
        </tr>
        <tr>
            <td class="k">${_('Allocation')}</td>
            <td id="quota_mode_container">
                <%
                maxVolume = i18n.format_bytes(stack_quota_bytes, output_units='MB', mantissa=0)
                if isUnlimited:
                    maxVolume = _('Unlimited')
                %>
                <strong>${_('What is the maximum daily volume this pool may consume?')}</strong>
                <p>
                ${formset.radioset(name="quota_mode", value=pool_quota_mode, options=[['all',_('The license maximum')]])}
                (${maxVolume | h})
                </p>
                <p>
                ${formset.radioset(name="quota_mode", value=pool_quota_mode, options=[['explicit',_('A specific amount')]], id_offset=10)}
                </p>
                <div style="padding-left:22px;display:none" id="quota_value_input">
                	<label for="allocationAmt" class="hiddenLabel">Allocation Quota Value</label>
                    <label for="allocationUnit" class="hiddenLabel">Allocation Quota Unit</label>
                    ${formset.textbox(name="quota_value", id="allocationAmt", size=8, value=pool_quota_value)}
                    ${formset.select(name="quota_units", id="allocationUnit",value=pool_quota_units, options=pool_quota_enum)}
                </div>
                <br />
            </td>
        </tr>
        <tr>
            <td class="k">${_('Indexers')}</td>
            <td id="slave_select_container">
                <strong>${_('Which indexers are eligible to draw from this pool?')}</strong>
                <p>
                ${formset.radioset(name="slave_mode", value=slave_mode, options=[['catchall',_('Any indexer that connects')]], disabled=(not can_be_catch_all))}
                </p>
                <p>
                ${formset.radioset(name="slave_mode", value=slave_mode, options=[['explicit',_('Specific indexers')]], id_offset=10)}
                </p>
                <div style="padding-left:22px;display:none" id="slave_select_accumulator">
                    ${formset.accumulator(name="slaves", values=assigned_slave_list, options=slave_list, available_label=_('Available indexers'), selected_label=_('Associated indexers'))}
                </div>
            </td>
        </tr>
    </table>

    <script>
        
        //
        // switch the slave selection
        //

        function update_slave_selection() {
            if ($('input[type="radio"]:checked', '#slave_select_container').val() == 'explicit') {
                $('#slave_select_accumulator').show();
            } else {
                $('#slave_select_accumulator').hide();
            }
        }

        $('#slave_select_container').bind('click', function(evt) {
            var e = $(evt.target);
            if (e.is('input')) {
                update_slave_selection();
            }
        });

        $(update_slave_selection);


        //
        // switch the quota selection
        //

        function update_quota_selection() {
            if ($('input[type="radio"]:checked', '#quota_mode_container').val() == 'explicit') {
                $('#quota_value_input').show();
                $('input[name="formset_quota_value"]').focus();
            } else {
                $('#quota_value_input').hide();
            }
        }

        $('#quota_mode_container').bind('click', function(evt) {
            var e = $(evt.target);
            if (e.is('input')) {
                update_quota_selection();
            }
        });

        $(update_quota_selection);


        // add extra confirmation for users adding 0-byte bools
        $(function() {
            $('form.admin-lite-dialog').bind('submit', function() {
                if ($('input[name="formset_quota_value"]').val() == 0
                    && $('input[type="radio"]:checked', '#quota_mode_container').val() == 'explicit') {
                    if(confirm(_('Are you sure you want to create an empty (0MB) pool?'))) {
                        return true;
                    } else {
                        return false;
                    }
                }
                return true;
            });
        });
    </script>
% endif


##
## buttons
##

<%def name="gutter()">
    <div class="admin-dialog-tray">
    % if is_success:
        ${formset.button(_('OK'), is_primary=True, href=return_to)}
    % else:
        ${formset.button(_('Cancel'), href=return_to)}
        ${formset.button(_('Submit'), is_primary=True, is_submit=True)}
    % endif
    </div>
</%def>
