<%! import splunk.util, json, cgi %>
<div class="popupContainer fieldValuePopup splShadow">
    <div class="fieldValuePopupInner">
        % if error:
            <div class="splHeader splHeader-primary">
                <a class="splIcon splIcon-close" href="javascript:void(0)" s:type="close"></a>
                <h2>${_('Field summary unavailable')}</h2>
            </div>
            <div class="popupContent">
                <p>${error|h}</p>
            </div>            
        % else:
            <div class="splHeader splHeader-primary">
                <a class="splIcon splIcon-close" href="javascript:void(0)" s:type="close"></a>
                <h2>${field_name|h} ${_('(numeric)') if field_primarily_numeric else _('(categorical)')}</h2>
            </div>
            <div class="popupContent">
                <div class="fieldLinks">
                    <h4>${_('Appears in %(percent)s%% of results') % dict(percent=i18n.format_percent(field_frequency).rstrip('%'))}</h4>
                    
                    <% field_intention = {'name': 'addterm', 'arg': {field_name: '*'}} %>
                    <a href="#" s:type="search" s:intention="${json.dumps(field_intention)|x}">${_('Show only events with this field')}</a>
                </div>
                <div class="reportLinks splBackground-primary">
                    <h4>${_('Charts')} </h4>
                    <% reports = numerical_reports + categorical_reports if field_primarily_numeric else categorical_reports %>
                    % for index, report in enumerate(reports):
                        <a href="#" rel="popup" s:type="report" s:intention="${json.dumps(report['intention'])|x}" s:reportsearch="${report['reportSearch']|x}" s:charttype="${report['chartType']|x}">${report['label']|h}</a>
                        
                    % endfor
                </div>
                % if field_summary.get('numericCount')>field_summary.get('count')/2:
                    <p class="numericalStats">
                        <span><b>${_("Min")}</b>: ${field_summary.get('min')|h}</span>
                        <span><b>${_("Max")}</b>: ${field_summary.get('max')|h}</span>
                        <span><b>${_("Mean")}</b>: ${field_summary.get('mean')|h}</span>
                        <span><b>${_("Stdev")}</b>: ${field_summary.get('stdev')|h}</span>
                    </p>
                % endif
                <p class="fieldHeader"></p>
                <table>
                    <tbody style="display: table-row-group;">
                        <tr class="fieldNameHeaderRow">
                            <th class="fieldName">${_('Value')}</th>
                            <th style="text-align: right;">#</th>
                            <th style="text-align: right;">%</th>
                            <th></th>
                        </tr>
                        % for mode in field_summary.get('modes', [])[0:count]:
                            <% percent = mode.get('count', 0)/float(field_summary.get('count', 0)) %>
                            <tr>
                                <th class="fieldName">
                                    <% field_name_value_intention = {"name": "addterm", "arg": {field_name: mode.get('value', '')}} %>
                                    <a href="#" title="${mode.get('value', '')|h}" s:type="search" s:intention="${json.dumps(field_name_value_intention)|x}">${splunk.util.smartTrim(mode.get('value', 'undefined'), 35)|h}</a>
                                </th>
                                <td>${i18n.format_number(mode.get('count', -1))|h}</td>
                                <td>${i18n.format_percent(percent)|h}</td>
                                <td class="fieldValueGraphCell">
                                    <div class="splBarGraph">
                                        <div style="width:${int(percent*100)|h}%;" class="splBarGraphBar"></div>
                                    </div>
                                </td>
                            </tr>
                        % endfor
                    </tbody>
                </table>
            </div>
        % endif
    </div>
</div>
